Skip to content

Commit

Permalink
Replace deprecated e.keyCode with e.key
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Oct 7, 2017
1 parent 269ee94 commit 5f7fc43
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions qunit/tests.js
Expand Up @@ -127,7 +127,7 @@ QUnit.test('esc key', function (assert) {
)

$(document).trigger($.Event('keydown', {
keyCode: 27
key: 'Escape'
}))
})

Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 13 additions & 19 deletions src/sweetalert2.js
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom.js
Expand Up @@ -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()
}
Expand Down

0 comments on commit 5f7fc43

Please sign in to comment.