Skip to content

Commit 0e868bf

Browse files
author
Javier Morant
authored
Merge pull request #73 from fjmorant/childrenAsObject
2 parents 161cbda + 09de462 commit 0e868bf

File tree

9 files changed

+2007
-67
lines changed

9 files changed

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

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,57 @@ 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+
subChild1: {
122+
name: 'Sample',
123+
children: {},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
child2: {
130+
name: 'Main Child 2',
131+
children: {
132+
child1: {
133+
name: 'Sub Child 1',
134+
children: {},
135+
},
136+
child2: {
137+
name: 'Sub Child 2',
138+
children: {},
139+
},
140+
},
141+
},
142+
},
143+
},
144+
]
145+
const nestedListView = renderer
146+
.create(
147+
<NestedListView
148+
getChildrenName={(node: any) => 'children'}
149+
renderNode={renderNode}
150+
data={data}
151+
/>
152+
)
153+
.toJSON()
154+
expect(nestedListView).toMatchSnapshot()
155+
})
156+
106157
test('renders with onNodePressed', () => {
107158
const data = [{title: 'child1'}, {title: 'child2'}, {title: 'child3'}]
108159
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
)

0 commit comments

Comments
 (0)