Skip to content

Commit 9390f41

Browse files
author
Javier Morant
authored
Added extra data param in nestedListview (#91)
* Use extra data * Added extra data example * Fix tests issues * Bumped version number * Fixed TS issue
1 parent 3a2d06b commit 9390f41

File tree

10 files changed

+225
-1741
lines changed

10 files changed

+225
-1741
lines changed

examples/ExtraDataExample.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
TouchableOpacity,
12+
} from 'react-native'
13+
import NestedListView, {
14+
type Node,
15+
NestedRow,
16+
} from 'react-native-nested-listview'
17+
18+
const generateXNumItems = (numItems, prefix) => {
19+
const items = []
20+
21+
let i
22+
23+
for (i = 0; i < numItems; i++) {
24+
items.push({
25+
name: `${prefix}.${i}`,
26+
selected: false,
27+
})
28+
}
29+
30+
return items
31+
}
32+
33+
const data = [
34+
{
35+
name: 'Item level 1.1',
36+
customId: '1.1',
37+
selected: false,
38+
},
39+
{
40+
name: 'Item level 1.2',
41+
selected: false,
42+
customId: '1.2',
43+
descendants: [
44+
{
45+
name: 'Item level 1.2.1',
46+
customId: '1.2.1',
47+
selected: false,
48+
},
49+
{
50+
name: 'Item level 1.2.2',
51+
customId: '1.2.2',
52+
selected: false,
53+
},
54+
],
55+
},
56+
{
57+
name: 'Item level 1.3',
58+
customId: '1.3',
59+
selected: false,
60+
},
61+
]
62+
63+
const styles = StyleSheet.create({
64+
container: {flex: 1, backgroundColor: 'rgb(255, 255, 255)', padding: 15},
65+
node: {
66+
flex: 1,
67+
padding: 10,
68+
borderWidth: 1,
69+
borderColor: 'rgb(0, 0, 0)',
70+
},
71+
})
72+
export default class ExampleApp extends React.Component<Props, State> {
73+
nestedListView: any
74+
75+
constructor(props) {
76+
super(props)
77+
this.state = {
78+
selected: [],
79+
}
80+
}
81+
82+
getChildrenName = (node: Node) => {
83+
if (node.name === 'Item level 1.2.2') {
84+
return 'children'
85+
}
86+
87+
return 'descendants'
88+
}
89+
90+
toggleChecked = node => {
91+
if (this.state.selected.includes(node.customId)) {
92+
const selected = this.state.selected.filter(id => id !== node.customId)
93+
this.setState({
94+
selected,
95+
})
96+
} else {
97+
this.setState({
98+
selected: [node.customId, ...this.state.selected],
99+
})
100+
}
101+
}
102+
103+
render = () => {
104+
console.log('Render NestedRowExample')
105+
return (
106+
<View style={styles.container}>
107+
<NestedListView
108+
data={data}
109+
extraData={this.state.selected}
110+
getChildrenName={this.getChildrenName}
111+
renderNode={(node: Node, level: number) => (
112+
<View>
113+
<Text>{node.name}</Text>
114+
<TouchableOpacity onPress={() => this.toggleChecked(node)}>
115+
{this.state.selected.includes(node.customId) ? (
116+
<Text>CHECKED</Text>
117+
) : (
118+
<Text>NOT CHECKED</Text>
119+
)}
120+
</TouchableOpacity>
121+
</View>
122+
)}
123+
/>
124+
</View>
125+
)
126+
}
127+
}

examples/HomeScreen.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const options = [
1919
screen: 'NestedRowExample',
2020
title: 'NestedRow',
2121
},
22+
{
23+
screen: 'ExtraDataExample',
24+
title: 'ExtraDataExample',
25+
},
2226
{
2327
screen: 'DynamicContentExample',
2428
title: 'Dynamic Content',

examples/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ErrorMessageExample from './ErrorMessageExample'
66
import NestedRowExample from './NestedRowExample'
77
import DynamicContentExample from './DynamicContentExample'
88
import ChildrenAsObjectExample from './ChildrenAsObjectExample'
9+
import ExtraDataExample from './ExtraDataExample'
910

1011
import HomeScreen from './HomeScreen'
1112

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

examples/yarn.lock

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@
399399
"@types/lodash" "*"
400400

401401
"@types/lodash@*":
402-
version "4.14.106"
403-
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.106.tgz#6093e9a02aa567ddecfe9afadca89e53e5dce4dd"
402+
version "4.14.112"
403+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.112.tgz#4a8d8e5716b97a1ac01fe1931ad1e4cba719de5a"
404404

405405
abbrev@1:
406406
version "1.1.0"
@@ -3712,12 +3712,10 @@ 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.4.3"
3715+
version "0.5.0"
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"
37213719
shortid "^2.2.8"
37223720

37233721
react-native-safe-area-view@^0.7.0:
@@ -3826,15 +3824,6 @@ react-transform-hmr@^1.0.4:
38263824
global "^4.3.0"
38273825
react-proxy "^1.1.7"
38283826

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-
38383827
react@^16.3.0-alpha.1:
38393828
version "16.3.0-alpha.3"
38403829
resolved "https://registry.yarnpkg.com/react/-/react-16.3.0-alpha.3.tgz#b925cbb4a971db2b0ab87cb9da449227d1ccafb4"

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "react-native-nested-listview",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "Nested Listview for React native",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
77
"scripts": {
8-
"test": "jest && codecov",
8+
"test": "jest -u && codecov",
99
"lint": "eslint --cache ./src",
1010
"tslint": "tslint -p .",
1111
"type-check": "tsc --noEmit",

src/NestedListView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const styles = StyleSheet.create({
2424

2525
export interface IProps {
2626
data: any
27+
extraData?: any
2728
renderNode: (elem: any, level?: number) => any
2829
onNodePressed?: () => any
2930
getChildrenName: (elem: any) => any
@@ -111,6 +112,7 @@ export default class NestedListView extends React.PureComponent<
111112
generateIds={this.generateIds}
112113
level={0}
113114
renderNode={renderNode}
115+
extraData={this.props.extraData}
114116
/>
115117
)
116118
}

src/NodeView.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ export interface IProps {
2020
onNodePressed?: (item: any) => any
2121
renderNode: (item: any, level: number) => any
2222
renderChildrenNode?: (item: any) => any
23+
extraData?: any
2324
}
2425

2526
export interface IState {
2627
node: INode
28+
extraData?: any
2729
}
2830

2931
export default class NodeView extends React.PureComponent<IProps, IState> {
@@ -66,6 +68,7 @@ export default class NodeView extends React.PureComponent<IProps, IState> {
6668
getChildrenName={this.props.getChildrenName}
6769
node={item}
6870
level={level + 1}
71+
extraData={this.props.extraData}
6972
onNodePressed={this.props.onNodePressed}
7073
renderNode={this.props.renderNode}
7174
/>
@@ -92,6 +95,7 @@ export default class NodeView extends React.PureComponent<IProps, IState> {
9295
<FlatList
9396
data={rootChildren}
9497
renderItem={this.renderItem}
98+
extraData={this.props.extraData}
9599
keyExtractor={(item: INode) => item._internalId}
96100
/>
97101
) : null}

src/__snapshots__/NestedListView.test.tsx.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exports[`NestedListView renders with NestedRow 1`] = `
2020
]
2121
}
2222
disableVirtualization={false}
23+
extraData={undefined}
2324
getItem={[Function]}
2425
getItemCount={[Function]}
2526
horizontal={false}
@@ -217,6 +218,7 @@ exports[`NestedListView renders with children as objects 1`] = `
217218
]
218219
}
219220
disableVirtualization={false}
221+
extraData={undefined}
220222
getItem={[Function]}
221223
getItemCount={[Function]}
222224
horizontal={false}
@@ -315,6 +317,7 @@ exports[`NestedListView renders with nested arrays 1`] = `
315317
]
316318
}
317319
disableVirtualization={false}
320+
extraData={undefined}
318321
getItem={[Function]}
319322
getItemCount={[Function]}
320323
horizontal={false}
@@ -475,6 +478,7 @@ exports[`NestedListView renders with nested arrays and children with different n
475478
]
476479
}
477480
disableVirtualization={false}
481+
extraData={undefined}
478482
getItem={[Function]}
479483
getItemCount={[Function]}
480484
horizontal={false}
@@ -634,6 +638,7 @@ exports[`NestedListView renders with nested arrays and children with different t
634638
]
635639
}
636640
disableVirtualization={false}
641+
extraData={undefined}
637642
getItem={[Function]}
638643
getItemCount={[Function]}
639644
horizontal={false}
@@ -778,6 +783,7 @@ exports[`NestedListView renders with onNodePressed 1`] = `
778783
]
779784
}
780785
disableVirtualization={false}
786+
extraData={undefined}
781787
getItem={[Function]}
782788
getItemCount={[Function]}
783789
horizontal={false}
@@ -922,6 +928,7 @@ exports[`NestedListView renders with simple array 1`] = `
922928
]
923929
}
924930
disableVirtualization={false}
931+
extraData={undefined}
925932
getItem={[Function]}
926933
getItemCount={[Function]}
927934
horizontal={false}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"preserveConstEnums": true,
1212
"sourceMap": true,
1313
"strictNullChecks": true,
14-
"declaration": true
14+
"declaration": true,
15+
"skipLibCheck": true
1516
},
1617
"filesGlob": ["src/**/*.ts", "src/**/*.tsx"],
1718
"exclude": ["dist/", "node_modules"],

0 commit comments

Comments
 (0)