0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Toast should allow prevent default for hide and show events
This commit is contained in:
Johann-S 2019-05-22 09:55:14 +02:00 committed by XhmikosR
parent fe777292b5
commit c6dd1a7d93
2 changed files with 84 additions and 2 deletions

View File

@ -82,7 +82,12 @@ class Toast {
// Public // Public
show() { show() {
$(this._element).trigger(Event.SHOW) const showEvent = $.Event(Event.SHOW)
$(this._element).trigger(showEvent)
if (showEvent.isDefaultPrevented()) {
return
}
if (this._config.animation) { if (this._config.animation) {
this._element.classList.add(ClassName.FADE) this._element.classList.add(ClassName.FADE)
@ -119,7 +124,13 @@ class Toast {
return return
} }
$(this._element).trigger(Event.HIDE) const hideEvent = $.Event(Event.HIDE)
$(this._element).trigger(hideEvent)
if (hideEvent.isDefaultPrevented()) {
return
}
this._close() this._close()
} }

View File

@ -256,4 +256,75 @@ $(function () {
var toast = $toast.data('bs.toast') var toast = $toast.data('bs.toast')
assert.strictEqual(toast._config.delay, defaultDelay) assert.strictEqual(toast._config.delay, defaultDelay)
}) })
QUnit.test('should not trigger shown if show is prevented', function (assert) {
assert.expect(1)
var done = assert.async()
var toastHtml =
'<div class="toast" data-delay="1" data-autohide="false">' +
'<div class="toast-body">' +
'a simple toast' +
'</div>' +
'</div>'
var $toast = $(toastHtml)
.bootstrapToast()
.appendTo($('#qunit-fixture'))
var shownCalled = false
function assertDone() {
setTimeout(function () {
assert.strictEqual(shownCalled, false)
done()
}, 20)
}
$toast
.on('show.bs.toast', function (event) {
event.preventDefault()
assertDone()
})
.on('shown.bs.toast', function () {
shownCalled = true
})
.bootstrapToast('show')
})
QUnit.test('should not trigger hidden if hide is prevented', function (assert) {
assert.expect(1)
var done = assert.async()
var toastHtml =
'<div class="toast" data-delay="1" data-autohide="false">' +
'<div class="toast-body">' +
'a simple toast' +
'</div>' +
'</div>'
var $toast = $(toastHtml)
.bootstrapToast()
.appendTo($('#qunit-fixture'))
var hiddenCalled = false
function assertDone() {
setTimeout(function () {
assert.strictEqual(hiddenCalled, false)
done()
}, 20)
}
$toast
.on('shown.bs.toast', function () {
$toast.bootstrapToast('hide')
})
.on('hide.bs.toast', function (event) {
event.preventDefault()
assertDone()
})
.on('hidden.bs.toast', function () {
hiddenCalled = true
})
.bootstrapToast('show')
})
}) })