Skip to content

Commit

Permalink
[fixed] clear the delayed close timer when modal opens again. (reactj…
Browse files Browse the repository at this point in the history
…s#189)

* [fixed] Clear the delayed close timer when modal opens again

* [fixed] added test for the timeout case.

continuation of the PR reactjs#134.

thanks, @CompuIves.
  • Loading branch information
diasbruno authored and claydiffrient committed Jun 30, 2016
1 parent 70d91eb commit 13bd46e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,20 @@ var ModalPortal = module.exports = React.createClass({
},

open: function() {
focusManager.setupScopedFocus(this.node);
focusManager.markForFocusLater();
this.setState({isOpen: true}, function() {
this.setState({afterOpen: true});

if (this.props.isOpen && this.props.onAfterOpen) {
this.props.onAfterOpen();
}
}.bind(this));
if (this.state.afterOpen && this.state.beforeClose) {
clearTimeout(this.closeTimer);
this.setState({ beforeClose: false });
} else {
focusManager.setupScopedFocus(this.node);
focusManager.markForFocusLater();
this.setState({isOpen: true}, function() {
this.setState({afterOpen: true});

if (this.props.isOpen && this.props.onAfterOpen) {
this.props.onAfterOpen();
}
}.bind(this));
}
},

close: function() {
Expand All @@ -104,8 +109,9 @@ var ModalPortal = module.exports = React.createClass({

closeWithoutTimeout: function() {
this.setState({
beforeClose: false,
isOpen: false,
afterOpen: false,
beforeClose: false
}, this.afterClose);
},

Expand Down
14 changes: 14 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ describe('Modal', function () {
unmountModal();
});

it('check the state of the modal after close with time out and reopen it', function() {
var afterOpenCallback = sinon.spy();
var modal = renderModal({
isOpen: true,
closeTimeoutMS: 2000,
onRequestClose: function() {}
});
modal.portal.closeWithTimeout();
modal.portal.open();
modal.portal.closeWithoutTimeout();
ok(!modal.portal.state.isOpen);
unmountModal();
});

describe('should close on overlay click', function() {
afterEach('Unmount modal', function() {
unmountModal();
Expand Down

0 comments on commit 13bd46e

Please sign in to comment.