From 01b02f7fbb3bef8c5eace7634fc10aa5d05b2ea4 Mon Sep 17 00:00:00 2001 From: Dimitris Karagiannis Date: Thu, 26 Oct 2017 21:17:32 +0300 Subject: [PATCH] fix(Popup): execute `onClose` when Popup closes on scroll (#2182) * fix(Popup): Execute onClose when popup closes on scroll * test(Popup): add test for onClose --- src/modules/Popup/Popup.js | 8 +++++--- test/specs/modules/Popup/Popup-test.js | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/modules/Popup/Popup.js b/src/modules/Popup/Popup.js index d4e6bc3113..0e401b8602 100644 --- a/src/modules/Popup/Popup.js +++ b/src/modules/Popup/Popup.js @@ -278,17 +278,19 @@ export default class Popup extends Component { return portalProps } - hideOnScroll = () => { + hideOnScroll = (e) => { this.setState({ closed: true }) eventStack.unsub('scroll', this.hideOnScroll, { target: window }) setTimeout(() => this.setState({ closed: false }), 50) + + this.handleClose(e) } handleClose = (e) => { debug('handleClose()') - const { onClose } = this.props - if (onClose) onClose(e, this.props) + + _.invoke(this.props, 'onClose', e, this.props) } handleOpen = (e) => { diff --git a/test/specs/modules/Popup/Popup-test.js b/test/specs/modules/Popup/Popup-test.js index 85752c4726..41f0950ae5 100644 --- a/test/specs/modules/Popup/Popup-test.js +++ b/test/specs/modules/Popup/Popup-test.js @@ -163,14 +163,21 @@ describe('Popup', () => { wrapper.find('button').simulate('click') assertInBody('.ui.popup.visible') - document.body.scrollTop = 100 + domEvent.scroll(window) + assertInBody('.ui.popup.visible', false) + }) - const evt = document.createEvent('CustomEvent') - evt.initCustomEvent('scroll', false, false, null) + it('is called with (e, props) when scroll', () => { + const onClose = sandbox.spy() + const trigger = - window.dispatchEvent(evt) + wrapperMount() + .find('button') + .simulate('click') - assertInBody('.ui.popup.visible', false) + domEvent.scroll(window) + onClose.should.have.been.calledOnce() + onClose.should.have.been.calledWithMatch({}, { content: 'foo', onClose, trigger }) }) })