aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorGiovanni Mendoza <[email protected]>2020-01-10 03:06:12 -0600
committerXhmikosR <[email protected]>2020-01-10 11:06:12 +0200
commit954a0b1e6a206e4df910d4799d013f4388a2ef40 (patch)
tree59e1d72bc0452427421b7f856d57eeecd48b6b5e /js
parent4e1fb4fe1a4e968d83d8f62007b9354e2811eb63 (diff)
downloadbootstrap-954a0b1e6a206e4df910d4799d013f4388a2ef40.tar.xz
bootstrap-954a0b1e6a206e4df910d4799d013f4388a2ef40.zip
Close modal with keyboard=true & backdrop=static (#29986)
* Close modal with keyboard=true & backdrop=static
Diffstat (limited to 'js')
-rw-r--r--js/src/modal.js7
-rw-r--r--js/tests/unit/modal.spec.js58
2 files changed, 63 insertions, 2 deletions
diff --git a/js/src/modal.js b/js/src/modal.js
index e2b711e5b..48ff5b854 100644
--- a/js/src/modal.js
+++ b/js/src/modal.js
@@ -306,9 +306,12 @@ class Modal {
}
_setEscapeEvent() {
- if (this._isShown && this._config.keyboard) {
+ if (this._isShown) {
EventHandler.on(this._element, 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()
}
})
diff --git a/js/tests/unit/modal.spec.js b/js/tests/unit/modal.spec.js
index 2edef22b2..b4923ab3f 100644
--- a/js/tests/unit/modal.spec.js
+++ b/js/tests/unit/modal.spec.js
@@ -567,6 +567,64 @@ describe('Modal', () => {
modal.show()
})
+ it('should close modal when escape key is pressed with keyboard = true and backdrop is static', done => {
+ fixtureEl.innerHTML = '<div class="modal" data-backdrop="static"><div class="modal-dialog" /></div>'
+
+ const modalEl = fixtureEl.querySelector('.modal')
+ const modal = new Modal(modalEl, {
+ backdrop: 'static',
+ keyboard: true
+ })
+
+ const shownCallback = () => {
+ setTimeout(() => {
+ expect(modal._isShown).toEqual(false)
+ done()
+ }, 10)
+ }
+
+ modalEl.addEventListener('shown.bs.modal', () => {
+ const keydownEscape = createEvent('keydown')
+ keydownEscape.which = 27
+
+ modalEl.dispatchEvent(keydownEscape)
+ shownCallback()
+ })
+
+ modal.show()
+ })
+
+ it('should not close modal when escape key is pressed with keyboard = false and backdrop = static', done => {
+ fixtureEl.innerHTML = '<div class="modal" data-backdrop="static" data-keyboard="false"><div class="modal-dialog" /></div>'
+
+ const modalEl = fixtureEl.querySelector('.modal')
+ const modal = new Modal(modalEl, {
+ backdrop: 'static',
+ keyboard: false
+ })
+
+ const shownCallback = () => {
+ setTimeout(() => {
+ expect(modal._isShown).toEqual(true)
+ done()
+ }, 10)
+ }
+
+ modalEl.addEventListener('shown.bs.modal', () => {
+ const keydownEscape = createEvent('keydown')
+ keydownEscape.which = 27
+
+ modalEl.dispatchEvent(keydownEscape)
+ shownCallback()
+ })
+
+ modalEl.addEventListener('hidden.bs.modal', () => {
+ throw new Error('Should not hide a modal')
+ })
+
+ modal.show()
+ })
+
it('should not adjust the inline body padding when it does not overflow', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" /></div>'