Skip to content

Commit

Permalink
[fixed] Add shouldCloseOnEsc prop
Browse files Browse the repository at this point in the history
shouldCloseOnEsc: Add prop and logical check to keyHandler

shouldCloseOnEsc: Add to docs/README.md
  • Loading branch information
spen authored and diasbruno committed Oct 11, 2017
1 parent d98f091 commit 1d495a6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ import ReactModal from 'react-modal';
Boolean indicating if the overlay should close the modal
*/
shouldCloseOnOverlayClick={true}
/*
Boolean indicating if pressing the esc key should close the modal
Note: By disabling the esc key from closing the modal you may introduce an accessibility issue.
*/
shouldCloseOnEsc={true}
/*
String indicating the role of the modal, allowing the 'dialog' role to be applied if desired.
*/
Expand Down
39 changes: 28 additions & 11 deletions specs/Modal.events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,35 @@ export default () => {
document.activeElement.should.be.eql(content);
});

it("should close on Esc key event", () => {
const requestCloseCallback = sinon.spy();
const modal = renderModal({
isOpen: true,
shouldCloseOnOverlayClick: true,
onRequestClose: requestCloseCallback
describe("shouldCloseOnEsc", () => {
context("when true", () => {
it("should close on Esc key event", () => {
const requestCloseCallback = sinon.spy();
const modal = renderModal({
isOpen: true,
shouldCloseOnEsc: true,
onRequestClose: requestCloseCallback
});
escKeyDown(mcontent(modal));
requestCloseCallback.called.should.be.ok();
// Check if event is passed to onRequestClose callback.
const event = requestCloseCallback.getCall(0).args[0];
event.should.be.ok();
});
});

context("when false", () => {
it("should not close on Esc key event", () => {
const requestCloseCallback = sinon.spy();
const modal = renderModal({
isOpen: true,
shouldCloseOnEsc: false,
onRequestClose: requestCloseCallback
});
escKeyDown(mcontent(modal));
requestCloseCallback.called.should.be.false;
});
});
escKeyDown(mcontent(modal));
requestCloseCallback.called.should.be.ok();
// Check if event is passed to onRequestClose callback.
const ev = requestCloseCallback.getCall(0).args[0];
ev.should.be.ok();
});

describe("shouldCloseOnoverlayClick", () => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default class Modal extends Component {
parentSelector: PropTypes.func,
aria: PropTypes.object,
role: PropTypes.string,
contentLabel: PropTypes.string
contentLabel: PropTypes.string,
shouldCloseOnEsc: PropTypes.bool
};
/* eslint-enable react/no-unused-prop-types */

Expand All @@ -54,6 +55,7 @@ export default class Modal extends Component {
ariaHideApp: true,
closeTimeoutMS: 0,
shouldFocusAfterRender: true,
shouldCloseOnEsc: true,
shouldCloseOnOverlayClick: true,
parentSelector() {
return document.body;
Expand Down
6 changes: 4 additions & 2 deletions src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export default class ModalPortal extends Component {
role: PropTypes.string,
contentLabel: PropTypes.string,
aria: PropTypes.object,
children: PropTypes.node
children: PropTypes.node,
shouldCloseOnEsc: PropTypes.bool
};

constructor(props) {
Expand Down Expand Up @@ -195,7 +196,8 @@ export default class ModalPortal extends Component {
if (event.keyCode === TAB_KEY) {
scopeTab(this.content, event);
}
if (event.keyCode === ESC_KEY) {

if (this.props.shouldCloseOnEsc && event.keyCode === ESC_KEY) {
event.preventDefault();
this.requestClose(event);
}
Expand Down

0 comments on commit 1d495a6

Please sign in to comment.