-
Notifications
You must be signed in to change notification settings - Fork 49
/
tree-node-renderer.js
109 lines (99 loc) · 2.95 KB
/
tree-node-renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React, { Component, Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import styles from './tree-node-renderer.scss';
class MinimalThemeTreeNodeRenderer extends Component {
constructor (props) {
super(props)
this.state = {}
this.bound = {
handleMouseOver: this.handleMouseOver.bind(this),
handleMouseLeave: this.handleMouseLeave.bind(this)
}
}
handleMouseOver () {
if (!this.state.highlight) {
this.setState({highlight: true})
}
}
handleMouseLeave () {
this.setState({highlight: false})
}
render() {
const {
children,
swapFrom,
swapLength,
swapDepth,
scaffoldBlockPxWidth,
lowerSiblingCounts,
connectDropTarget,
isOver,
draggedNode,
canDrop,
treeIndex,
treeId,
listIndex,
rowDirection,
getPrevRow, // Delete from otherProps
node, // Delete from otherProps
path, // Delete from otherProps
...otherProps
} = this.props;
// Construct the scaffold representing the structure of the tree
const scaffoldBlockCount = lowerSiblingCounts.length - 1;
let dropType
if (canDrop && !isOver) {
dropType = 'validDrop'
} else if (!canDrop && isOver) {
dropType = 'invalidDrop'
}
return connectDropTarget(
<div {...otherProps} onMouseOver={this.bound.handleMouseOver} onMouseLeave={this.bound.handleMouseLeave} {...otherProps} onFocus={ () => {} } className={styles.node + (this.state.highlight ? ` ${styles.highlight}` : '') + (dropType ? ` ${styles[dropType]}` : '')}>
<div
className={styles.nodeContent}
style={{paddingLeft: scaffoldBlockPxWidth * scaffoldBlockCount}}
>
{Children.map(children, child =>
cloneElement(child, {
isOver,
canDrop,
draggedNode,
})
)}
</div>
</div>
);
}
}
MinimalThemeTreeNodeRenderer.defaultProps = {
swapFrom: null,
swapDepth: null,
swapLength: null,
canDrop: false,
draggedNode: null,
rowDirection: 'ltr'
};
MinimalThemeTreeNodeRenderer.propTypes = {
treeIndex: PropTypes.number.isRequired,
swapFrom: PropTypes.number,
swapDepth: PropTypes.number,
swapLength: PropTypes.number,
scaffoldBlockPxWidth: PropTypes.number.isRequired,
lowerSiblingCounts: PropTypes.arrayOf(PropTypes.number).isRequired,
treeId: PropTypes.string.isRequired,
listIndex: PropTypes.number.isRequired,
rowDirection: PropTypes.string,
children: PropTypes.node.isRequired,
// Drop target
connectDropTarget: PropTypes.func.isRequired,
isOver: PropTypes.bool.isRequired,
canDrop: PropTypes.bool,
draggedNode: PropTypes.shape({}),
// used in dndManager
getPrevRow: PropTypes.func.isRequired,
node: PropTypes.shape({}).isRequired,
path: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
).isRequired
};
export default MinimalThemeTreeNodeRenderer;