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(Modal): do not close when mouse click was occurred inside #3582

Merged
merged 5 commits into from
May 5, 2019
Merged
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
22 changes: 21 additions & 1 deletion src/modules/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Modal extends Component {

ref = createRef()
dimmerRef = createRef()
latestDocumentMouseDownEvent = null

componentWillUnmount() {
debug('componentWillUnmount()')
Expand All @@ -178,11 +179,22 @@ class Modal extends Component {
this.trySetState({ open: false })
}

handleDocumentMouseDown = (e) => {
this.latestDocumentMouseDownEvent = e
}

handleDocumentClick = (e) => {
debug('handleDocumentClick()')
const { closeOnDimmerClick } = this.props
const currentDocumentMouseDownEvent = this.latestDocumentMouseDownEvent
this.latestDocumentMouseDownEvent = null

if (!closeOnDimmerClick || doesNodeContainClick(this.ref.current, e)) return
if (
!closeOnDimmerClick ||
doesNodeContainClick(this.ref.current, currentDocumentMouseDownEvent) ||
doesNodeContainClick(this.ref.current, e)
)
return

_.invoke(this.props, 'onClose', e, this.props)
this.trySetState({ open: false })
Expand All @@ -209,6 +221,10 @@ class Modal extends Component {
this.setState({ scrolling: false })
this.setPositionAndClassNames()

eventStack.sub('mousedown', this.handleDocumentMouseDown, {
pool: eventPool,
target: this.dimmerRef.current,
})
eventStack.sub('click', this.handleDocumentClick, {
pool: eventPool,
target: this.dimmerRef.current,
Expand All @@ -221,6 +237,10 @@ class Modal extends Component {
debug('handlePortalUnmount()', { eventPool })

cancelAnimationFrame(this.animationRequestId)
eventStack.unsub('mousedown', this.handleDocumentMouseDown, {
pool: eventPool,
target: this.dimmerRef.current,
})
eventStack.unsub('click', this.handleDocumentClick, {
pool: eventPool,
target: this.dimmerRef.current,
Expand Down
4 changes: 2 additions & 2 deletions test/specs/addons/Confirm/Confirm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('Confirm', () => {
autoGenerateKey: false,
propKey: 'header',
ShorthandComponent: Modal.Header,
mapValueToProps: content => ({ content }),
mapValueToProps: (content) => ({ content }),
})
common.implementsShorthandProp(Confirm, {
autoGenerateKey: false,
propKey: 'content',
ShorthandComponent: Modal.Content,
mapValueToProps: content => ({ content }),
mapValueToProps: (content) => ({ content }),
})

describe('children', () => {
Expand Down
12 changes: 10 additions & 2 deletions test/specs/modules/Modal/Modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ describe('Modal', () => {
autoGenerateKey: false,
propKey: 'header',
ShorthandComponent: ModalHeader,
mapValueToProps: content => ({ content }),
mapValueToProps: (content) => ({ content }),
})
common.implementsShorthandProp(Modal, {
autoGenerateKey: false,
propKey: 'content',
ShorthandComponent: ModalContent,
mapValueToProps: content => ({ content }),
mapValueToProps: (content) => ({ content }),
})

// Heads up!
Expand Down Expand Up @@ -330,6 +330,14 @@ describe('Modal', () => {
spy.should.have.been.calledOnce()
})

it('is not called on mousedown inside and mouseup outside of the modal', () => {
wrapperMount(<Modal onClose={spy} defaultOpen />)

domEvent.mouseDown(document.querySelector('.ui.modal'))
domEvent.click(document.querySelector('.ui.modal').parentNode)
spy.should.not.have.been.called()
})

it('is not called on click inside of the modal', () => {
wrapperMount(<Modal onClose={spy} defaultOpen />)

Expand Down