forked from assisrafael/react-bootstrap-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeView.jsx
90 lines (83 loc) · 2.3 KB
/
TreeView.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { getValueByPath } from '../utils';
export function TreeView({ childrenPath, draggable, nodes, template }) {
return (
<div className="rbu-treeview">
<ul className="list-group">
<TreeNodes
depth={0}
nodes={nodes}
template={template}
childrenPath={childrenPath}
draggable={draggable}
relativePath={`${childrenPath}`}
/>
</ul>
</div>
);
}
TreeView.propTypes = {
childrenPath: PropTypes.string.isRequired,
draggable: PropTypes.bool,
nodes: PropTypes.array.isRequired,
template: PropTypes.func.isRequired,
};
function TreeNodes({ childrenPath, depth, draggable, nodes, parentNode, template, relativePath }) {
return (
<>
{nodes.map((node, index) => (
<TreeNode
key={index}
node={node}
parentNode={parentNode}
index={index}
template={template}
childrenPath={childrenPath}
depth={depth}
draggable={draggable}
relativePath={`${relativePath}[${index}]`}
/>
))}
</>
);
}
TreeNodes.propTypes = {
childrenPath: PropTypes.string,
depth: PropTypes.number,
draggable: PropTypes.bool,
nodes: PropTypes.array,
parentNode: PropTypes.object,
template: PropTypes.func,
relativePath: PropTypes.string,
};
function TreeNode({ node, parentNode, index, template, childrenPath, depth, draggable, relativePath }) {
const childrenNodes = getValueByPath(node, childrenPath);
return (
<div draggable={draggable}>
<li className="list-group-item" style={{ marginLeft: `${depth * 20}px`, borderTopWidth: '1px' }}>
{template(node, index, parentNode, relativePath)}
</li>
{childrenNodes && (
<TreeNodes
nodes={childrenNodes}
parentNode={node}
template={template}
childrenPath={childrenPath}
depth={depth + 1}
relativePath={`${relativePath}.${childrenPath}`}
/>
)}
</div>
);
}
TreeNode.propTypes = {
node: PropTypes.object,
parentNode: PropTypes.object,
index: PropTypes.number,
template: PropTypes.func,
childrenPath: PropTypes.string,
depth: PropTypes.number,
draggable: PropTypes.bool,
relativePath: PropTypes.string,
};