Skip to content

Commit

Permalink
[Modal] Ignore event.defaultPrevented (#14991)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Mar 23, 2019
1 parent 1a707c6 commit aa15729
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
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

0 comments on commit aa15729

Please sign in to comment.