@@ -5,117 +5,46 @@ import {ScrollView, FlatList} from 'react-native'
55import NodeView from './NodeView'
66import shortid from 'shortid'
77
8- export default class NestedListView extends React . Component {
8+ export default class NestedListView extends React . PureComponent {
99 props : {
1010 data : any ,
1111 renderNode : Function ,
1212 onNodePressed : Function ,
1313 getChildrenName : Function ,
14- autoScrollToNodeId : ?string ,
1514 }
1615
17- scrollview: any
18-
1916 state = {
2017 data : { } ,
2118 }
2219
2320 componentWillMount = ( ) => {
24- let rootChildren = this . props . data
25-
26-
27- if ( rootChildren ) {
28- rootChildren = rootChildren . map ( ( child , index ) => {
29- return this . generateIds ( rootChildren [ index ] )
21+ const rootChildren = this . props . data
22+ if ( rootChildren ) {
23+ this . setState ( {
24+ data : rootChildren . map ( ( child , index ) =>
25+ this . generateIds ( rootChildren [ index ] )
26+ ) ,
3027 } )
31- }
32-
33- console . log ( 'rootChildren: ' , rootChildren )
34-
35- this . setState ( { data : rootChildren } )
36- }
37-
38- contractAllNodesInTree = ( ) => {
39- this . setState ( { data : this . compactNodeInTree ( this . state . data ) } )
40- }
41-
42- expandNode = ( node : any ) => {
43- this . setState ( { data : this . expandNodeInTree ( this . state . data , node ) } )
44- }
45-
46- compactNodeInTree = ( element : any ) => {
47- const childrenName = this . props . getChildrenName ( element )
48-
49- if ( childrenName && element . opened ) {
50- const children = element [ childrenName ]
51-
52- if ( children ) {
53- for ( i = 0 ; i < children . length ; i ++ ) {
54- element [ childrenName ] [ i ] = this . compactNodeInTree ( children [ i ] )
55- }
56- }
57-
58- element . opened = false
59-
60- return element
6128 }
62-
63- element . opened = false
64-
65- return element
66- }
67-
68- expandNodeInTree = ( element : any , otherElement : any ) => {
69- const childrenName = this . props . getChildrenName ( element )
70-
71- if ( otherElement && element . id == otherElement . id ) {
72- element . opened = true
73-
74- return element
75- } else if ( childrenName ) {
76- const children = element [ childrenName ]
77-
78- if ( children ) {
79- var anyChildrenHasOpened = false
80- for ( i = 0 ; i < children . length ; i ++ ) {
81- element [ childrenName ] [ i ] = this . expandNodeInTree (
82- children [ i ] ,
83- otherElement ,
84- )
85-
86- if ( element [ childrenName ] [ i ] . opened ) {
87- anyChildrenHasOpened = true
88- }
89- }
90-
91- if ( anyChildrenHasOpened ) {
92- element . opened = true
93- }
94- }
95-
96- return element
97- }
98-
99- return element
10029 }
10130
10231 searchTree = ( element : any , otherElement : any ) => {
10332 if ( element . id === otherElement . id ) {
10433 element . opened = ! element . opened
10534
10635 return element
107- }
36+ }
10837
10938 const childrenName = this . props . getChildrenName ( element )
110-
39+
11140 if ( childrenName ) {
11241 const children = element [ childrenName ]
11342
114- if ( children ) {
115- for ( i = 0 ; i < children . length ; i ++ ) {
116- element [ childrenName ] [ i ] = this . searchTree ( children [ i ] , otherElement )
117- }
118- }
43+ if ( children ) {
44+ element [ childrenName ] = children . map ( ( child , index ) =>
45+ this . searchTree ( children [ index ] , otherElement )
46+ )
47+ }
11948
12049 return element
12150 }
@@ -133,11 +62,11 @@ export default class NestedListView extends React.Component {
13362 if ( childrenName ) {
13463 const children = element [ childrenName ]
13564
136- if ( children ) {
137- for ( i = 0 ; i < children . length ; i ++ ) {
138- element [ childrenName ] [ i ] = this . generateIds ( children [ i ] )
139- }
140- }
65+ if ( children ) {
66+ element [ childrenName ] = children . map ( ( child , index ) =>
67+ this . generateIds ( children [ index ] )
68+ )
69+ }
14170 }
14271
14372 element . id = shortid . generate ( )
@@ -146,11 +75,11 @@ export default class NestedListView extends React.Component {
14675 }
14776
14877 onNodePressed = ( node : any ) => {
149- const newState = rootChildren = this . state . data . map ( ( child , index ) => {
150- return this . searchTree ( this . state . data [ index ] , node )
151- } )
78+ const rootChildren = this . state . data . map ( ( child , index ) =>
79+ this . searchTree ( this . state . data [ index ] , node )
80+ )
15281
153- this . setState ( { data : newState } )
82+ this . setState ( { data : rootChildren } )
15483 this . props . onNodePressed ( node )
15584 }
15685
@@ -163,7 +92,8 @@ export default class NestedListView extends React.Component {
16392 searchTree = { this . searchTree }
16493 generateIds = { this . generateIds }
16594 onNodePressed = { ( ) => this . onNodePressed ( item ) }
166- renderChildrenNode = { ( childrenNode : Object ) => this . onCreateChildren ( childrenNode , level + 1 ) }
95+ renderChildrenNode = { ( childrenNode : Object ) =>
96+ this . onCreateChildren ( childrenNode , level + 1 ) }
16797 renderNode = { ( ) => this . props . renderNode ( item , level ) }
16898 />
16999 )
@@ -172,10 +102,11 @@ export default class NestedListView extends React.Component {
172102 render = ( ) => {
173103 return (
174104 < FlatList
175- data = { this . state . data }
176- style = { this . props . style }
105+ data = { this . state . data }
106+ style = { this . props . style }
177107 renderItem = { ( { item} ) => this . onCreateChildren ( item , 0 ) }
178- keyExtractor = { ( item ) => item . id } />
108+ keyExtractor = { item => item . id }
109+ />
179110 )
180111 }
181112}
0 commit comments