Skip to content

Commit

Permalink
Merge pull request tajo#50 from Sinewyk/call_only_once_onClose_again
Browse files Browse the repository at this point in the history
close portal logic only if portal is active
  • Loading branch information
tajo committed Jan 26, 2016
2 parents da9def4 + 99bbd6e commit 7f4ebb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export default class Portal extends React.Component {
document.removeEventListener('touchstart', this.handleOutsideMouseClick);
}

if (this.state.active) {
this.closePortal();
}
this.closePortal();
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -121,13 +119,15 @@ export default class Portal extends React.Component {
this.setState({active: false});
};

if (this.props.beforeClose) {
this.props.beforeClose(this.node, resetPortalState);
} else {
resetPortalState(this.node);
}
if (this.state.active) {
if (this.props.beforeClose) {
this.props.beforeClose(this.node, resetPortalState);
} else {
resetPortalState();
}

this.props.onClose();
this.props.onClose();
}
}

handleOutsideMouseClick(e) {
Expand Down
18 changes: 18 additions & 0 deletions test/portal_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,22 @@ describe('react-portal', () => {
const props = {isOpened: true, beforeClose: spy()};
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
wrapper.instance().closePortal();
assert(props.beforeClose.calledOnce);
assert(props.beforeClose.calledWith(wrapper.instance().node));
});

it('should call props.beforeClose() only once even if closePortal is called multiple times', () => {
const props = {isOpened: true, beforeClose: spy((node, cb) => cb())};
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
wrapper.instance().closePortal();
wrapper.instance().closePortal();
assert(props.beforeClose.calledOnce);
});

it('should call props.onOpen() when portal opens', () => {
const props = {isOpened: true, onOpen: spy()};
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
assert(props.onOpen.calledOnce);
assert(props.onOpen.calledWith(wrapper.instance().node));
});

Expand All @@ -121,6 +131,14 @@ describe('react-portal', () => {
assert(props.onClose.calledOnce);
});

it('should call props.onClose() only once even if closePortal is called multiple times', () => {
const props = {isOpened: true, onClose: spy()};
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
wrapper.instance().closePortal();
wrapper.instance().closePortal();
assert(props.onClose.calledOnce);
});

it('should call props.onClose() only once when portal closes and then is unmounted', () => {
const div = document.createElement('div');
const props = {isOpened: true, onClose: spy()};
Expand Down

0 comments on commit 7f4ebb9

Please sign in to comment.