Skip to content

Commit a8afd1e

Browse files
author
Weslen Nascimento
authored
[Modal] Should propagate event if disableEscapeKeyDown (#20786)
1 parent 70e8e96 commit a8afd1e

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

docs/pages/api-docs/modal.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ This component shares many concepts with [react-overlays](https://react-bootstra
4242
| <span class="prop-name">closeAfterTransition</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | When set to true the Modal waits until a nested Transition is completed before closing. |
4343
| <span class="prop-name">container</span> | <span class="prop-type">HTML element<br>&#124;&nbsp;React.Component<br>&#124;&nbsp;func</span> | | A HTML element, component instance, or function that returns either. The `container` will have the portal children appended to it.<br>By default, it uses the body of the top-level document object, so it's simply `document.body` most of the time. |
4444
| <span class="prop-name">disableAutoFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the modal will not automatically shift focus to itself when it opens, and replace it to the last focused element when it closes. This also works correctly with any modal children that have the `disableAutoFocus` prop.<br>Generally this should never be set to `true` as it makes the modal less accessible to assistive technologies, like screen readers. |
45-
| <span class="prop-name">disableBackdropClick</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, clicking the backdrop will not fire any callback. |
45+
| <span class="prop-name">disableBackdropClick</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, clicking the backdrop will not fire `onClose`. |
4646
| <span class="prop-name">disableEnforceFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the modal will not prevent focus from leaving the modal while open.<br>Generally this should never be set to `true` as it makes the modal less accessible to assistive technologies, like screen readers. |
47-
| <span class="prop-name">disableEscapeKeyDown</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, hitting escape will not fire any callback. |
47+
| <span class="prop-name">disableEscapeKeyDown</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, hitting escape will not fire `onClose`. |
4848
| <span class="prop-name">disablePortal</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | Disable the portal behavior. The children stay within it's parent DOM hierarchy. |
4949
| <span class="prop-name">disableRestoreFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | If `true`, the modal will not restore focus to previously focused element once modal is hidden. |
5050
| <span class="prop-name">disableScrollLock</span> | <span class="prop-type">bool</span> | <span class="prop-default">false</span> | Disable the scroll lock behavior. |

packages/material-ui/src/ButtonBase/ButtonBase.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ describe('<ButtonBase />', () => {
830830
});
831831

832832
it('does not call onClick if Space was released on a child', () => {
833-
const onClickSpy = spy((event) => event.defaultPrevented);
833+
const onClickSpy = spy();
834834
const onKeyUpSpy = spy();
835835
render(
836836
<ButtonBase onClick={onClickSpy} onKeyUp={onKeyUpSpy} component="div">

packages/material-ui/src/Modal/Modal.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ const Modal = React.forwardRef(function Modal(inProps, ref) {
193193
return;
194194
}
195195

196-
// Swallow the event, in case someone is listening for the escape key on the body.
197-
event.stopPropagation();
198-
199196
if (onEscapeKeyDown) {
200197
onEscapeKeyDown(event);
201198
}
202199

203-
if (!disableEscapeKeyDown && onClose) {
204-
onClose(event, 'escapeKeyDown');
200+
if (!disableEscapeKeyDown) {
201+
// Swallow the event, in case someone is listening for the escape key on the body.
202+
event.stopPropagation();
203+
204+
if (onClose) {
205+
onClose(event, 'escapeKeyDown');
206+
}
205207
}
206208
};
207209

@@ -294,7 +296,7 @@ Modal.propTypes = {
294296
*/
295297
disableAutoFocus: PropTypes.bool,
296298
/**
297-
* If `true`, clicking the backdrop will not fire any callback.
299+
* If `true`, clicking the backdrop will not fire `onClose`.
298300
*/
299301
disableBackdropClick: PropTypes.bool,
300302
/**
@@ -305,7 +307,7 @@ Modal.propTypes = {
305307
*/
306308
disableEnforceFocus: PropTypes.bool,
307309
/**
308-
* If `true`, hitting escape will not fire any callback.
310+
* If `true`, hitting escape will not fire `onClose`.
309311
*/
310312
disableEscapeKeyDown: PropTypes.bool,
311313
/**

packages/material-ui/src/Modal/Modal.test.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,15 @@ describe('<Modal />', () => {
270270
});
271271

272272
it('should call onEscapeKeyDown and onClose', () => {
273+
const handleKeyDown = spy();
273274
const onEscapeKeyDownSpy = spy();
274275
const onCloseSpy = spy();
275276
const { getByTestId } = render(
276-
<Modal data-testid="modal" open onEscapeKeyDown={onEscapeKeyDownSpy} onClose={onCloseSpy}>
277-
<div />
278-
</Modal>,
277+
<div onKeyDown={handleKeyDown}>
278+
<Modal data-testid="modal" open onEscapeKeyDown={onEscapeKeyDownSpy} onClose={onCloseSpy}>
279+
<div />
280+
</Modal>
281+
</div>,
279282
);
280283
getByTestId('modal').focus();
281284

@@ -285,21 +288,25 @@ describe('<Modal />', () => {
285288

286289
expect(onEscapeKeyDownSpy).to.have.property('callCount', 1);
287290
expect(onCloseSpy).to.have.property('callCount', 1);
291+
expect(handleKeyDown).to.have.property('callCount', 0);
288292
});
289293

290-
it('when disableEscapeKeyDown should call only onEscapeKeyDownSpy', () => {
294+
it('should not call onChange when `disableEscapeKeyDown=true`', () => {
295+
const handleKeyDown = spy();
291296
const onEscapeKeyDownSpy = spy();
292297
const onCloseSpy = spy();
293298
const { getByTestId } = render(
294-
<Modal
295-
data-testid="modal"
296-
open
297-
disableEscapeKeyDown
298-
onEscapeKeyDown={onEscapeKeyDownSpy}
299-
onClose={onCloseSpy}
300-
>
301-
<div />
302-
</Modal>,
299+
<div onKeyDown={handleKeyDown}>
300+
<Modal
301+
data-testid="modal"
302+
open
303+
disableEscapeKeyDown
304+
onEscapeKeyDown={onEscapeKeyDownSpy}
305+
onClose={onCloseSpy}
306+
>
307+
<div />
308+
</Modal>
309+
</div>,
303310
);
304311
getByTestId('modal').focus();
305312

@@ -309,6 +316,7 @@ describe('<Modal />', () => {
309316

310317
expect(onEscapeKeyDownSpy).to.have.property('callCount', 1);
311318
expect(onCloseSpy).to.have.property('callCount', 0);
319+
expect(handleKeyDown).to.have.property('callCount', 1);
312320
});
313321
});
314322

0 commit comments

Comments
 (0)