Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): showLoading() #2595

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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