Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Modal] Ignore event.defaultPrevented #14991

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/material-ui/src/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,13 @@ class Modal extends React.Component {
};

handleKeyDown = event => {
// event.defaultPrevented:
// We don't take event.defaultPrevented into account:
//
// Ignore events that have been `event.preventDefault()` marked.
// preventDefault() is meant to stop default behaviours like
// event.preventDefault() is meant to stop default behaviours like
// clicking a checkbox to check it, hitting a button to submit a form,
// and hitting left arrow to move the cursor in a text input etc.
// Only special HTML elements have these default bahaviours.
//
// To remove in v4.
if (event.key !== 'Escape' || !this.isTopModal() || event.defaultPrevented) {
// Only special HTML elements have these default behaviors.
if (event.key !== 'Escape' || !this.isTopModal()) {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions packages/material-ui/src/Modal/Modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ describe('<Modal />', () => {
assert.strictEqual(onCloseSpy.callCount, 0);
});

it('should not be call when defaultPrevented', () => {
it('should be call when defaultPrevented', () => {
topModalStub.returns(true);
wrapper.setProps({ disableEscapeKeyDown: true, manager: { isTopModal: topModalStub } });
event = { key: 'Escape', defaultPrevented: true };
wrapper.setProps({ manager: { isTopModal: topModalStub } });
event = { key: 'Escape', defaultPrevented: true, stopPropagation: () => {} };

instance.handleKeyDown(event);
assert.strictEqual(topModalStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 0);
assert.strictEqual(onCloseSpy.callCount, 0);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 1);
assert.strictEqual(onCloseSpy.callCount, 1);
});
});

Expand Down