From 49da7b5ba95818362c8fb55ca2e4136269f02b22 Mon Sep 17 00:00:00 2001 From: Kyle Tsang <6854874+kyletsang@users.noreply.github.com> Date: Mon, 27 Jan 2020 07:43:13 -0800 Subject: [PATCH] =?UTF-8?q?fix(Modal):=20Allow=20esc=20to=20close=20static?= =?UTF-8?q?=20backdrop=20modal=20when=20keybo=E2=80=A6=20(#1767)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #1766 --- docs/lib/examples/ModalBackdrop.js | 12 +++++++++- src/Modal.js | 17 +++++++------ src/__tests__/Modal.spec.js | 38 ++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/docs/lib/examples/ModalBackdrop.js b/docs/lib/examples/ModalBackdrop.js index 4131c4d0d..5680e20ed 100644 --- a/docs/lib/examples/ModalBackdrop.js +++ b/docs/lib/examples/ModalBackdrop.js @@ -10,6 +10,7 @@ const ModalExample = (props) => { } = props; const [modal, setModal] = useState(false); const [backdrop, setBackdrop] = useState(true); + const [keyboard, setKeyboard] = useState(true); const toggle = () => setModal(!modal); @@ -21,6 +22,10 @@ const ModalExample = (props) => { setBackdrop(value); } + const changeKeyboard = e => { + setKeyboard(e.currentTarget.checked); + } + return (
e.preventDefault()}> @@ -32,10 +37,15 @@ const ModalExample = (props) => { + + + {' '}
- + Modal title Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. diff --git a/src/Modal.js b/src/Modal.js index 613625136..d07ca90a6 100644 --- a/src/Modal.js +++ b/src/Modal.js @@ -254,16 +254,19 @@ class Modal extends React.Component { } handleEscape(e) { - if (this.props.isOpen && this.props.keyboard && e.keyCode === keyCodes.esc && this.props.toggle) { - e.preventDefault(); - e.stopPropagation(); + if (this.props.isOpen && e.keyCode === keyCodes.esc && this.props.toggle) { + if (this.props.keyboard) { + e.preventDefault(); + e.stopPropagation(); - if (this.props.backdrop === 'static') { + this.props.toggle(e); + } + else if (this.props.backdrop === 'static') { + e.preventDefault(); + e.stopPropagation(); + this.handleStaticBackdropAnimation(); - return; } - - this.props.toggle(e); } } diff --git a/src/__tests__/Modal.spec.js b/src/__tests__/Modal.spec.js index 0784dde38..143c64ec1 100644 --- a/src/__tests__/Modal.spec.js +++ b/src/__tests__/Modal.spec.js @@ -511,7 +511,7 @@ describe('Modal', () => { expect(document.getElementsByClassName('modal').length).toBe(1); const escapeKeyUpEvent = { - keyCode: 27, + keyCode: keyCodes.esc, preventDefault: jest.fn(() => {}), stopPropagation: jest.fn(() => {}), }; @@ -629,10 +629,10 @@ describe('Modal', () => { wrapper.unmount(); }); - it('should not close modal when escape key pressed when backdrop is "static"', () => { + it('should not close modal when escape key pressed and backdrop is "static" and keyboard=false', () => { isOpen = true; const wrapper = didMount( - + ); @@ -657,10 +657,38 @@ describe('Modal', () => { wrapper.unmount(); }); - it('should animate when backdrop is "static" and escape key pressed', () => { + it('should close modal when escape key pressed and backdrop is "static" and keyboard=true', () => { isOpen = true; const wrapper = didMount( - + + + + ); + const instance = wrapper.instance(); + + jest.runTimersToTime(300); + + expect(isOpen).toBe(true); + expect(document.getElementsByClassName('modal').length).toBe(1); + + const escapeKeyUpEvent = { + keyCode: keyCodes.esc, + preventDefault: jest.fn(() => {}), + stopPropagation: jest.fn(() => {}), + }; + + instance.handleEscape(escapeKeyUpEvent); + jest.runTimersToTime(300); + + expect(isOpen).toBe(false); + + wrapper.unmount(); + }); + + it('should animate when backdrop is "static" and escape key pressed and keyboard=false', () => { + isOpen = true; + const wrapper = didMount( + );