Skip to content

Commit 163c741

Browse files
author
Javier Morant
authored
Customise the id generated per node (#75)
* Added new prop to get the node id name * Use internal id private * Updated snapshot tests * Removed unnecessary prop
1 parent 4c1bc64 commit 163c741

File tree

5 files changed

+1747
-96
lines changed

5 files changed

+1747
-96
lines changed

src/NestedListView.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ describe('NestedListView', () => {
5050
.toJSON()
5151
expect(nestedListView).toMatchSnapshot()
5252
})
53-
5453
test('renders with nested arrays and children with different name', () => {
5554
const data = [
5655
{

src/NestedListView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default class NestedListView extends React.PureComponent<
6565
)
6666
}
6767

68-
node.id = shortid.generate()
68+
node._internalId = shortid.generate()
6969

7070
return node
7171
}
@@ -117,7 +117,7 @@ export default class NestedListView extends React.PureComponent<
117117

118118
private generateRootNode = (props: any): INode => {
119119
return {
120-
id: shortid.generate(),
120+
_internalId: shortid.generate(),
121121
items: props.data
122122
? props.data.map((_: INode, index: number) =>
123123
this.generateIds(props.data[index])

src/NodeView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as React from 'react'
55
import {FlatList, TouchableWithoutFeedback, View} from 'react-native'
66

77
export interface INode {
8-
id: string
8+
_internalId: string
99
hidden: boolean
1010
opened: boolean
1111
[key: string]: any
@@ -92,7 +92,7 @@ export default class NodeView extends React.PureComponent<IProps, IState> {
9292
<FlatList
9393
data={rootChildren}
9494
renderItem={this.renderItem}
95-
keyExtractor={(item: INode) => item.id}
95+
keyExtractor={(item: INode) => item._internalId}
9696
/>
9797
) : null}
9898
</View>

src/__snapshots__/NestedListView.test.tsx.snap

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ exports[`NestedListView renders with NestedRow 1`] = `
66
data={
77
Array [
88
Object {
9-
"id": "1",
9+
"_internalId": "1",
1010
"title": "child1",
1111
},
1212
Object {
13-
"id": "1",
13+
"_internalId": "1",
1414
"title": "child2",
1515
},
1616
Object {
17-
"id": "1",
17+
"_internalId": "1",
1818
"title": "child3",
1919
},
2020
]
@@ -171,47 +171,47 @@ exports[`NestedListView renders with children as objects 1`] = `
171171
data={
172172
Array [
173173
Object {
174+
"_internalId": "1",
174175
"children": Array [
175176
Object {
177+
"_internalId": "1",
176178
"children": Array [
177179
Object {
180+
"_internalId": "1",
178181
"children": Array [],
179-
"id": "1",
180182
"name": "Sub Child 1",
181183
},
182184
Object {
185+
"_internalId": "1",
183186
"children": Array [
184187
Object {
188+
"_internalId": "1",
185189
"children": Array [],
186-
"id": "1",
187190
"name": "Sample",
188191
},
189192
],
190-
"id": "1",
191193
"name": "Sub Child 2",
192194
},
193195
],
194-
"id": "1",
195196
"name": "Main Child 1",
196197
},
197198
Object {
199+
"_internalId": "1",
198200
"children": Array [
199201
Object {
202+
"_internalId": "1",
200203
"children": Array [],
201-
"id": "1",
202204
"name": "Sub Child 1",
203205
},
204206
Object {
207+
"_internalId": "1",
205208
"children": Array [],
206-
"id": "1",
207209
"name": "Sub Child 2",
208210
},
209211
],
210-
"id": "1",
211212
"name": "Main Child 2",
212213
},
213214
],
214-
"id": "1",
215215
"name": "Main Parent",
216216
},
217217
]
@@ -285,31 +285,31 @@ exports[`NestedListView renders with nested arrays 1`] = `
285285
data={
286286
Array [
287287
Object {
288-
"id": "1",
288+
"_internalId": "1",
289289
"items": Array [
290290
Object {
291-
"id": "1",
291+
"_internalId": "1",
292292
"name": "subchild 1.1",
293293
},
294294
Object {
295-
"id": "1",
295+
"_internalId": "1",
296296
"name": "subchild 1.2",
297297
},
298298
],
299299
"title": "child1",
300300
},
301301
Object {
302-
"id": "1",
302+
"_internalId": "1",
303303
"items": Array [
304304
Object {
305-
"id": "1",
305+
"_internalId": "1",
306306
"key": "subchild 2.1",
307307
},
308308
],
309309
"title": "child2",
310310
},
311311
Object {
312-
"id": "1",
312+
"_internalId": "1",
313313
"title": "child3",
314314
},
315315
]
@@ -445,31 +445,31 @@ exports[`NestedListView renders with nested arrays and children with different n
445445
data={
446446
Array [
447447
Object {
448-
"id": "1",
448+
"_internalId": "1",
449449
"items": Array [
450450
Object {
451-
"id": "1",
451+
"_internalId": "1",
452452
"name": "subchild 1.1",
453453
},
454454
Object {
455-
"id": "1",
455+
"_internalId": "1",
456456
"name": "subchild 1.2",
457457
},
458458
],
459459
"title": "child1",
460460
},
461461
Object {
462+
"_internalId": "1",
462463
"descendants": Array [
463464
Object {
464-
"id": "1",
465+
"_internalId": "1",
465466
"key": "subchild 2.1",
466467
},
467468
],
468-
"id": "1",
469469
"title": "child2",
470470
},
471471
Object {
472-
"id": "1",
472+
"_internalId": "1",
473473
"title": "child3",
474474
},
475475
]
@@ -605,30 +605,30 @@ exports[`NestedListView renders with nested arrays and children with different t
605605
data={
606606
Array [
607607
Object {
608-
"id": "1",
608+
"_internalId": "1",
609609
"items": Array [
610610
Object {
611-
"id": "1",
611+
"_internalId": "1",
612612
"name": "subchild 1.1",
613613
},
614614
Object {
615-
"id": "1",
615+
"_internalId": "1",
616616
"name": "subchild 1.2",
617617
},
618618
],
619619
"title": "child1",
620620
},
621621
Object {
622+
"_internalId": "1",
622623
"descendants": Array [
623624
Object {
624625
"key": "subchild 2.1",
625626
},
626627
],
627-
"id": "1",
628628
"title": "child2",
629629
},
630630
Object {
631-
"id": "1",
631+
"_internalId": "1",
632632
"title": "child3",
633633
},
634634
]
@@ -764,15 +764,15 @@ exports[`NestedListView renders with onNodePressed 1`] = `
764764
data={
765765
Array [
766766
Object {
767-
"id": "1",
767+
"_internalId": "1",
768768
"title": "child1",
769769
},
770770
Object {
771-
"id": "1",
771+
"_internalId": "1",
772772
"title": "child2",
773773
},
774774
Object {
775-
"id": "1",
775+
"_internalId": "1",
776776
"title": "child3",
777777
},
778778
]
@@ -908,15 +908,15 @@ exports[`NestedListView renders with simple array 1`] = `
908908
data={
909909
Array [
910910
Object {
911-
"id": "1",
911+
"_internalId": "1",
912912
"title": "child1",
913913
},
914914
Object {
915-
"id": "1",
915+
"_internalId": "1",
916916
"title": "child2",
917917
},
918918
Object {
919-
"id": "1",
919+
"_internalId": "1",
920920
"title": "child3",
921921
},
922922
]

0 commit comments

Comments
 (0)