Skip to content

Commit 2f6a19a

Browse files
committed
Convert generateIds to support objects as well
1 parent 85dc24e commit 2f6a19a

File tree

9 files changed

+1999
-67
lines changed

9 files changed

+1999
-67
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* @flow */
2+
3+
import React from 'react'
4+
import {
5+
Alert,
6+
StyleSheet,
7+
Text,
8+
View,
9+
type Props,
10+
type State,
11+
} from 'react-native'
12+
import NestedListView from 'react-native-nested-listview'
13+
14+
const data = [
15+
{
16+
name: 'Main Parent',
17+
children: {
18+
child1: {
19+
name: 'Main Child 1',
20+
children: {
21+
child1: {
22+
name: 'Sub Child 1',
23+
children: {},
24+
},
25+
child2: {
26+
name: 'Sub Child 2',
27+
children: {
28+
name: 'Sample',
29+
children: {},
30+
},
31+
},
32+
},
33+
},
34+
child2: {
35+
name: 'Main Child 2',
36+
children: {
37+
child1: {
38+
name: 'Sub Child 1',
39+
children: {},
40+
},
41+
child2: {
42+
name: 'Sub Child 2',
43+
children: {},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
]
50+
51+
const colorLevels = {
52+
[0]: 'white',
53+
[1]: 'blue',
54+
[2]: 'green',
55+
[3]: 'red',
56+
}
57+
58+
const styles = StyleSheet.create({
59+
container: {flex: 1, backgroundColor: 'rgb(255, 255, 255)', padding: 15},
60+
node: {
61+
flex: 1,
62+
padding: 10,
63+
borderWidth: 1,
64+
borderColor: 'rgb(0, 0, 0)',
65+
},
66+
})
67+
export default class ExampleApp extends React.Component<Props, State> {
68+
nestedListView: any
69+
70+
renderNode = (node: Object, level: number) => {
71+
const paddingLeft = (level + 1) * 30
72+
const backgroundColor = colorLevels[level] || 'white'
73+
74+
return (
75+
<View style={[styles.node, {backgroundColor, paddingLeft}]}>
76+
<Text>{node.name}</Text>
77+
</View>
78+
)
79+
}
80+
81+
onNodePressed = (node: any) => {
82+
alert(node.name)
83+
}
84+
85+
getChildrenName = (node: Object) => {
86+
return 'children'
87+
}
88+
89+
render = () => {
90+
return (
91+
<View style={styles.container}>
92+
<NestedListView
93+
data={data}
94+
getChildrenName={this.getChildrenName}
95+
onNodePressed={this.onNodePressed}
96+
renderNode={this.renderNode}
97+
/>
98+
</View>
99+
)
100+
}
101+
}

examples/HomeScreen.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const options = [
2323
screen: 'DynamicContentExample',
2424
title: 'Dynamic Content',
2525
},
26+
{
27+
screen: 'ChildrenAsObjectExample',
28+
title: 'Children as Object',
29+
},
2630
]
2731

2832
export default class HomeScreen extends React.Component {

examples/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import StateChangeNodeExample from './StateChangeNodeExample'
55
import ErrorMessageExample from './ErrorMessageExample'
66
import NestedRowExample from './NestedRowExample'
77
import DynamicContentExample from './DynamicContentExample'
8+
import ChildrenAsObjectExample from './ChildrenAsObjectExample'
89

910
import HomeScreen from './HomeScreen'
1011

@@ -17,6 +18,7 @@ const SimpleApp = StackNavigator({
1718
ErrorMessageExample: {screen: ErrorMessageExample},
1819
NestedRowExample: {screen: NestedRowExample},
1920
DynamicContentExample: {screen: DynamicContentExample},
21+
ChildrenAsObjectExample: {screen: ChildrenAsObjectExample},
2022
})
2123

2224
AppRegistry.registerComponent('example', () => SimpleApp)

examples/yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3712,10 +3712,12 @@ react-native-drawer-layout@1.3.2:
37123712
react-native-dismiss-keyboard "1.0.0"
37133713

37143714
"react-native-nested-listview@file:..":
3715-
version "0.3.0"
3715+
version "0.4.3"
37163716
dependencies:
37173717
"@types/lodash.isequal" "^4.5.2"
37183718
lodash.isequal "^4.5.0"
3719+
react "16.2.0"
3720+
react-native "0.54.2"
37193721
shortid "^2.2.8"
37203722

37213723
react-native-safe-area-view@^0.7.0:
@@ -3824,6 +3826,15 @@ react-transform-hmr@^1.0.4:
38243826
global "^4.3.0"
38253827
react-proxy "^1.1.7"
38263828

3829+
react@16.2.0:
3830+
version "16.2.0"
3831+
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
3832+
dependencies:
3833+
fbjs "^0.8.16"
3834+
loose-envify "^1.1.0"
3835+
object-assign "^4.1.1"
3836+
prop-types "^15.6.0"
3837+
38273838
react@^16.3.0-alpha.1:
38283839
version "16.3.0-alpha.3"
38293840
resolved "https://registry.yarnpkg.com/react/-/react-16.3.0-alpha.3.tgz#b925cbb4a971db2b0ab87cb9da449227d1ccafb4"

package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"dependencies": {
1919
"@types/lodash.isequal": "^4.5.2",
2020
"lodash.isequal": "^4.5.0",
21+
"react": "16.2.0",
22+
"react-native": "0.54.2",
2123
"shortid": "^2.2.8"
2224
},
2325
"peerDependencies": {
@@ -49,7 +51,12 @@
4951
"tslint": "^5.9.1",
5052
"typescript": "^2.7.2"
5153
},
52-
"keywords": ["react", "native", "list", "nested"],
54+
"keywords": [
55+
"react",
56+
"native",
57+
"list",
58+
"nested"
59+
],
5360
"author": "Javier Morant",
5461
"license": "MIT",
5562
"bugs": {
@@ -58,7 +65,11 @@
5865
"homepage": "https://github.com/fjmorant/react-native-nested-listview#readme",
5966
"jest": {
6067
"preset": "react-native",
61-
"moduleFileExtensions": ["ts", "tsx", "js"],
68+
"moduleFileExtensions": [
69+
"ts",
70+
"tsx",
71+
"js"
72+
],
6273
"transform": {
6374
"^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
6475
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
@@ -67,7 +78,9 @@
6778
"node_modules/(?!(jest-)|react-native|react-navigation|react-clone-referenced-element|mobx-react)"
6879
],
6980
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
70-
"modulePathIgnorePatterns": ["<rootDir>/examples/"],
81+
"modulePathIgnorePatterns": [
82+
"<rootDir>/examples/"
83+
],
7184
"testPathIgnorePatterns": [
7285
"\\.snap$",
7386
"<rootDir>/node_modules/",

src/NestedListView.test.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,60 @@ describe('NestedListView', () => {
103103
expect(nestedListView).toMatchSnapshot()
104104
})
105105

106+
test('renders with children as objects', () => {
107+
const data = [
108+
{
109+
name: 'Main Parent',
110+
children: {
111+
child1: {
112+
name: 'Main Child 1',
113+
children: {
114+
child1: {
115+
name: 'Sub Child 1',
116+
children: {},
117+
},
118+
child2: {
119+
name: 'Sub Child 2',
120+
children: {
121+
name: 'Sample',
122+
children: {},
123+
},
124+
},
125+
},
126+
},
127+
child2: {
128+
name: 'Main Child 2',
129+
children: {
130+
child1: {
131+
name: 'Sub Child 1',
132+
children: {},
133+
},
134+
child2: {
135+
name: 'Sub Child 2',
136+
children: {},
137+
},
138+
},
139+
},
140+
},
141+
},
142+
]
143+
const nestedListView = renderer
144+
.create(
145+
<NestedListView
146+
getChildrenName={(node: any) => {
147+
if (node.title === 'child2') {
148+
return 'children'
149+
}
150+
return 'items'
151+
}}
152+
renderNode={renderNode}
153+
data={data}
154+
/>
155+
)
156+
.toJSON()
157+
expect(nestedListView).toMatchSnapshot()
158+
})
159+
106160
test('renders with onNodePressed', () => {
107161
const data = [{title: 'child1'}, {title: 'child2'}, {title: 'child3'}]
108162
const nestedListView = renderer

src/NestedListView.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ export default class NestedListView extends React.PureComponent<
5454
}
5555

5656
const childrenName: string = this.props.getChildrenName(node) || 'items'
57-
const children = node[childrenName]
57+
let children = node[childrenName]
5858

5959
if (children) {
60+
if (!Array.isArray(children)) {
61+
children = Object.keys(children).map((key: string) => children[key])
62+
}
6063
node[childrenName] = children.map((_: INode, index: number) =>
6164
this.generateIds(children[index])
6265
)

src/__snapshots__/NestedListView.test.tsx.snap

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,111 @@ exports[`NestedListView renders with NestedRow 1`] = `
165165
</View>
166166
`;
167167

168+
exports[`NestedListView renders with children as objects 1`] = `
169+
<View>
170+
<RCTScrollView
171+
data={
172+
Array [
173+
Object {
174+
"children": Object {
175+
"child1": Object {
176+
"children": Object {
177+
"child1": Object {
178+
"children": Object {},
179+
"name": "Sub Child 1",
180+
},
181+
"child2": Object {
182+
"children": Object {
183+
"children": Object {},
184+
"name": "Sample",
185+
},
186+
"name": "Sub Child 2",
187+
},
188+
},
189+
"name": "Main Child 1",
190+
},
191+
"child2": Object {
192+
"children": Object {
193+
"child1": Object {
194+
"children": Object {},
195+
"name": "Sub Child 1",
196+
},
197+
"child2": Object {
198+
"children": Object {},
199+
"name": "Sub Child 2",
200+
},
201+
},
202+
"name": "Main Child 2",
203+
},
204+
},
205+
"id": "1",
206+
"name": "Main Parent",
207+
},
208+
]
209+
}
210+
disableVirtualization={false}
211+
getItem={[Function]}
212+
getItemCount={[Function]}
213+
horizontal={false}
214+
initialNumToRender={10}
215+
invertStickyHeaders={undefined}
216+
keyExtractor={[Function]}
217+
maxToRenderPerBatch={10}
218+
numColumns={1}
219+
onContentSizeChange={[Function]}
220+
onEndReachedThreshold={2}
221+
onLayout={[Function]}
222+
onMomentumScrollEnd={[Function]}
223+
onScroll={[Function]}
224+
onScrollBeginDrag={[Function]}
225+
onScrollEndDrag={[Function]}
226+
renderItem={[Function]}
227+
scrollEventThrottle={50}
228+
stickyHeaderIndices={Array []}
229+
updateCellsBatchingPeriod={50}
230+
viewabilityConfigCallbackPairs={Array []}
231+
windowSize={21}
232+
>
233+
<View>
234+
<View
235+
onLayout={[Function]}
236+
style={null}
237+
>
238+
<View>
239+
<View
240+
accessibilityComponentType={undefined}
241+
accessibilityLabel={undefined}
242+
accessibilityTraits={undefined}
243+
accessible={true}
244+
hitSlop={undefined}
245+
nativeID={undefined}
246+
onLayout={undefined}
247+
onResponderGrant={[Function]}
248+
onResponderMove={[Function]}
249+
onResponderRelease={[Function]}
250+
onResponderTerminate={[Function]}
251+
onResponderTerminationRequest={[Function]}
252+
onStartShouldSetResponder={[Function]}
253+
style={undefined}
254+
testID={undefined}
255+
>
256+
<View>
257+
<Text
258+
accessible={true}
259+
allowFontScaling={true}
260+
ellipsizeMode="tail"
261+
>
262+
Main Parent
263+
</Text>
264+
</View>
265+
</View>
266+
</View>
267+
</View>
268+
</View>
269+
</RCTScrollView>
270+
</View>
271+
`;
272+
168273
exports[`NestedListView renders with nested arrays 1`] = `
169274
<View>
170275
<RCTScrollView

0 commit comments

Comments
 (0)