Skip to content

Commit 3aca30a

Browse files
committed
Refactored nestedlistview
1 parent 5d5d8a3 commit 3aca30a

File tree

3 files changed

+62
-109
lines changed

3 files changed

+62
-109
lines changed

example/ExampleApp.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ const generateXNumItems = (numItems, prefix) => {
2828
const data = [
2929
{
3030
name: 'Item level 1.1',
31-
items: generateXNumItems(100, 'Item level 1.1'),
31+
descendants: generateXNumItems(10, 'Item level 1.1'),
3232
},
3333
{
3434
name: 'Item level 1.2',
35-
items: [
35+
descendants: [
3636
{
3737
name: 'Item level 1.2.1',
3838
},
3939
{
4040
name: 'Item level 1.2.2',
41-
children: generateXNumItems(2, 'Item level 1.2.2'),
41+
children: generateXNumItems(25, 'Item level 1.2.2'),
4242
},
4343
],
4444
},
4545
{
4646
name: 'Item level 1.3',
47-
items: generateXNumItems(1000, 'Item level 1.3'),
47+
descendants: generateXNumItems(50, 'Item level 1.3'),
4848
},
4949
]
5050

@@ -63,7 +63,7 @@ const styles = StyleSheet.create({
6363
borderWidth: 1,
6464
borderColor: 'rgb(0, 0, 0)',
6565
},
66-
nestedListView: {padding: 10},
66+
nestedListView: {padding: 10, flex: 1},
6767
})
6868
export default class ExampleApp extends React.Component<Props, State> {
6969
nestedListView: any
@@ -80,15 +80,15 @@ export default class ExampleApp extends React.Component<Props, State> {
8080
}
8181

8282
onNodePressed = (node: any) => {
83-
Alert.alert(node.id)
83+
// alert(node)
8484
}
8585

8686
getChildrenName = (node: Object) => {
8787
if (node.name === 'Item level 1.2.2') {
8888
return 'children'
8989
}
9090

91-
return 'items'
91+
return 'descendants'
9292
}
9393

9494
render = () => {

example/nestedListView/NodeView.js

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,53 @@ export default class NodeView extends React.PureComponent<Props, State> {
1414
generateIds: Function,
1515
getChildren: Function,
1616
node: any,
17-
searchTree: Function,
1817
onNodePressed: Function,
1918
onLayout: Function,
2019
renderNode: Function,
2120
renderChildrenNode: Function,
2221
}
2322

24-
state: {
25-
childrenNodes: Array<any>,
26-
}
27-
28-
state = {
29-
childrenNodes: [],
30-
}
31-
3223
componentWillMount = () => {
33-
const rootChildren = this.props.getChildren(this.props.node)
34-
35-
if (rootChildren) {
36-
this.setState({
37-
childrenNodes: rootChildren.map((child, index) => {
38-
return this.props.generateIds(rootChildren[index])
39-
}),
40-
})
41-
}
24+
this.setState({
25+
node: this.props.node,
26+
})
4227
}
4328

44-
onNodePressed = (node: any) => {
45-
if (this.state.childrenNodes) {
46-
const childrenNodes = this.state.childrenNodes.map((child, index) => {
47-
return this.props.searchTree(this.state.childrenNodes[index], node)
48-
})
49-
50-
this.setState({childrenNodes})
51-
}
29+
onNodePressed = () => {
30+
this.setState({
31+
node: {
32+
...this.state.node,
33+
opened: !this.state.node.opened,
34+
},
35+
})
36+
}
5237

53-
this.props.onNodePressed(node)
38+
renderChildren = (item: any, level: number) => {
39+
return (
40+
<NodeView
41+
getChildrenName={this.props.getChildrenName}
42+
node={item}
43+
level={level + 1}
44+
renderNode={(item, level) => this.props.renderNode(item, level)}
45+
/>
46+
)
5447
}
5548

5649
render() {
57-
const {node, onLayout, renderNode, renderChildrenNode} = this.props
50+
const rootChildrenName = this.props.getChildrenName(this.state.node)
51+
const rootChildren = this.state.node[rootChildrenName]
5852

5953
return (
60-
<View onLayout={onLayout}>
61-
<TouchableWithoutFeedback onPress={() => this.onNodePressed(node)}>
62-
{renderNode()}
63-
</TouchableWithoutFeedback>
64-
{node.opened && this.state.childrenNodes ? (
54+
<View>
55+
{!this.state.node.hidden ? (
56+
<TouchableWithoutFeedback onPress={this.onNodePressed}>
57+
{this.props.renderNode(this.state.node, this.props.level)}
58+
</TouchableWithoutFeedback>
59+
) : null}
60+
{this.state.node.opened && rootChildren ? (
6561
<FlatList
66-
data={this.state.childrenNodes}
67-
renderItem={({item}) => renderChildrenNode(item)}
62+
data={rootChildren}
63+
renderItem={({item}) => this.renderChildren(item, this.props.level)}
6864
keyExtractor={item => item.id}
6965
/>
7066
) : null}

example/nestedListView/index.js

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import React from 'react'
4-
import {FlatList, type Props, type State} from 'react-native'
4+
import {FlatList, type Props, type State, View} from 'react-native'
55
import NodeView from './NodeView'
66
import shortid from 'shortid'
77

@@ -14,47 +14,18 @@ export default class NestedListView extends React.PureComponent<Props, State> {
1414
style: any,
1515
}
1616

17-
state: {
18-
childrenNodes: Array<any>,
19-
}
20-
21-
state = {
22-
childrenNodes: [],
23-
}
24-
2517
componentWillMount = () => {
26-
const rootChildren = this.props.data
27-
if (rootChildren) {
28-
this.setState({
29-
childrenNodes: rootChildren.map((child, index) =>
30-
this.generateIds(rootChildren[index])
31-
),
32-
})
33-
}
34-
}
35-
36-
searchTree = (element: any, otherElement: any) => {
37-
if (element.id === otherElement.id) {
38-
element.opened = !element.opened
39-
40-
return element
18+
const root = {
19+
id: 1,
20+
items: this.props.data.map((child, index) =>
21+
this.generateIds(this.props.data[index])
22+
),
23+
name: 'root',
24+
opened: true,
25+
hidden: true,
4126
}
4227

43-
const childrenName = this.props.getChildrenName(element)
44-
45-
if (childrenName) {
46-
const children = element[childrenName]
47-
48-
if (children) {
49-
element[childrenName] = children.map((child, index) =>
50-
this.searchTree(children[index], otherElement)
51-
)
52-
}
53-
54-
return element
55-
}
56-
57-
return element
28+
this.setState({root})
5829
}
5930

6031
generateIds = (element: any) => {
@@ -79,39 +50,25 @@ export default class NestedListView extends React.PureComponent<Props, State> {
7950
return element
8051
}
8152

82-
onNodePressed = (node: any) => {
83-
const childrenNodes = this.state.childrenNodes.map((child, index) =>
84-
this.searchTree(this.state.childrenNodes[index], node)
85-
)
86-
87-
this.setState({childrenNodes})
88-
this.props.onNodePressed(node)
89-
}
53+
getChildrenName = node => {
54+
if (node.name === 'root') {
55+
return 'items'
56+
}
9057

91-
onCreateChildren = (item: any, level: number) => {
92-
return (
93-
<NodeView
94-
getChildren={(node: Object) => node[this.props.getChildrenName(node)]}
95-
key={item.id}
96-
node={item}
97-
searchTree={this.searchTree}
98-
generateIds={this.generateIds}
99-
onNodePressed={() => this.onNodePressed(item)}
100-
renderChildrenNode={(childrenNode: Object) =>
101-
this.onCreateChildren(childrenNode, level + 1)}
102-
renderNode={() => this.props.renderNode(item, level)}
103-
/>
104-
)
58+
return this.props.getChildrenName(node)
10559
}
10660

10761
render = () => {
10862
return (
109-
<FlatList
110-
data={this.state.childrenNodes}
111-
style={this.props.style}
112-
renderItem={({item}) => this.onCreateChildren(item, 0)}
113-
keyExtractor={item => item.id}
114-
/>
63+
<View style={this.props.style}>
64+
<NodeView
65+
getChildrenName={this.getChildrenName}
66+
node={this.state.root}
67+
generateIds={this.generateIds}
68+
level={0}
69+
renderNode={(item, level) => this.props.renderNode(item, level)}
70+
/>
71+
</View>
11572
)
11673
}
11774
}

0 commit comments

Comments
 (0)