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 if the event prevent default is called #12202

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
1 change: 1 addition & 0 deletions packages/material-ui/src/ButtonBase/createRippleHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function createRippleHandler(instance, eventName, action, cb) {

let ignore = false;

// Ignore events that have been `event.preventDefault()` marked.
if (event.defaultPrevented) {
ignore = true;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/material-ui/src/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ class Modal extends React.Component {
return;
}

// Ignore events that have been `event.preventDefault()` marked.
if (event.defaultPrevented) {
return;
}

if (this.props.onEscapeKeyDown) {
this.props.onEscapeKeyDown(event);
}
Expand Down
73 changes: 34 additions & 39 deletions packages/material-ui/src/Modal/Modal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,24 +249,19 @@ describe('<Modal />', () => {
describe('handleDocumentKeyDown()', () => {
let wrapper;
let instance;
let onEscapeKeyDownStub;
let onCloseStub;
let onEscapeKeyDownSpy;
let onCloseSpy;
let topModalStub;
let event;

beforeEach(() => {
wrapper = shallow(<Modal open={false} />);
instance = wrapper.instance();
onEscapeKeyDownStub = stub().returns(true);
onCloseStub = stub().returns(true);
onEscapeKeyDownSpy = spy();
onCloseSpy = spy();
topModalStub = stub();
wrapper.setProps({ onEscapeKeyDown: onEscapeKeyDownStub, onClose: onCloseStub });
});

afterEach(() => {
onEscapeKeyDownStub.reset();
onCloseStub.reset();
topModalStub.reset();
wrapper = shallow(
<Modal open={false} onEscapeKeyDown={onEscapeKeyDownSpy} onClose={onCloseSpy} />,
);
instance = wrapper.instance();
});

it('should have handleDocumentKeyDown', () => {
Expand All @@ -275,66 +270,66 @@ describe('<Modal />', () => {
});

it('when not mounted should not call onEscapeKeyDown and onClose', () => {
instance = wrapper.instance();
instance.mounted = false;
instance.handleDocumentKeyDown(undefined);
assert.strictEqual(onEscapeKeyDownStub.callCount, 0);
assert.strictEqual(onCloseStub.callCount, 0);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 0);
assert.strictEqual(onCloseSpy.callCount, 0);
});

it('when mounted and not TopModal should not call onEscapeKeyDown and onClose', () => {
topModalStub.returns('false');
topModalStub.returns(false);
wrapper.setProps({ manager: { isTopModal: topModalStub } });
instance = wrapper.instance();
instance.mounted = true;

instance.handleDocumentKeyDown(undefined);
assert.strictEqual(topModalStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownStub.callCount, 0);
assert.strictEqual(onCloseStub.callCount, 0);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 0);
assert.strictEqual(onCloseSpy.callCount, 0);
});

it('when mounted, TopModal and event not esc should not call given funcs', () => {
topModalStub.returns(true);
wrapper.setProps({ manager: { isTopModal: topModalStub } });
instance = wrapper.instance();
instance.mounted = true;
event = { keyCode: keycode('j') }; // Not 'esc'

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

it('should call onEscapeKeyDown and onClose', () => {
topModalStub.returns(true);
wrapper.setProps({ manager: { isTopModal: topModalStub } });
event = { keyCode: keycode('esc') };
instance = wrapper.instance();
instance.mounted = true;

instance.handleDocumentKeyDown(event);
assert.strictEqual(topModalStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownStub.calledWith(event), true);
assert.strictEqual(onCloseStub.callCount, 1);
assert.strictEqual(onCloseStub.calledWith(event), true);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 1);
assert.strictEqual(onEscapeKeyDownSpy.calledWith(event), true);
assert.strictEqual(onCloseSpy.callCount, 1);
assert.strictEqual(onCloseSpy.calledWith(event), true);
});

it('when disableEscapeKeyDown should call only onClose', () => {
topModalStub.returns(true);
wrapper.setProps({ manager: { isTopModal: topModalStub } });
wrapper.setProps({ disableEscapeKeyDown: true });
wrapper.setProps({ disableEscapeKeyDown: true, manager: { isTopModal: topModalStub } });
event = { keyCode: keycode('esc') };
instance = wrapper.instance();
instance.mounted = true;

instance.handleDocumentKeyDown(event);
assert.strictEqual(topModalStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownStub.callCount, 1);
assert.strictEqual(onEscapeKeyDownStub.calledWith(event), true);
assert.strictEqual(onCloseStub.callCount, 0);
assert.strictEqual(onEscapeKeyDownSpy.callCount, 1);
assert.strictEqual(onEscapeKeyDownSpy.calledWith(event), true);
assert.strictEqual(onCloseSpy.callCount, 0);
});

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

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

Expand Down