Skip to content

Commit

Permalink
fix: remove innerParams from handleTab() and setFocus() (#2570)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Dec 20, 2022
1 parent 0968cc4 commit ac79cfc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 45 deletions.
10 changes: 5 additions & 5 deletions cypress/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ export const dispatchCustomEvent = (elem, eventName, eventDetail = {}) => {
}

export const triggerKeydownEvent = (target, key, params = {}) => {
const e = document.createEvent('HTMLEvents')
e.key = key
e.initEvent(
const event = document.createEvent('HTMLEvents')
event.key = key
event.initEvent(
'keydown',
true, // bubbles
true // cancelable
)
for (const param in params) {
e[param] = params[param]
event[param] = params[param]
}
target.dispatchEvent(e)
target.dispatchEvent(event)
}

export const ensureClosed = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/SweetAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const initFocus = (domCache, innerParams) => {
}

if (!focusButton(domCache, innerParams)) {
setFocus(innerParams, -1, 1)
setFocus(-1, 1)
}
}

Expand Down
62 changes: 30 additions & 32 deletions src/keydown-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ export const addKeydownHandler = (instance, globalState, innerParams, dismissWit
}

/**
* @param {SweetAlertOptions} innerParams
* @param {number} index
* @param {number} increment
*/
export const setFocus = (innerParams, index, increment) => {
export const setFocus = (index, increment) => {
const focusableElements = dom.getFocusableElements()
// search for visible elements and select the next possible match
if (focusableElements.length) {
Expand Down Expand Up @@ -68,10 +67,10 @@ const arrowKeysPreviousButton = ['ArrowLeft', 'ArrowUp']

/**
* @param {SweetAlert2} instance
* @param {KeyboardEvent} e
* @param {KeyboardEvent} event
* @param {Function} dismissWith
*/
const keydownHandler = (instance, e, dismissWith) => {
const keydownHandler = (instance, event, dismissWith) => {
const innerParams = privateProps.innerParams.get(instance)

if (!innerParams) {
Expand All @@ -82,67 +81,66 @@ const keydownHandler = (instance, e, dismissWith) => {
// https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event#ignoring_keydown_during_ime_composition
// https://github.com/sweetalert2/sweetalert2/issues/720
// https://github.com/sweetalert2/sweetalert2/issues/2406
if (e.isComposing || e.keyCode === 229) {
if (event.isComposing || event.keyCode === 229) {
return
}

if (innerParams.stopKeydownPropagation) {
e.stopPropagation()
event.stopPropagation()
}

// ENTER
if (e.key === 'Enter') {
handleEnter(instance, e, innerParams)
if (event.key === 'Enter') {
handleEnter(instance, event, innerParams)
}

// TAB
else if (e.key === 'Tab') {
handleTab(e, innerParams)
else if (event.key === 'Tab') {
handleTab(event)
}

// ARROWS - switch focus between buttons
else if ([...arrowKeysNextButton, ...arrowKeysPreviousButton].includes(e.key)) {
handleArrows(e.key)
else if ([...arrowKeysNextButton, ...arrowKeysPreviousButton].includes(event.key)) {
handleArrows(event.key)
}

// ESC
else if (e.key === 'Escape') {
handleEsc(e, innerParams, dismissWith)
else if (event.key === 'Escape') {
handleEsc(event, innerParams, dismissWith)
}
}

/**
* @param {SweetAlert2} instance
* @param {KeyboardEvent} e
* @param {KeyboardEvent} event
* @param {SweetAlertOptions} innerParams
*/
const handleEnter = (instance, e, innerParams) => {
const handleEnter = (instance, event, innerParams) => {
// https://github.com/sweetalert2/sweetalert2/issues/2386
if (!callIfFunction(innerParams.allowEnterKey)) {
return
}

if (
e.target &&
event.target &&
instance.getInput() &&
e.target instanceof HTMLElement &&
e.target.outerHTML === instance.getInput().outerHTML
event.target instanceof HTMLElement &&
event.target.outerHTML === instance.getInput().outerHTML
) {
if (['textarea', 'file'].includes(innerParams.input)) {
return // do not submit
}

clickConfirm()
e.preventDefault()
event.preventDefault()
}
}

/**
* @param {KeyboardEvent} e
* @param {SweetAlertOptions} innerParams
* @param {KeyboardEvent} event
*/
const handleTab = (e, innerParams) => {
const targetElement = e.target
const handleTab = (event) => {
const targetElement = event.target

const focusableElements = dom.getFocusableElements()
let btnIndex = -1
Expand All @@ -154,17 +152,17 @@ const handleTab = (e, innerParams) => {
}

// Cycle to the next button
if (!e.shiftKey) {
setFocus(innerParams, btnIndex, 1)
if (!event.shiftKey) {
setFocus(btnIndex, 1)
}

// Cycle to the prev button
else {
setFocus(innerParams, btnIndex, -1)
setFocus(btnIndex, -1)
}

e.stopPropagation()
e.preventDefault()
event.stopPropagation()
event.preventDefault()
}

/**
Expand Down Expand Up @@ -197,13 +195,13 @@ const handleArrows = (key) => {
}

/**
* @param {KeyboardEvent} e
* @param {KeyboardEvent} event
* @param {SweetAlertOptions} innerParams
* @param {Function} dismissWith
*/
const handleEsc = (e, innerParams, dismissWith) => {
const handleEsc = (event, innerParams, dismissWith) => {
if (callIfFunction(innerParams.allowEscapeKey)) {
e.preventDefault()
event.preventDefault()
dismissWith(DismissReason.esc)
}
}
14 changes: 7 additions & 7 deletions src/utils/iosFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ const lockBodyScroll = () => {
const container = dom.getContainer()
let preventTouchMove
/**
* @param {TouchEvent} e
* @param {TouchEvent} event
*/
container.ontouchstart = (e) => {
preventTouchMove = shouldPreventTouchMove(e)
container.ontouchstart = (event) => {
preventTouchMove = shouldPreventTouchMove(event)
}
/**
* @param {TouchEvent} e
* @param {TouchEvent} event
*/
container.ontouchmove = (e) => {
container.ontouchmove = (event) => {
if (preventTouchMove) {
e.preventDefault()
e.stopPropagation()
event.preventDefault()
event.stopPropagation()
}
}
}
Expand Down

0 comments on commit ac79cfc

Please sign in to comment.