Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit f1eece0

Browse files
committed
Add onActivate prop called on Enter key press.
1 parent 336f4be commit f1eece0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/devtools-components/src/tests/tree.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,42 @@ describe("Tree", () => {
399399
expect(onFocus.mock.calls[4][0]).toBe("E");
400400
});
401401

402+
it("calls onActivate when expected", () => {
403+
const onActivate = jest.fn();
404+
405+
const wrapper = mountTree({
406+
expanded: new Set("ABCDEFGHIJKLMNO".split("")),
407+
focused: "A",
408+
onActivate,
409+
});
410+
411+
simulateKeyDown(wrapper, "Enter");
412+
expect(onActivate.mock.calls.length).toBe(1);
413+
expect(onActivate.mock.calls[0][0]).toBe("A");
414+
415+
simulateKeyDown(wrapper, "Enter");
416+
expect(onActivate.mock.calls.length).toBe(2);
417+
expect(onActivate.mock.calls[1][0]).toBe("A");
418+
419+
simulateKeyDown(wrapper, "ArrowDown");
420+
simulateKeyDown(wrapper, "Enter");
421+
expect(onActivate.mock.calls.length).toBe(3);
422+
expect(onActivate.mock.calls[2][0]).toBe("B");
423+
424+
wrapper.simulate("blur");
425+
simulateKeyDown(wrapper, "Enter");
426+
expect(onActivate.mock.calls.length).toBe(3);
427+
});
428+
429+
it("does not throw when onActivate is undefined and Enter is pressed", () => {
430+
const wrapper = mountTree({
431+
expanded: new Set("ABCDEFGHIJKLMNO".split("")),
432+
focused: "A",
433+
});
434+
435+
simulateKeyDown(wrapper, "Enter");
436+
});
437+
402438
it("ignores key strokes when pressing modifiers", () => {
403439
const wrapper = mountTree({
404440
expanded: new Set("ABCDEFGHIJKLMNO".split("")),

packages/devtools-components/src/tree.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ class Tree extends Component {
330330
// onExpand: item => dispatchExpandActionToRedux(item)
331331
onExpand: PropTypes.func,
332332
onCollapse: PropTypes.func,
333+
// Optional event handler called with the current focused node when the Enter key
334+
// is pressed. Can be useful to allow further keyboard actions within the tree node.
335+
onActivate: PropTypes.func,
333336
isExpandable: PropTypes.func,
334337
// Additional classes to add to the root element.
335338
className: PropTypes.string,
@@ -369,6 +372,7 @@ class Tree extends Component {
369372
this._onBlur = this._onBlur.bind(this);
370373
this._onKeyDown = this._onKeyDown.bind(this);
371374
this._nodeIsExpandable = this._nodeIsExpandable.bind(this);
375+
this._activateNode = oncePerAnimationFrame(this._activateNode).bind(this);
372376
}
373377

374378
componentDidMount() {
@@ -639,6 +643,10 @@ class Tree extends Component {
639643

640644
case "End":
641645
this._focusLastNode();
646+
return;
647+
648+
case "Enter":
649+
this._activateNode();
642650
}
643651
}
644652

@@ -726,6 +734,12 @@ class Tree extends Component {
726734
this._focus(traversal[lastIndex].item, {alignTo: "bottom"});
727735
}
728736

737+
_activateNode() {
738+
if (this.props.onActivate) {
739+
this.props.onActivate(this.props.focused);
740+
}
741+
}
742+
729743
_nodeIsExpandable(item) {
730744
return this.props.isExpandable
731745
? this.props.isExpandable(item)

0 commit comments

Comments
 (0)