Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Prevent outside click events for unmounted component #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/OutsideClickHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export default class OutsideClickHandler extends React.Component {
this.onMouseDown = this.onMouseDown.bind(this);
this.onMouseUp = this.onMouseUp.bind(this);
this.setChildNodeRef = this.setChildNodeRef.bind(this);
this._isMounted = false;
}

componentDidMount() {
this._isMounted = true;
const { disabled, useCapture } = this.props;

if (!disabled) this.addMouseDownEventListener(useCapture);
Expand All @@ -57,6 +59,7 @@ export default class OutsideClickHandler extends React.Component {
}

componentWillUnmount() {
this._isMounted = false;
this.removeEventListeners();
}

Expand Down Expand Up @@ -87,7 +90,7 @@ export default class OutsideClickHandler extends React.Component {
if (this.removeMouseUp) this.removeMouseUp();
this.removeMouseUp = null;

if (!isDescendantOfRoot) {
if (this._isMounted && !isDescendantOfRoot) {
onOutsideClick(e);
}
}
Expand Down
19 changes: 18 additions & 1 deletion test/OutsideClickHandler_test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('OutsideClickHandler', () => {
const instance = wrapper.instance();

instance.childNode = {};
instance._isMounted = true;
target.parentNode = instance.childNode;
expect(contains(instance.childNode, target)).to.equal(true);

Expand All @@ -48,13 +49,28 @@ describe('OutsideClickHandler', () => {
expect(spy).to.have.property('callCount', 0);
});

describe('when `this.childNode` does not contain `e.target`', () => {
it('is a noop if `this._isMounted` is `false`', () => {
const spy = sinon.spy();
const wrapper = shallow(<OutsideClickHandler onOutsideClick={spy} />);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use mount here, and then wrapper.unmount() to test unmounting a component.

const instance = wrapper.instance();

instance.childNode = {};
instance._isMounted = false;
expect(contains(instance.childNode, target)).to.equal(false);

instance.onMouseUp(event);

expect(spy).to.have.property('callCount', 0);
});

describe('when `this.childNode` does not contain `e.target` and `this._isMounted` is `true`', () => {
it('calls onOutsideClick', () => {
const spy = sinon.spy();
const wrapper = shallow(<OutsideClickHandler onOutsideClick={spy} />);
const instance = wrapper.instance();

instance.childNode = {};
instance._isMounted = true;
expect(contains(instance.childNode, target)).to.equal(false);

instance.onMouseUp(event);
Expand All @@ -63,6 +79,7 @@ describe('OutsideClickHandler', () => {
expect(spy.firstCall.args).to.eql([event]);
});
});

});

describe.skip('lifecycle methods', () => {
Expand Down