From 05d7aa4631f8da59d992e4264768d79c22813ea7 Mon Sep 17 00:00:00 2001 From: Kermit Date: Mon, 18 Jan 2021 18:03:01 +0800 Subject: [PATCH 1/2] fix: should stopPropagation when pressing esc --- src/OptionList.tsx | 1 + tests/Select.test.tsx | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index d2f7738ab..5f164f5d3 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -218,6 +218,7 @@ const OptionList: React.RefForwardingComponent< // >>> Close case KeyCode.ESC: { onToggleOpen(false); + event.stopPropagation(); } } }, diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index 99a20bb84..d16e303ef 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -666,7 +666,12 @@ describe('Select.Basic', () => { }); it('close on ESC', () => { - const wrapper = mount( + , + ); toggleOpen(wrapper); wrapper .find('input') @@ -677,6 +682,7 @@ describe('Select.Basic', () => { expect(wrapper.find('input').props().value).toBe(''); expectOpen(wrapper, false); + expect(onKeyDown).not.toHaveBeenCalled(); }); it('close after select', () => { From c0aca65ce2a75d42c78cccd5d0a6fdef3defccb1 Mon Sep 17 00:00:00 2001 From: Kermit Date: Tue, 19 Jan 2021 14:01:45 +0800 Subject: [PATCH 2/2] chore: optimize logic --- src/OptionList.tsx | 4 +++- tests/Select.test.tsx | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 5f164f5d3..bfc2a9f9c 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -218,7 +218,9 @@ const OptionList: React.RefForwardingComponent< // >>> Close case KeyCode.ESC: { onToggleOpen(false); - event.stopPropagation(); + if (open) { + event.stopPropagation(); + } } } }, diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index d16e303ef..961d39aaf 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -682,7 +682,12 @@ describe('Select.Basic', () => { expect(wrapper.find('input').props().value).toBe(''); expectOpen(wrapper, false); - expect(onKeyDown).not.toHaveBeenCalled(); + expect(onKeyDown).toHaveBeenCalledTimes(0); + + // should keep propagation when optionList is closed + wrapper.simulate('keyDown', { which: KeyCode.ESC }); + wrapper.update(); + expect(onKeyDown).toHaveBeenCalledTimes(1); }); it('close after select', () => {