From 8a71f71698d4c06ee9f57336ba01906636ddd2b9 Mon Sep 17 00:00:00 2001 From: gados3 Date: Mon, 27 May 2019 22:04:33 -0400 Subject: [PATCH] [fixed] onAfterClose prop falsly called on unmount --- .gitignore | 1 + specs/Modal.events.spec.js | 17 ++++++++++++++++- src/components/ModalPortal.js | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5ef0c3ce..a75cdaf9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ scripts/__pycache__/ examples/**/*-bundle.js node_modules/ .idea/ +.vscode _book *.patch *.diff diff --git a/specs/Modal.events.spec.js b/specs/Modal.events.spec.js index 299b1ba2..5565f472 100644 --- a/specs/Modal.events.spec.js +++ b/specs/Modal.events.spec.js @@ -12,7 +12,8 @@ import { escKeyDown, tabKeyDown, renderModal, - emptyDOM + emptyDOM, + unmountModal } from "./helper"; export default () => { @@ -36,6 +37,20 @@ export default () => { onAfterCloseCallback.called.should.be.ok(); }); + it("should not trigger onAfterClose callback when unmounting a closed modal", () => { + const onAfterCloseCallback = sinon.spy(); + renderModal({ isOpen: false, onAfterClose: onAfterCloseCallback }); + unmountModal(); + onAfterCloseCallback.called.should.not.be.ok(); + }); + + it("should trigger onAfterClose callback when unmounting an opened modal", () => { + const onAfterCloseCallback = sinon.spy(); + renderModal({ isOpen: true, onAfterClose: onAfterCloseCallback }); + unmountModal(); + onAfterCloseCallback.called.should.be.ok(); + }); + it("keeps focus inside the modal when child has no tabbable elements", () => { let tabPrevented = false; const modal = renderModal({ isOpen: true }, "hello"); diff --git a/src/components/ModalPortal.js b/src/components/ModalPortal.js index 5384ad6c..85fd693b 100644 --- a/src/components/ModalPortal.js +++ b/src/components/ModalPortal.js @@ -114,7 +114,9 @@ export default class ModalPortal extends Component { } componentWillUnmount() { - this.afterClose(); + if (this.state.isOpen) { + this.afterClose(); + } clearTimeout(this.closeTimer); }