Skip to content

Commit 13bd46e

Browse files
diasbrunoclaydiffrient
authored andcommitted
[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.
1 parent 70d91eb commit 13bd46e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

lib/components/ModalPortal.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,20 @@ var ModalPortal = module.exports = React.createClass({
7272
},
7373

7474
open: function() {
75-
focusManager.setupScopedFocus(this.node);
76-
focusManager.markForFocusLater();
77-
this.setState({isOpen: true}, function() {
78-
this.setState({afterOpen: true});
79-
80-
if (this.props.isOpen && this.props.onAfterOpen) {
81-
this.props.onAfterOpen();
82-
}
83-
}.bind(this));
75+
if (this.state.afterOpen && this.state.beforeClose) {
76+
clearTimeout(this.closeTimer);
77+
this.setState({ beforeClose: false });
78+
} else {
79+
focusManager.setupScopedFocus(this.node);
80+
focusManager.markForFocusLater();
81+
this.setState({isOpen: true}, function() {
82+
this.setState({afterOpen: true});
83+
84+
if (this.props.isOpen && this.props.onAfterOpen) {
85+
this.props.onAfterOpen();
86+
}
87+
}.bind(this));
88+
}
8489
},
8590

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

105110
closeWithoutTimeout: function() {
106111
this.setState({
112+
beforeClose: false,
113+
isOpen: false,
107114
afterOpen: false,
108-
beforeClose: false
109115
}, this.afterClose);
110116
},
111117

specs/Modal.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ describe('Modal', function () {
200200
unmountModal();
201201
});
202202

203+
it('check the state of the modal after close with time out and reopen it', function() {
204+
var afterOpenCallback = sinon.spy();
205+
var modal = renderModal({
206+
isOpen: true,
207+
closeTimeoutMS: 2000,
208+
onRequestClose: function() {}
209+
});
210+
modal.portal.closeWithTimeout();
211+
modal.portal.open();
212+
modal.portal.closeWithoutTimeout();
213+
ok(!modal.portal.state.isOpen);
214+
unmountModal();
215+
});
216+
203217
describe('should close on overlay click', function() {
204218
afterEach('Unmount modal', function() {
205219
unmountModal();

0 commit comments

Comments
 (0)