Skip to content

Commit ccaf071

Browse files
committed
focus node and selected node fixes
1 parent 79156b9 commit ccaf071

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

components/tree/__tests__/tree.browser-test.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import createReactClass from 'create-react-class';
88
import PropTypes from 'prop-types';
99

1010
import isFunction from 'lodash.isfunction';
11+
import cloneDeep from 'lodash.clonedeep';
1112

1213
import chai, { expect } from 'chai';
1314
import chaiEnzyme from 'chai-enzyme';
@@ -60,7 +61,7 @@ const DemoTree = createReactClass({
6061
? sampleNodes[this.props.exampleNodesIndex]
6162
: sampleNodes.sampleNodesDefault;
6263
return {
63-
nodes: initalNodes,
64+
nodes: cloneDeep(initalNodes),
6465
searchTerm: this.props.searchable ? 'fruit' : undefined,
6566
};
6667
},

components/tree/index.jsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import PropTypes from 'prop-types';
1414
// ### classNames
1515
import classNames from 'classnames';
1616

17+
import find from 'lodash.find';
18+
1719
// Child components
1820
import Branch from './private/branch';
1921

@@ -29,15 +31,27 @@ import { TREE } from '../../utilities/constants';
2931
class Tree extends React.Component {
3032
constructor (props) {
3133
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+
}
3248
this.handleSelect = this.handleSelect.bind(this);
3349
this.handleNodeBlur = this.handleNodeBlur.bind(this);
3450
this.handleExpand = this.handleExpand.bind(this);
3551
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,
4155
};
4256
}
4357

components/tree/private/branch.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { TREE_BRANCH } from '../../../utilities/constants';
3838

3939
const handleExpand = (event, props) => {
4040
EventUtil.trap(event);
41-
4241
if (isFunction(props.onExpand)) {
4342
props.onExpand(event, {
4443
node: props.node,
@@ -173,7 +172,7 @@ const handleKeyDown = (event, props) => {
173172
};
174173

175174
const handleFocus = (event, props) => {
176-
if (!props.focusedNodeIndex) {
175+
if (!props.focusedNodeIndex && event.target === event.currentTarget) {
177176
handleSelect(event, props);
178177
}
179178
};

components/tree/private/item.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const handleKeyDown = (event, props) => {
130130
};
131131

132132
const handleFocus = (event, props) => {
133-
if (!props.focusedNodeIndex) {
133+
if (!props.focusedNodeIndex && event.target === event.currentTarget) {
134134
handleSelect(event, props);
135135
}
136136
};

0 commit comments

Comments
 (0)