File tree Expand file tree Collapse file tree 4 files changed +23
-9
lines changed Expand file tree Collapse file tree 4 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import createReactClass from 'create-react-class';
8
8
import PropTypes from 'prop-types' ;
9
9
10
10
import isFunction from 'lodash.isfunction' ;
11
+ import cloneDeep from 'lodash.clonedeep' ;
11
12
12
13
import chai , { expect } from 'chai' ;
13
14
import chaiEnzyme from 'chai-enzyme' ;
@@ -60,7 +61,7 @@ const DemoTree = createReactClass({
60
61
? sampleNodes [ this . props . exampleNodesIndex ]
61
62
: sampleNodes . sampleNodesDefault ;
62
63
return {
63
- nodes : initalNodes ,
64
+ nodes : cloneDeep ( initalNodes ) ,
64
65
searchTerm : this . props . searchable ? 'fruit' : undefined ,
65
66
} ;
66
67
} ,
Original file line number Diff line number Diff line change @@ -14,6 +14,8 @@ import PropTypes from 'prop-types';
14
14
// ### classNames
15
15
import classNames from 'classnames' ;
16
16
17
+ import find from 'lodash.find' ;
18
+
17
19
// Child components
18
20
import Branch from './private/branch' ;
19
21
@@ -29,15 +31,27 @@ import { TREE } from '../../utilities/constants';
29
31
class Tree extends React . Component {
30
32
constructor ( props ) {
31
33
super ( props ) ;
34
+ const flattenedNodes = this . flattenTree ( {
35
+ nodes : this . props . nodes ,
36
+ expanded : true ,
37
+ } ) . slice ( 1 ) ;
38
+ const selectedNode = find (
39
+ flattenedNodes ,
40
+ ( curNode ) => curNode . node . selected
41
+ ) ;
42
+ const selectedNodeIndexes = [ ] ;
43
+ let focusedNodeIndex ;
44
+ if ( selectedNode ) {
45
+ selectedNodeIndexes . push ( selectedNode . treeIndex ) ;
46
+ focusedNodeIndex = selectedNode . treeIndex ;
47
+ }
32
48
this . handleSelect = this . handleSelect . bind ( this ) ;
33
49
this . handleNodeBlur = this . handleNodeBlur . bind ( this ) ;
34
50
this . handleExpand = this . handleExpand . bind ( this ) ;
35
51
this . state = {
36
- flattenedNodes : this . flattenTree ( {
37
- nodes : this . props . nodes ,
38
- expanded : true ,
39
- } ) . slice ( 1 ) ,
40
- selectedNodeIndexes : [ ] ,
52
+ flattenedNodes,
53
+ selectedNodeIndexes,
54
+ focusedNodeIndex,
41
55
} ;
42
56
}
43
57
Original file line number Diff line number Diff line change @@ -38,7 +38,6 @@ import { TREE_BRANCH } from '../../../utilities/constants';
38
38
39
39
const handleExpand = ( event , props ) => {
40
40
EventUtil . trap ( event ) ;
41
-
42
41
if ( isFunction ( props . onExpand ) ) {
43
42
props . onExpand ( event , {
44
43
node : props . node ,
@@ -173,7 +172,7 @@ const handleKeyDown = (event, props) => {
173
172
} ;
174
173
175
174
const handleFocus = ( event , props ) => {
176
- if ( ! props . focusedNodeIndex ) {
175
+ if ( ! props . focusedNodeIndex && event . target === event . currentTarget ) {
177
176
handleSelect ( event , props ) ;
178
177
}
179
178
} ;
Original file line number Diff line number Diff line change @@ -130,7 +130,7 @@ const handleKeyDown = (event, props) => {
130
130
} ;
131
131
132
132
const handleFocus = ( event , props ) => {
133
- if ( ! props . focusedNodeIndex ) {
133
+ if ( ! props . focusedNodeIndex && event . target === event . currentTarget ) {
134
134
handleSelect ( event , props ) ;
135
135
}
136
136
} ;
You can’t perform that action at this time.
0 commit comments