@@ -97,7 +97,7 @@ class FilterExample extends React.Component {
9797 ] ,
9898 filterText : '' ,
9999 nodesFiltered : nodes ,
100- nodes
100+ nodes,
101101 } ;
102102
103103 this . onCheck = this . onCheck . bind ( this ) ;
@@ -114,37 +114,41 @@ class FilterExample extends React.Component {
114114 onExpand ( expanded ) {
115115 this . setState ( { expanded } ) ;
116116 }
117-
118- nodeFilter ( node : Node ) {
119- const children = ( node . children || [ ] ) . map ( this . nodeFilter ) . filter ( c => c !== null ) ;
120117
121- return node . label . indexOf ( this . state . filterText ) !== - 1 || children . length ? { ...node , children : children } : null ;
118+ setFilterText ( e ) {
119+ this . setState ( { filterText : e . target . value } , this . filterTree ) ;
122120 }
123121
124- filterTree ( ) {
122+ filterTree ( ) {
125123 if ( ! this . state . filterText ) {
126- return this . setState ( {
127- nodesFiltered : this . state . nodes
128- } ) ;
124+ this . setState ( prevState => ( {
125+ nodesFiltered : prevState . nodes ,
126+ } ) ) ;
127+
128+ return ;
129129 }
130130
131- const nodesFiltered = this . state . nodes . map ( this . nodeFilter ) . filter ( n => n !== null ) ;
131+ const nodesFiltered = prevState => (
132+ { nodesFiltered : prevState . nodes . map ( this . nodeFilter ) . filter ( n => n !== null ) }
133+ ) ;
132134
133- this . setState ( {
134- nodesFiltered : nodesFiltered
135- } ) ;
135+ this . setState ( nodesFiltered ) ;
136136 }
137-
138- setFilterText ( e ) {
139- this . setState ( { filterText : e . target . value } , this . filterTree ) ;
137+
138+ nodeFilter ( node ) {
139+ const children = ( node . children || [ ] ) . map ( this . nodeFilter ) . filter ( c => c !== null ) ;
140+
141+ return node . label . indexOf ( this . state . filterText ) !== - 1 || children . length ?
142+ { ...node , children } :
143+ null ;
140144 }
141145
142146 render ( ) {
143147 const { checked, expanded } = this . state ;
144148
145149 return (
146150 < div >
147- < input type = 'text' placeholder = 'Search' value = { this . state . filterText } onChange = { this . setFilterText } />
151+ < input type = 'text' placeholder = 'Search' value = { this . state . filterText } onChange = { this . setFilterText } />
148152 < CheckboxTree
149153 checked = { checked }
150154 expanded = { expanded }
0 commit comments