From 04db149922dcdffd8b7c1f783b94ec2bf8ad8afd Mon Sep 17 00:00:00 2001 From: Robert Klep Date: Thu, 21 Apr 2016 04:44:53 +0200 Subject: [PATCH] [added] Propagate event on close request (#91) --- lib/components/ModalPortal.js | 8 ++++---- specs/Modal.spec.js | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/components/ModalPortal.js b/lib/components/ModalPortal.js index dc8003c9..56dc3076 100644 --- a/lib/components/ModalPortal.js +++ b/lib/components/ModalPortal.js @@ -118,7 +118,7 @@ var ModalPortal = module.exports = React.createClass({ if (event.keyCode == 9 /*tab*/) scopeTab(this.refs.content, event); if (event.keyCode == 27 /*esc*/) { event.preventDefault(); - this.requestClose(); + this.requestClose(event); } }, @@ -132,15 +132,15 @@ var ModalPortal = module.exports = React.createClass({ if (this.props.shouldCloseOnOverlayClick) { if (this.ownerHandlesClose()) - this.requestClose(); + this.requestClose(event); else this.focusContent(); } }, - requestClose: function() { + requestClose: function(event) { if (this.ownerHandlesClose()) - this.props.onRequestClose(); + this.props.onRequestClose(event); }, ownerHandlesClose: function() { diff --git a/specs/Modal.spec.js b/specs/Modal.spec.js index fd525340..2883bed2 100644 --- a/specs/Modal.spec.js +++ b/specs/Modal.spec.js @@ -272,6 +272,44 @@ describe('Modal', function () { ok(hasPropagated) }); }); + + it('verify event passing on overlay click', function() { + var requestCloseCallback = sinon.spy(); + var modal = renderModal({ + isOpen: true, + shouldCloseOnOverlayClick: true, + onRequestClose: requestCloseCallback, + }); + equal(modal.props.isOpen, true); + var overlay = TestUtils.scryRenderedDOMComponentsWithClass(modal.portal, 'ReactModal__Overlay'); + equal(overlay.length, 1); + Simulate.click(overlay[0]); // click the overlay + ok(requestCloseCallback.called) + // Check if event is passed to onRequestClose callback. + var event = requestCloseCallback.getCall(0).args[0]; + ok(event); + ok(event.constructor); + equal(event.constructor.name, 'SyntheticEvent'); + }); + }); + + it('should close on Esc key event', function() { + var requestCloseCallback = sinon.spy(); + var modal = renderModal({ + isOpen: true, + shouldCloseOnOverlayClick: true, + onRequestClose: requestCloseCallback, + }); + equal(modal.props.isOpen, true); + assert.doesNotThrow(function() { + Simulate.keyDown(modal.portal.refs.content, {key: "Esc", keyCode: 27, which: 27}) + }); + ok(requestCloseCallback.called) + // Check if event is passed to onRequestClose callback. + var event = requestCloseCallback.getCall(0).args[0]; + ok(event); + ok(event.constructor); + equal(event.constructor.name, 'SyntheticEvent'); }); //it('adds --before-close for animations', function() {