From 5f7fc43768e403b79d17caed4cca35ecd4dff11f Mon Sep 17 00:00:00 2001 From: Limon Monte Date: Sat, 7 Oct 2017 22:24:58 +0300 Subject: [PATCH] Replace deprecated e.keyCode with e.key --- qunit/tests.js | 4 ++-- src/sweetalert2.js | 32 +++++++++++++------------------- src/utils/dom.js | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/qunit/tests.js b/qunit/tests.js index 110a21712..449429b1d 100644 --- a/qunit/tests.js +++ b/qunit/tests.js @@ -127,7 +127,7 @@ QUnit.test('esc key', function (assert) { ) $(document).trigger($.Event('keydown', { - keyCode: 27 + key: 'Escape' })) }) @@ -647,7 +647,7 @@ QUnit.test('esc key no rejections test', function (assert) { ) $(document).trigger($.Event('keydown', { - keyCode: 27 + key: 'Escape' })) }) QUnit.test('close button no rejections test', function (assert) { diff --git a/src/sweetalert2.js b/src/sweetalert2.js index 22b5f9883..b7f08d35a 100644 --- a/src/sweetalert2.js +++ b/src/sweetalert2.js @@ -646,26 +646,20 @@ const sweetAlert = (...args) => { const handleKeyDown = (event) => { const e = event || window.event - const keyCode = e.keyCode || e.which - if ([9, 13, 32, 27, 37, 38, 39, 40].indexOf(keyCode) === -1) { - // Don't do work on keys we don't care about. - return - } - - const targetElement = e.target || e.srcElement - - const focusableElements = dom.getFocusableElements(params.focusCancel) - let btnIndex = -1 // Find the button - note, this is a nodelist, not an array. - for (let i = 0; i < focusableElements.length; i++) { - if (targetElement === focusableElements[i]) { - btnIndex = i - break + // TAB + if (e.key === 'Tab') { + const targetElement = e.target || e.srcElement + + const focusableElements = dom.getFocusableElements(params.focusCancel) + let btnIndex = -1 // Find the button - note, this is a nodelist, not an array. + for (let i = 0; i < focusableElements.length; i++) { + if (targetElement === focusableElements[i]) { + btnIndex = i + break + } } - } - // TAB - if (keyCode === 9) { if (!e.shiftKey) { // Cycle to the next button setFocus(btnIndex, 1) @@ -677,7 +671,7 @@ const sweetAlert = (...args) => { e.preventDefault() // ARROWS - switch focus between buttons - } else if (keyCode === 37 || keyCode === 38 || keyCode === 39 || keyCode === 40) { + } else if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'Arrowdown'].includes(e.key)) { // focus Cancel button if Confirm button is currently focused if (document.activeElement === confirmButton && dom.isVisible(cancelButton)) { cancelButton.focus() @@ -687,7 +681,7 @@ const sweetAlert = (...args) => { } // ESC - } else if (keyCode === 27 && params.allowEscapeKey === true) { + } else if (e.key === 'Escape' && params.allowEscapeKey === true) { sweetAlert.closeModal(params.onClose) if (params.useRejections) { reject('esc') diff --git a/src/utils/dom.js b/src/utils/dom.js index 6458e4a86..b9783aac7 100644 --- a/src/utils/dom.js +++ b/src/utils/dom.js @@ -46,7 +46,7 @@ export const init = (params) => { input.onkeydown = (event) => { setTimeout(() => { - if (event.keyCode === 13 && params.allowEnterKey) { + if (event.key === 'Enter' && params.allowEnterKey) { event.stopPropagation() sweetAlert.clickConfirm() }