From 655740d1a11be6943a30b7b400d5d08ec51b6d0d Mon Sep 17 00:00:00 2001 From: xin xu Date: Wed, 29 Nov 2017 11:40:31 +0800 Subject: [PATCH] fix issue 168: Menu doesn't close when press 'Enter' There are two cases: 1. Given the menu is open and the selected item is disabled, When I press Enter, the menu doesn't close, the expected behavior is that the menu should close 2. Given the menu is open and no item is selected, When I press Enter, the menu doesn't close, the expected behavior is that the menu should close --- src/AbstractMenu.js | 6 +++++- src/ContextMenu.js | 2 +- tests/ContextMenu.test.js | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/AbstractMenu.js b/src/AbstractMenu.js index 43a5ae62..d26d3517 100644 --- a/src/AbstractMenu.js +++ b/src/AbstractMenu.js @@ -45,8 +45,12 @@ export default class AbstractMenu extends Component { case 13: // enter e.preventDefault(); this.tryToOpenSubMenu(e); - if (this.seletedItemRef && this.seletedItemRef.ref instanceof HTMLElement) { + if (this.seletedItemRef && + this.seletedItemRef.ref instanceof HTMLElement && + !this.seletedItemRef.ref.props.disabled) { this.seletedItemRef.ref.click(); + } else { + this.hideMenu(e); } break; default: diff --git a/src/ContextMenu.js b/src/ContextMenu.js index d0f319dd..b411868b 100644 --- a/src/ContextMenu.js +++ b/src/ContextMenu.js @@ -143,7 +143,7 @@ export default class ContextMenu extends AbstractMenu { } hideMenu = (e) => { - if (e.keyCode === 27) { // enter + if (e.keyCode === 27 || e.keyCode === 13) { // ECS or enter hideMenu(); } } diff --git a/tests/ContextMenu.test.js b/tests/ContextMenu.test.js index 86881414..3ffd7a15 100644 --- a/tests/ContextMenu.test.js +++ b/tests/ContextMenu.test.js @@ -115,6 +115,30 @@ describe('ContextMenu tests', () => { component.unmount(); }); + test('menu should close on "Enter" when selectedItem is null', () => { + const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' }; + const onHide = jest.fn(); + const component = mount(); + const enter = new window.KeyboardEvent('keydown', { keyCode: 13 }); + + showMenu(data); + expect(component.state()).toEqual( + Object.assign( + { isVisible: true, forceSubMenuOpen: false, selectedItem: null }, + data.position + ) + ); + document.dispatchEvent(enter); + expect(component.state()).toEqual( + Object.assign( + { isVisible: false, forceSubMenuOpen: false, selectedItem: null }, + data.position + ) + ); + expect(onHide).toHaveBeenCalled(); + component.unmount(); + }); + test('menu should close on "outside" click', () => { const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' }; const onHide = jest.fn();