Skip to content

Commit

Permalink
fix(types): showLoading() (#2595)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte authored Jan 24, 2023
1 parent 49ecc95 commit 6b4aa39
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/keydown-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,9 @@ const handleArrows = (key) => {
const confirmButton = dom.getConfirmButton()
const denyButton = dom.getDenyButton()
const cancelButton = dom.getCancelButton()
if (
document.activeElement instanceof HTMLElement &&
![confirmButton, denyButton, cancelButton].includes(document.activeElement)
) {
/** @type HTMLElement[] */
const buttons = [confirmButton, denyButton, cancelButton]
if (document.activeElement instanceof HTMLElement && !buttons.includes(document.activeElement)) {
return
}
const sibling = arrowKeysNextButton.includes(key) ? 'nextElementSibling' : 'previousElementSibling'
Expand Down
6 changes: 6 additions & 0 deletions src/staticMethods/showLoading.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as dom from '../utils/dom/index.js'
/**
* Shows loader (spinner), this is useful with AJAX requests.
* By default the loader be shown instead of the "Confirm" button.
*
* @param {HTMLButtonElement} [buttonToReplace]
*/
const showLoading = (buttonToReplace) => {
let popup = dom.getPopup()
Expand All @@ -26,6 +28,10 @@ const showLoading = (buttonToReplace) => {
popup.focus()
}

/**
* @param {HTMLElement} popup
* @param {HTMLButtonElement} [buttonToReplace]
*/
const replaceButton = (popup, buttonToReplace) => {
const actions = dom.getActions()
const loader = dom.getLoader()
Expand Down
19 changes: 11 additions & 8 deletions src/utils/dom/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,32 @@ export const getProgressSteps = () => elementByClass(swalClasses['progress-steps
export const getValidationMessage = () => elementByClass(swalClasses['validation-message'])

/**
* @returns {HTMLElement | null}
* @returns {HTMLButtonElement | null}
*/
export const getConfirmButton = () => elementBySelector(`.${swalClasses.actions} .${swalClasses.confirm}`)
export const getConfirmButton = () =>
/** @type {HTMLButtonElement} */ (elementBySelector(`.${swalClasses.actions} .${swalClasses.confirm}`))

/**
* @returns {HTMLElement | null}
* @returns {HTMLButtonElement | null}
*/
export const getDenyButton = () => elementBySelector(`.${swalClasses.actions} .${swalClasses.deny}`)
export const getCancelButton = () =>
/** @type {HTMLButtonElement} */ (elementBySelector(`.${swalClasses.actions} .${swalClasses.cancel}`))

/**
* @returns {HTMLElement | null}
* @returns {HTMLButtonElement | null}
*/
export const getInputLabel = () => elementByClass(swalClasses['input-label'])
export const getDenyButton = () =>
/** @type {HTMLButtonElement} */ (elementBySelector(`.${swalClasses.actions} .${swalClasses.deny}`))

/**
* @returns {HTMLElement | null}
*/
export const getLoader = () => elementBySelector(`.${swalClasses.loader}`)
export const getInputLabel = () => elementByClass(swalClasses['input-label'])

/**
* @returns {HTMLElement | null}
*/
export const getCancelButton = () => elementBySelector(`.${swalClasses.actions} .${swalClasses.cancel}`)
export const getLoader = () => elementBySelector(`.${swalClasses.loader}`)

/**
* @returns {HTMLElement | null}
Expand Down
2 changes: 1 addition & 1 deletion sweetalert2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ declare module 'sweetalert2' {
* Swal.showLoading(Swal.getDenyButton())
* ```
*/
function showLoading(buttonToReplace: HTMLButtonElement | null): void
function showLoading(buttonToReplace?: HTMLButtonElement): void

/**
* Hides loader and shows back the button which was hidden by .showLoading()
Expand Down

0 comments on commit 6b4aa39

Please sign in to comment.