Skip to content

Commit 7ec08ee

Browse files
committed
Use flatlist
1 parent fc79c1d commit 7ec08ee

File tree

3 files changed

+54
-63
lines changed

3 files changed

+54
-63
lines changed

example/ExampleApp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const data = [
3131
},
3232
{
3333
name: 'Item level 1.2.2',
34-
children: generateXNumItems(1000, 'Item level 1.2.2')
34+
children: generateXNumItems(2, 'Item level 1.2.2')
3535
},
3636
],
3737
},
@@ -45,7 +45,7 @@ export default class ExampleApp extends React.Component {
4545
nestedListView: any
4646

4747
renderNode = (node: Object, level: string) => {
48-
const paddingLeft = (level + 1) * 10
48+
const paddingLeft = (level + 1) * 30
4949
return (
5050
<View style={{flex: 1, padding: 10, paddingLeft, borderWidth: 1, borderColor: 'rgb(0, 0, 0)'}}>
5151
<Text>{node.name}</Text>

example/nestedListView/NodeView.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
/* @flow */
22

33
import React from 'react'
4-
import {TouchableOpacity, View} from 'react-native'
4+
import {TouchableOpacity, View, FlatList} from 'react-native'
55

6-
const NodeView = ({
7-
getChildren,
8-
node,
9-
renderNode,
10-
renderChildrenNode,
11-
onLayout,
12-
onNodePressed,
13-
}: {
14-
getChildren: Function,
15-
node: any,
16-
nodeStyle?: any,
17-
onLayout?: Function,
18-
onNodePressed: Function,
19-
renderNode: Function,
20-
renderChildrenNode: Function,
21-
}) => {
22-
const children = getChildren(node)
6+
export default class NodeView extends React.PureComponent {
237

24-
return (
25-
<View onLayout={onLayout}>
26-
<TouchableOpacity onPress={() => onNodePressed(node)}>
27-
{renderNode()}
28-
</TouchableOpacity>
29-
{node.opened && children
30-
? <View>
31-
{children.map(renderChildrenNode)}
32-
</View>
33-
: null}
34-
</View>
35-
)
36-
}
8+
componentWillMount = () => {
9+
let rootChildren = this.props.getChildren(this.props.node)
10+
11+
if (rootChildren) {
12+
rootChildren = rootChildren.map((child, index) => {
13+
return this.props.generateIds(rootChildren[index])
14+
})
15+
}
16+
17+
this.setState({data: rootChildren})
18+
}
19+
20+
onNodePressed = (node: any) => {
21+
const newState = rootChildren = this.state.data.map((child, index) => {
22+
return this.props.searchTree(this.state.data[index], node)
23+
})
3724

38-
export default NodeView
25+
this.setState({data: newState})
26+
this.props.onNodePressed(node)
27+
}
28+
29+
render() {
30+
const {getChildren, node, nodeStyle, onLayout, onNodePressed, renderNode, renderChildrenNode} = this.props
31+
const children = getChildren(node)
32+
33+
return (
34+
<View onLayout={onLayout}>
35+
<TouchableOpacity onPress={() => onNodePressed(node)}>
36+
{renderNode()}
37+
</TouchableOpacity>
38+
{node.opened && this.state.data
39+
? <FlatList
40+
data={this.state.data}
41+
renderItem={({item}) => renderChildrenNode(item)}
42+
keyExtractor={(item) => item.id}/> : null}
43+
</View>
44+
)
45+
}
46+
}

example/nestedListView/index.js

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

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

@@ -100,13 +100,13 @@ export default class NestedListView extends React.Component {
100100
}
101101

102102
searchTree = (element: any, otherElement: any) => {
103-
const childrenName = this.props.getChildrenName(element)
104-
105-
if (element.id == otherElement.id) {
103+
if (element.id === otherElement.id) {
106104
element.opened = !element.opened
107105

108106
return element
109107
}
108+
109+
const childrenName = this.props.getChildrenName(element)
110110

111111
if (childrenName) {
112112
const children = element[childrenName]
@@ -154,45 +154,28 @@ export default class NestedListView extends React.Component {
154154
this.props.onNodePressed(node)
155155
}
156156

157-
onLayoutSelectedNode = (event: any) => {
158-
const {x, y} = event.nativeEvent.layout
159-
160-
this.scrollview.scrollTo({x, y, animated: true})
161-
}
162-
163157
onCreateChildren = (item: any, level: number) => {
164-
const autoScrollToNodeId = this.props.autoScrollToNodeId
165-
166158
return (
167159
<NodeView
168160
getChildren={(node: Object) => node[this.props.getChildrenName(node)]}
169161
key={item.id}
170162
node={item}
171-
onLayout={
172-
autoScrollToNodeId && item.id_video === autoScrollToNodeId
173-
? this.onLayoutSelectedNode
174-
: null
175-
}
163+
searchTree={this.searchTree}
164+
generateIds={this.generateIds}
176165
onNodePressed={() => this.onNodePressed(item)}
177-
renderChildrenNode={(childrenNode: Object) =>
178-
this.onCreateChildren(childrenNode, level + 1)}
166+
renderChildrenNode={(childrenNode: Object) => this.onCreateChildren(childrenNode, level + 1)}
179167
renderNode={() => this.props.renderNode(item, level)}
180168
/>
181169
)
182170
}
183171

184172
render = () => {
185-
186173
return (
187-
<ScrollView
188-
ref={ref => {
189-
this.scrollview = ref
190-
}}
191-
style={this.props.style}>
192-
{this.state.data
193-
? this.state.data.map((item: Object) => this.onCreateChildren(item, 0))
194-
: null}
195-
</ScrollView>
174+
<FlatList
175+
data={this.state.data}
176+
style={this.props.style}
177+
renderItem={({item}) => this.onCreateChildren(item, 0)}
178+
keyExtractor={(item) => item.id}/>
196179
)
197180
}
198181
}

0 commit comments

Comments
 (0)