11/* @flow */
22
33import React from 'react'
4- import { FlatList , type Props , type State } from 'react-native'
4+ import { FlatList , type Props , type State , View } from 'react-native'
55import NodeView from './NodeView'
66import shortid from 'shortid'
77
@@ -14,47 +14,18 @@ export default class NestedListView extends React.PureComponent<Props, State> {
1414 style : any ,
1515 }
1616
17- state : {
18- childrenNodes : Array < any > ,
19- }
20-
21- state = {
22- childrenNodes : [ ] ,
23- }
24-
2517 componentWillMount = ( ) => {
26- const rootChildren = this . props . data
27- if ( rootChildren ) {
28- this . setState ( {
29- childrenNodes : rootChildren . map ( ( child , index ) =>
30- this . generateIds ( rootChildren [ index ] )
31- ) ,
32- } )
33- }
34- }
35-
36- searchTree = ( element : any , otherElement : any ) => {
37- if ( element . id === otherElement . id ) {
38- element . opened = ! element . opened
39-
40- return element
18+ const root = {
19+ id : 1 ,
20+ items : this . props . data . map ( ( child , index ) =>
21+ this . generateIds ( this . props . data [ index ] )
22+ ) ,
23+ name : 'root' ,
24+ opened : true ,
25+ hidden : true ,
4126 }
4227
43- const childrenName = this . props . getChildrenName ( element )
44-
45- if ( childrenName ) {
46- const children = element [ childrenName ]
47-
48- if ( children ) {
49- element [ childrenName ] = children . map ( ( child , index ) =>
50- this . searchTree ( children [ index ] , otherElement )
51- )
52- }
53-
54- return element
55- }
56-
57- return element
28+ this . setState ( { root} )
5829 }
5930
6031 generateIds = ( element : any ) = > {
@@ -79,39 +50,25 @@ export default class NestedListView extends React.PureComponent<Props, State> {
7950 return element
8051 }
8152
82- onNodePressed = ( node : any ) => {
83- const childrenNodes = this . state . childrenNodes . map ( ( child , index ) =>
84- this . searchTree ( this . state . childrenNodes [ index ] , node )
85- )
86-
87- this . setState ( { childrenNodes} )
88- this . props . onNodePressed ( node )
89- }
53+ getChildrenName = node => {
54+ if ( node . name === 'root' ) {
55+ return 'items'
56+ }
9057
91- onCreateChildren = ( item : any , level : number ) => {
92- return (
93- < NodeView
94- getChildren = { ( node : Object ) => node [ this . props . getChildrenName ( node ) ] }
95- key = { item . id }
96- node = { item }
97- searchTree = { this . searchTree }
98- generateIds = { this . generateIds }
99- onNodePressed = { ( ) => this . onNodePressed ( item ) }
100- renderChildrenNode = { ( childrenNode : Object ) =>
101- this . onCreateChildren ( childrenNode , level + 1 ) }
102- renderNode = { ( ) => this . props . renderNode ( item , level ) }
103- />
104- )
58+ return this . props . getChildrenName ( node )
10559 }
10660
10761 render = ( ) => {
10862 return (
109- < FlatList
110- data = { this . state . childrenNodes }
111- style = { this . props . style }
112- renderItem = { ( { item} ) => this . onCreateChildren ( item , 0 ) }
113- keyExtractor = { item => item . id }
114- />
63+ < View style = { this . props . style } >
64+ < NodeView
65+ getChildrenName = { this . getChildrenName }
66+ node = { this . state . root }
67+ generateIds = { this . generateIds }
68+ level = { 0 }
69+ renderNode = { ( item , level ) => this . props . renderNode ( item , level ) }
70+ />
71+ </ View >
11572 )
11673 }
11774}
0 commit comments