From 13bd46ea2985413f4facd20ced5b524db42c0abd Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Thu, 30 Jun 2016 16:09:55 -0300 Subject: [PATCH] [fixed] clear the delayed close timer when modal opens again. (#189) * [fixed] Clear the delayed close timer when modal opens again * [fixed] added test for the timeout case. continuation of the PR #134. thanks, @CompuIves. --- lib/components/ModalPortal.js | 26 ++++++++++++++++---------- specs/Modal.spec.js | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/components/ModalPortal.js b/lib/components/ModalPortal.js index 56dc3076..5e887b4f 100644 --- a/lib/components/ModalPortal.js +++ b/lib/components/ModalPortal.js @@ -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() { @@ -104,8 +109,9 @@ var ModalPortal = module.exports = React.createClass({ closeWithoutTimeout: function() { this.setState({ + beforeClose: false, + isOpen: false, afterOpen: false, - beforeClose: false }, this.afterClose); }, diff --git a/specs/Modal.spec.js b/specs/Modal.spec.js index 3fb2f967..bafbbbcb 100644 --- a/specs/Modal.spec.js +++ b/specs/Modal.spec.js @@ -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();