Skip to content

Commit 49da7b5

Browse files
kyletsangTheSharpieOne
authored andcommitted
fix(Modal): Allow esc to close static backdrop modal when keybo… (#1767)
Fixes #1766
1 parent 1fec610 commit 49da7b5

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

‎docs/lib/examples/ModalBackdrop.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const ModalExample = (props) => {
1010
} = props;
1111
const [modal, setModal] = useState(false);
1212
const [backdrop, setBackdrop] = useState(true);
13+
const [keyboard, setKeyboard] = useState(true);
1314

1415
const toggle = () => setModal(!modal);
1516

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

25+
const changeKeyboard = e => {
26+
setKeyboard(e.currentTarget.checked);
27+
}
28+
2429
return (
2530
<div>
2631
<Form inline onSubmit={(e) => e.preventDefault()}>
@@ -32,10 +37,15 @@ const ModalExample = (props) => {
3237
<option value="static">"static"</option>
3338
</Input>
3439
</FormGroup>
40+
<FormGroup className="mx-2" check>
41+
<Label check>
42+
<Input type="checkbox" checked={keyboard} onChange={changeKeyboard} /> Keyboard
43+
</Label>
44+
</FormGroup>
3545
{' '}
3646
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
3747
</Form>
38-
<Modal isOpen={modal} toggle={toggle} className={className} backdrop={backdrop}>
48+
<Modal isOpen={modal} toggle={toggle} className={className} backdrop={backdrop} keyboard={keyboard}>
3949
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
4050
<ModalBody>
4151
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.

‎src/Modal.js‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,19 @@ class Modal extends React.Component {
254254
}
255255

256256
handleEscape(e) {
257-
if (this.props.isOpen && this.props.keyboard && e.keyCode === keyCodes.esc && this.props.toggle) {
258-
e.preventDefault();
259-
e.stopPropagation();
257+
if (this.props.isOpen && e.keyCode === keyCodes.esc && this.props.toggle) {
258+
if (this.props.keyboard) {
259+
e.preventDefault();
260+
e.stopPropagation();
260261

261-
if (this.props.backdrop === 'static') {
262+
this.props.toggle(e);
263+
}
264+
else if (this.props.backdrop === 'static') {
265+
e.preventDefault();
266+
e.stopPropagation();
267+
262268
this.handleStaticBackdropAnimation();
263-
return;
264269
}
265-
266-
this.props.toggle(e);
267270
}
268271
}
269272

‎src/__tests__/Modal.spec.js‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ describe('Modal', () => {
511511
expect(document.getElementsByClassName('modal').length).toBe(1);
512512

513513
const escapeKeyUpEvent = {
514-
keyCode: 27,
514+
keyCode: keyCodes.esc,
515515
preventDefault: jest.fn(() => {}),
516516
stopPropagation: jest.fn(() => {}),
517517
};
@@ -629,10 +629,10 @@ describe('Modal', () => {
629629
wrapper.unmount();
630630
});
631631

632-
it('should not close modal when escape key pressed when backdrop is "static"', () => {
632+
it('should not close modal when escape key pressed and backdrop is "static" and keyboard=false', () => {
633633
isOpen = true;
634634
const wrapper = didMount(
635-
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
635+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={false}>
636636
<button id="clicker">Does Nothing</button>
637637
</Modal>
638638
);
@@ -657,10 +657,38 @@ describe('Modal', () => {
657657
wrapper.unmount();
658658
});
659659

660-
it('should animate when backdrop is "static" and escape key pressed', () => {
660+
it('should close modal when escape key pressed and backdrop is "static" and keyboard=true', () => {
661661
isOpen = true;
662662
const wrapper = didMount(
663-
<Modal isOpen={isOpen} toggle={toggle} backdrop="static">
663+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={true}>
664+
<button id="clicker">Does Nothing</button>
665+
</Modal>
666+
);
667+
const instance = wrapper.instance();
668+
669+
jest.runTimersToTime(300);
670+
671+
expect(isOpen).toBe(true);
672+
expect(document.getElementsByClassName('modal').length).toBe(1);
673+
674+
const escapeKeyUpEvent = {
675+
keyCode: keyCodes.esc,
676+
preventDefault: jest.fn(() => {}),
677+
stopPropagation: jest.fn(() => {}),
678+
};
679+
680+
instance.handleEscape(escapeKeyUpEvent);
681+
jest.runTimersToTime(300);
682+
683+
expect(isOpen).toBe(false);
684+
685+
wrapper.unmount();
686+
});
687+
688+
it('should animate when backdrop is "static" and escape key pressed and keyboard=false', () => {
689+
isOpen = true;
690+
const wrapper = didMount(
691+
<Modal isOpen={isOpen} toggle={toggle} backdrop="static" keyboard={false}>
664692
<button id="clicker">Does Nothing</button>
665693
</Modal>
666694
);

0 commit comments

Comments
 (0)