-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
129 lines (107 loc) · 3.48 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
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
123
124
125
126
127
128
129
import get from 'lodash.get'
import PropTypes from 'prop-types'
import React from 'react'
import { TouchableOpacity, View } from 'react-native'
function noop() {}
class TreeView extends React.Component {
static propTypes = {
data: PropTypes.array.isRequired,
renderNode: PropTypes.func.isRequired,
initialExpanded: PropTypes.bool,
idKey: PropTypes.string,
activeOpacityNode: PropTypes.number,
childrenKey: PropTypes.string,
onNodePress: PropTypes.func,
onNodeLongPress: PropTypes.func,
isNodeExpanded: PropTypes.func,
shouldDisableTouchOnLeaf: PropTypes.func,
}
static defaultProps = {
initialExpanded: false,
idKey: 'id',
childrenKey: 'children',
activeOpacityNode: 0.2,
onNodePress: noop,
onNodeLongPress: noop,
isNodeExpanded: noop,
shouldDisableTouchOnLeaf: () => false,
}
constructor(props) {
super(props)
this.state = this.getInitialState()
}
getInitialState = () => ({
expandedNodeKeys: {},
})
componentDidUpdate(prevProps) {
const hasDataUpdated = prevProps.data !== this.props.data
const hasIdKeyUpdated = prevProps.idKey !== this.props.idKey
const childrenKeyUpdated = prevProps.childrenKey !== this.props.childrenKey
if (hasDataUpdated || hasIdKeyUpdated || childrenKeyUpdated) {
this.setState(this.getInitialState())
}
}
hasChildrenNodes = (node) =>
get(node, `${this.props.childrenKey}.length`, 0) > 0
isExpanded = (id) => {
if (this.props.isNodeExpanded !== noop) {
return this.props.isNodeExpanded(id)
} else {
return get(this.state.expandedNodeKeys, id, this.props.initialExpanded)
}
}
updateNodeKeyById =
(id, expanded) =>
({ expandedNodeKeys }) => ({
expandedNodeKeys: Object.assign({}, expandedNodeKeys, {
[id]: expanded,
}),
})
collapseNode = (id) => this.setState(this.updateNodeKeyById(id, false))
expandNode = (id) => this.setState(this.updateNodeKeyById(id, true))
toggleCollapse = (id) => {
const method = this.isExpanded(id) ? 'collapseNode' : 'expandNode'
this[method](id)
}
handleNodePressed = async ({ node, level }) => {
const nodePressResult = await this.props.onNodePress({ node, level })
if (nodePressResult !== false && this.hasChildrenNodes(node)) {
this.toggleCollapse(node[this.props.idKey])
}
}
Node = ({ nodes, level }) => {
const NodeComponent = this.Node
return nodes.map((node) => {
const isExpanded = this.isExpanded(node[this.props.idKey])
const hasChildrenNodes = this.hasChildrenNodes(node)
const shouldRenderLevel = hasChildrenNodes && isExpanded
return (
<View key={node[this.props.idKey]}>
<TouchableOpacity
activeOpacity={this.props.activeOpacityNode}
disabled={this.props.shouldDisableTouchOnLeaf({ node, level })}
onPress={() => this.handleNodePressed({ node, level })}
onLongPress={() => this.props.onNodeLongPress({ node, level })}
>
{React.createElement(this.props.renderNode, {
node,
level,
isExpanded,
hasChildrenNodes,
})}
</TouchableOpacity>
{shouldRenderLevel && (
<NodeComponent
nodes={node[this.props.childrenKey]}
level={level + 1}
/>
)}
</View>
)
})
}
render() {
return <this.Node nodes={this.props.data} level={0} />
}
}
export default TreeView