Skip to content

Commit

Permalink
fix(Modal): Allow esc to close static backdrop modal when keybo… (#1767)
Browse files Browse the repository at this point in the history
Fixes #1766
  • Loading branch information
kyletsang authored and TheSharpieOne committed Jan 27, 2020
1 parent 1fec610 commit 49da7b5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
12 changes: 11 additions & 1 deletion docs/lib/examples/ModalBackdrop.js
Expand Up @@ -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);

Expand All @@ -21,6 +22,10 @@ const ModalExample = (props) => {
setBackdrop(value);
}

const changeKeyboard = e => {
setKeyboard(e.currentTarget.checked);
}

return (
<div>
<Form inline onSubmit={(e) => e.preventDefault()}>
Expand All @@ -32,10 +37,15 @@ const ModalExample = (props) => {
<option value="static">"static"</option>
</Input>
</FormGroup>
<FormGroup className="mx-2" check>
<Label check>
<Input type="checkbox" checked={keyboard} onChange={changeKeyboard} /> Keyboard
</Label>
</FormGroup>
{' '}
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
</Form>
<Modal isOpen={modal} toggle={toggle} className={className} backdrop={backdrop}>
<Modal isOpen={modal} toggle={toggle} className={className} backdrop={backdrop} keyboard={keyboard}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
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.
Expand Down
17 changes: 10 additions & 7 deletions src/Modal.js
Expand Up @@ -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);
}
}

Expand Down
38 changes: 33 additions & 5 deletions src/__tests__/Modal.spec.js
Expand Up @@ -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(() => {}),
};
Expand Down Expand Up @@ -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(
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={false}>
<button id="clicker">Does Nothing</button>
</Modal>
);
Expand All @@ -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(
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={true}>
<button id="clicker">Does Nothing</button>
</Modal>
);
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(
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={false}>
<button id="clicker">Does Nothing</button>
</Modal>
);
Expand Down

0 comments on commit 49da7b5

Please sign in to comment.