@@ -16,7 +16,7 @@ export default class Tree extends React.Component {
16
16
data : this . assignInternalProperties ( clone ( this . props . data ) ) ,
17
17
zoom : undefined ,
18
18
} ;
19
- this . findTargetNode = this . findTargetNode . bind ( this ) ;
19
+ this . findNodesById = this . findNodesById . bind ( this ) ;
20
20
this . collapseNode = this . collapseNode . bind ( this ) ;
21
21
this . handleNodeToggle = this . handleNodeToggle . bind ( this ) ;
22
22
}
@@ -100,29 +100,31 @@ export default class Tree extends React.Component {
100
100
101
101
102
102
/**
103
- * findTargetNode - Recursively walks a set of nodes (`nodeSet`) and its
104
- * children until a `node.id` that matches `nodeId` param is found.
103
+ * findNodesById - Description
105
104
*
106
- * @param {string } nodeId The `node.id` being searched for
105
+ * @param {string } nodeId The `node.id` being searched for
107
106
* @param {array } nodeSet Array of `node` objects
107
+ * @param {array } hits Accumulator for matches, passed between recursive calls
108
108
*
109
- * @return {object } Returns the targeted `node` object
109
+ * @return {array } Set of nodes matching `nodeId`
110
110
*/
111
111
// TODO Refactor this into a more readable/reasonable recursive depth-first walk.
112
- findTargetNode ( nodeId , nodeSet ) {
113
- const hits = nodeSet . filter ( ( node ) => node . id === nodeId ) ;
114
-
112
+ findNodesById ( nodeId , nodeSet , hits ) {
115
113
if ( hits . length > 0 ) {
116
- const targetNode = hits [ 0 ] ;
117
- return targetNode ;
114
+ return hits ;
118
115
}
119
116
120
- return nodeSet . map ( ( node ) => {
117
+ hits = hits . concat ( nodeSet . filter ( ( node ) => node . id === nodeId ) ) ;
118
+
119
+ nodeSet . forEach ( ( node ) => {
121
120
if ( node . _children && node . _children . length > 0 ) {
122
- return this . findTargetNode ( nodeId , node . _children ) ;
121
+ hits = this . findNodesById ( nodeId , node . _children , hits ) ;
122
+ return hits ;
123
123
}
124
- return null ;
125
- } ) [ 0 ] ;
124
+ return hits ;
125
+ } ) ;
126
+
127
+ return hits ;
126
128
}
127
129
128
130
@@ -169,7 +171,8 @@ export default class Tree extends React.Component {
169
171
handleNodeToggle ( nodeId ) {
170
172
if ( this . props . collapsible ) {
171
173
const data = clone ( this . state . data ) ;
172
- const targetNode = this . findTargetNode ( nodeId , data ) ;
174
+ const matches = this . findNodesById ( nodeId , data , [ ] ) ;
175
+ const targetNode = matches [ 0 ] ;
173
176
targetNode . _collapsed
174
177
? this . expandNode ( targetNode )
175
178
: this . collapseNode ( targetNode ) ;
0 commit comments