-
Notifications
You must be signed in to change notification settings - Fork 95
/
TreeView.js
122 lines (112 loc) · 3.27 KB
/
TreeView.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
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, {
useContext,
useCallback,
useLayoutEffect,
useState,
memo,
} from 'react';
import PropTypes from 'prop-types';
import ExpandedPathsContext from './ExpandedPathsContext';
import TreeNode from './TreeNode';
import {
DEFAULT_ROOT_PATH,
hasChildNodes,
getExpandedPaths,
} from './pathUtils';
import { useStyles } from '../styles';
const ConnectedTreeNode = memo(props => {
const { data, dataIterator, path, depth, nodeRenderer } = props;
const [expandedPaths, setExpandedPaths] = useContext(ExpandedPathsContext);
const nodeHasChildNodes = hasChildNodes(data, dataIterator);
const expanded = !!expandedPaths[path];
const handleClick = useCallback(
() =>
nodeHasChildNodes &&
setExpandedPaths(prevExpandedPaths => ({
...prevExpandedPaths,
[path]: !expanded,
})),
[nodeHasChildNodes, setExpandedPaths, path, expanded]
);
return (
<TreeNode
expanded={expanded}
onClick={handleClick}
// show arrow anyway even if not expanded and not rendering children
shouldShowArrow={nodeHasChildNodes}
// show placeholder only for non root nodes
shouldShowPlaceholder={depth > 0}
// Render a node from name and data (or possibly other props like isNonenumerable)
nodeRenderer={nodeRenderer}
{...props}>
{// only render if the node is expanded
expanded
? [...dataIterator(data)].map(({ name, data, ...renderNodeProps }) => {
return (
<ConnectedTreeNode
name={name}
data={data}
depth={depth + 1}
path={`${path}.${name}`}
key={name}
dataIterator={dataIterator}
nodeRenderer={nodeRenderer}
{...renderNodeProps}
/>
);
})
: null}
</TreeNode>
);
});
ConnectedTreeNode.propTypes = {
name: PropTypes.string,
data: PropTypes.any,
dataIterator: PropTypes.func,
depth: PropTypes.number,
expanded: PropTypes.bool,
nodeRenderer: PropTypes.func,
};
const TreeView = memo(
({ name, data, dataIterator, nodeRenderer, expandPaths, expandLevel }) => {
const styles = useStyles('TreeView');
const stateAndSetter = useState({});
const [, setExpandedPaths] = stateAndSetter;
useLayoutEffect(
() =>
setExpandedPaths(prevExpandedPaths =>
getExpandedPaths(
data,
dataIterator,
expandPaths,
expandLevel,
prevExpandedPaths
)
),
[data, dataIterator, expandPaths, expandLevel]
);
return (
<ExpandedPathsContext.Provider value={stateAndSetter}>
<ol role="tree" style={styles.treeViewOutline}>
<ConnectedTreeNode
name={name}
data={data}
dataIterator={dataIterator}
depth={0}
path={DEFAULT_ROOT_PATH}
nodeRenderer={nodeRenderer}
/>
</ol>
</ExpandedPathsContext.Provider>
);
}
);
TreeView.propTypes = {
name: PropTypes.string,
data: PropTypes.any,
dataIterator: PropTypes.func,
nodeRenderer: PropTypes.func,
expandPaths: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
expandLevel: PropTypes.number,
};
export default TreeView;