/
NestedListView.tsx
133 lines (113 loc) · 2.96 KB
/
NestedListView.tsx
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
130
131
132
133
/* @flow */
import isEqual from 'lodash.isequal'
import * as React from 'react'
import {StyleSheet, Text, View} from 'react-native'
import * as shortid from 'shortid'
import NodeView, {INode} from './NodeView'
const styles = StyleSheet.create({
errorContainer: {
borderColor: 'rgb(84, 85, 86)',
backgroundColor: 'rgb(237, 57, 40)',
borderWidth: 1,
justifyContent: 'center',
alignItems: 'center',
height: 60,
},
errorText: {
color: 'rgb(255, 255, 255)',
fontSize: 17,
fontWeight: 'bold',
},
})
export interface IProps {
data: any
extraData?: any
renderNode: (elem: any, level?: number) => any
onNodePressed?: () => any
getChildrenName: (elem: any) => any
style?: any
}
export interface IState {
root: any
}
export default class NestedListView extends React.PureComponent<
IProps,
IState
> {
public componentWillMount() {
this.setState({root: this.generateRootNode(this.props)})
}
public componentWillReceiveProps(nextProps: any) {
if (!isEqual(this.props.data, nextProps.data)) {
this.setState({root: this.generateRootNode(nextProps)})
}
}
public generateIds = (node?: INode) => {
if (!node) {
return
}
const childrenName: string = this.props.getChildrenName(node) || 'items'
let children = node[childrenName]
if (children) {
if (!Array.isArray(children)) {
children = Object.keys(children).map((key: string) => children[key])
}
node[childrenName] = children.map((_: INode, index: number) =>
this.generateIds(children[index])
)
}
node._internalId = shortid.generate()
return node
}
public getChildrenName = (node: INode) => {
if (node.name === 'root') {
return 'items'
}
return this.props.getChildrenName
? this.props.getChildrenName(node)
: 'items'
}
public renderErrorMessage(prop: string) {
return (
<View style={styles.errorContainer}>
<Text style={styles.errorText}>prop {prop} has not been passed</Text>
</View>
)
}
public render() {
const {data, getChildrenName, onNodePressed, renderNode} = this.props
if (!getChildrenName) {
return this.renderErrorMessage('getChildrenName')
}
if (!renderNode) {
return this.renderErrorMessage('renderNode')
}
if (!data) {
return this.renderErrorMessage('data')
}
return (
<NodeView
getChildrenName={this.getChildrenName}
node={this.state.root}
onNodePressed={onNodePressed}
generateIds={this.generateIds}
level={0}
renderNode={renderNode}
extraData={this.props.extraData}
/>
)
}
private generateRootNode = (props: any): INode => {
return {
_internalId: shortid.generate(),
items: props.data
? props.data.map((_: INode, index: number) =>
this.generateIds(props.data[index])
)
: [],
name: 'root',
opened: true,
hidden: true,
}
}
}