Skip to content

Commit 8a71f71

Browse files
gados3diasbruno
gados3
authored andcommitted
[fixed] onAfterClose prop falsly called on unmount
1 parent 1b80146 commit 8a71f71

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ scripts/__pycache__/
55
examples/**/*-bundle.js
66
node_modules/
77
.idea/
8+
.vscode
89
_book
910
*.patch
1011
*.diff

Diff for: specs/Modal.events.spec.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
escKeyDown,
1313
tabKeyDown,
1414
renderModal,
15-
emptyDOM
15+
emptyDOM,
16+
unmountModal
1617
} from "./helper";
1718

1819
export default () => {
@@ -36,6 +37,20 @@ export default () => {
3637
onAfterCloseCallback.called.should.be.ok();
3738
});
3839

40+
it("should not trigger onAfterClose callback when unmounting a closed modal", () => {
41+
const onAfterCloseCallback = sinon.spy();
42+
renderModal({ isOpen: false, onAfterClose: onAfterCloseCallback });
43+
unmountModal();
44+
onAfterCloseCallback.called.should.not.be.ok();
45+
});
46+
47+
it("should trigger onAfterClose callback when unmounting an opened modal", () => {
48+
const onAfterCloseCallback = sinon.spy();
49+
renderModal({ isOpen: true, onAfterClose: onAfterCloseCallback });
50+
unmountModal();
51+
onAfterCloseCallback.called.should.be.ok();
52+
});
53+
3954
it("keeps focus inside the modal when child has no tabbable elements", () => {
4055
let tabPrevented = false;
4156
const modal = renderModal({ isOpen: true }, "hello");

Diff for: src/components/ModalPortal.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ export default class ModalPortal extends Component {
114114
}
115115

116116
componentWillUnmount() {
117-
this.afterClose();
117+
if (this.state.isOpen) {
118+
this.afterClose();
119+
}
118120
clearTimeout(this.closeTimer);
119121
}
120122

0 commit comments

Comments
 (0)