-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (66 loc) · 2.32 KB
/
index.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
import React, { useState, useContext } from 'react';
const RenderList = ({children, level, onNodePress, onRenderNode}) => (
children.map(item =>
<TreeNode
key={item.id}
nodeData={item}
level={level}
onNodePress={onNodePress}
onRenderNode={onRenderNode}
/>
)
);
const TreeContext = React.createContext({
selectedId: -1,
expandedId: -1,
singleExpanded: false
});
const TreeNode = ({ nodeData, level, onNodePress, onRenderNode }) => {
const treeContext = useContext(TreeContext);
let { id, children } = nodeData;
let isSelected = treeContext.selectedId === id;
const [expanded, setExpanded] = useState(false);
let isExpanded = treeContext.singleExpanded ? treeContext.expandedId[level] === id : expanded;
const isParent = children && children.length > 0;
const onItemPress = () => {
if (isParent) {
setExpanded(!expanded);
onNodePress(nodeData, level, true);
} else {
onNodePress(nodeData, level, false);
}
};
return (
<>
{onRenderNode(nodeData, level, isParent, isExpanded, isSelected, onItemPress)}
{
isParent && isExpanded &&
<RenderList children={children} level={level+1} onNodePress={onNodePress} onRenderNode={onRenderNode} />
}
</>
)
};
const TreeMenu = ({ treeData, onNodePress, onRenderNode, singleExpanded }) => {
const [selectedId, setSelectedId] = useState(-1);
const [expandedId, setExpandedId] = useState([]);
const onItemPress = (nodeData, level, isParent) => {
let { id } = nodeData;
onNodePress(nodeData, level, isParent);
if (isParent) {
const isExpanded = expandedId[level] === id;
let newExpanded = expandedId.filter((item ,ind) => ind < level);
if (!isExpanded) {
newExpanded[level] = id;
}
setExpandedId(newExpanded);
} else {
setSelectedId(id);
}
};
return (
<TreeContext.Provider value={{ selectedId, expandedId, singleExpanded }}>
<RenderList children={treeData} level={0} onNodePress={onItemPress} onRenderNode={onRenderNode} />
</TreeContext.Provider>
)
};
export default TreeMenu;