Skip to content

Commit

Permalink
fix: change dismiss handler, listening to key down, instead of click (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot authored Jun 21, 2022
1 parent a877c5d commit d05c42a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_RESIZE = `resize${EVENT_KEY}`
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

Expand Down Expand Up @@ -221,7 +221,7 @@ class Modal extends BaseComponent {
}
})

EventHandler.on(this._element, EVENT_CLICK_DISMISS, event => {
EventHandler.on(this._element, EVENT_MOUSEDOWN_DISMISS, event => {
if (event.target !== event.currentTarget) { // click is inside modal-dialog
return
}
Expand Down
19 changes: 13 additions & 6 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,10 @@ describe('Modal', () => {

modalEl.addEventListener('shown.bs.modal', () => {
const spy = spyOn(modal, '_queueCallback').and.callThrough()
const mouseDown = createEvent('mousedown')

modalEl.click()
modalEl.click()
modalEl.dispatchEvent(mouseDown)
modalEl.dispatchEvent(mouseDown)

setTimeout(() => {
expect(spy).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -709,13 +710,19 @@ describe('Modal', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'

const modalEl = fixtureEl.querySelector('.modal')
const dialogEl = modalEl.querySelector('.modal-dialog')
const modal = new Modal(modalEl)

spyOn(modal, 'hide')

modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
})
const mouseDown = createEvent('mousedown')

modalEl.addEventListener('hidden.bs.modal', () => {
expect(document.querySelector('.modal-backdrop')).toBeNull()
dialogEl.dispatchEvent(mouseDown)
expect(modal.hide).not.toHaveBeenCalled()

modalEl.dispatchEvent(mouseDown)
expect(modal.hide).toHaveBeenCalled()
resolve()
})

Expand Down

0 comments on commit d05c42a

Please sign in to comment.