Skip to content

Commit

Permalink
[added] Propagate event on close request (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertklep authored and claydiffrient committed Apr 21, 2016
1 parent 7f9bd95 commit 04db149
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
},

Expand All @@ -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() {
Expand Down
38 changes: 38 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 04db149

Please sign in to comment.