1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-30 08:39:56 +02:00
Close modal with keyboard=true & backdrop=static
This commit is contained in:
Giovanni Mendoza
2020-01-10 11:06:12 +02:00
committed by XhmikosR
parent 0f0a8c364d
commit 6daae47cc0
3 changed files with 58 additions and 6 deletions

View File

@@ -325,9 +325,12 @@ class Modal {
}
_setEscapeEvent() {
if (this._isShown && this._config.keyboard) {
if (this._isShown) {
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
if (event.which === ESCAPE_KEYCODE) {
if (this._config.keyboard && event.which === ESCAPE_KEYCODE) {
event.preventDefault()
this.hide()
} else if (!this._config.keyboard && event.which === ESCAPE_KEYCODE) {
this._triggerBackdropTransition()
}
})

View File

@@ -855,4 +855,53 @@ $(function () {
backdrop: 'static'
})
})
QUnit.test('should close modal when escape key is pressed with keyboard = true and backdrop is static', function (assert) {
assert.expect(1)
var done = assert.async()
var $modal = $('<div class="modal" data-backdrop="static" data-keyboard="true"><div class="modal-dialog" /></div>').appendTo('#qunit-fixture')
$modal.on('shown.bs.modal', function () {
$modal.trigger($.Event('keydown', {
which: 27
}))
setTimeout(function () {
var modal = $modal.data('bs.modal')
assert.strictEqual(modal._isShown, false)
done()
}, 10)
})
.bootstrapModal({
backdrop: 'static',
keyboard: true
})
})
QUnit.test('should not close modal when escape key is pressed with keyboard = false and backdrop = static', function (assert) {
assert.expect(1)
var done = assert.async()
var $modal = $('<div class="modal" data-backdrop="static" data-keyboard="false"><div class="modal-dialog" /></div>').appendTo('#qunit-fixture')
$modal.on('shown.bs.modal', function () {
$modal.trigger($.Event('keydown', {
which: 27
}))
setTimeout(function () {
var modal = $modal.data('bs.modal')
assert.strictEqual(modal._isShown, true)
done()
}, 10)
})
.on('hidden.bs.modal', function () {
assert.strictEqual(false, true, 'should not hide the modal')
})
.bootstrapModal({
backdrop: 'static',
keyboard: false
})
})
})