0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-18 10:52:19 +01:00

Fix prevented show event disables modals with fade class from being displayed again (#34085)

Fix modal, in case is faded, a prevented show event can cause show method to not  be executed again.
This commit is contained in:
alpadev 2021-05-24 17:52:36 +02:00 committed by GitHub
parent 136665903b
commit b513a19003
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View File

@ -108,20 +108,20 @@ class Modal extends BaseComponent {
return
}
if (this._isAnimated()) {
this._isTransitioning = true
}
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
relatedTarget
})
if (this._isShown || showEvent.defaultPrevented) {
if (showEvent.defaultPrevented) {
return
}
this._isShown = true
if (this._isAnimated()) {
this._isTransitioning = true
}
scrollBarHide()
document.body.classList.add(CLASS_NAME_OPEN)

View File

@ -203,6 +203,33 @@ describe('Modal', () => {
modal.show()
})
it('should be shown after the first call to show() has been prevented while fading is enabled ', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
let prevented = false
modalEl.addEventListener('show.bs.modal', e => {
if (!prevented) {
e.preventDefault()
prevented = true
setTimeout(() => {
modal.show()
})
}
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(prevented).toBeTrue()
expect(modal._isAnimated()).toBeTrue()
done()
})
modal.show()
})
it('should set is transitioning if fade class is present', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
@ -210,8 +237,10 @@ describe('Modal', () => {
const modal = new Modal(modalEl)
modalEl.addEventListener('show.bs.modal', () => {
setTimeout(() => {
expect(modal._isTransitioning).toEqual(true)
})
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(modal._isTransitioning).toEqual(false)