Skip to content

Commit

Permalink
feat: showLoading(buttonToReplace) (#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Nov 11, 2020
1 parent 665b236 commit 73c0582
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/instanceMethods/hideLoading.js
Expand Up @@ -3,7 +3,7 @@ import { swalClasses } from '../utils/classes.js'
import privateProps from '../privateProps.js'

/**
* Enables buttons and hide loader.
* Hides loader and shows back the button which was hidden by .showLoading()
*/
function hideLoading () {
// do nothing if popup is closed
Expand All @@ -13,9 +13,10 @@ function hideLoading () {
}
const domCache = privateProps.domCache.get(this)
dom.hide(domCache.loader)
if (innerParams.showConfirmButton) {
dom.show(domCache.confirmButton, 'inline-block')
} else if (!innerParams.showConfirmButton && !innerParams.showCancelButton) {
const buttonToReplace = domCache.popup.getElementsByClassName(domCache.loader.getAttribute('data-button-to-replace'))
if (buttonToReplace.length) {
dom.show(buttonToReplace[0], 'inline-block')
} else if (dom.allButtonsAreHidden()) {
dom.hide(domCache.actions)
}
dom.removeClass([domCache.popup, domCache.actions], swalClasses.loading)
Expand Down
16 changes: 12 additions & 4 deletions src/staticMethods/showLoading.js
Expand Up @@ -3,20 +3,28 @@ import Swal from '../sweetalert2.js'
import { swalClasses } from '../utils/classes.js'

/**
* Show spinner instead of Confirm button
* Shows loader (spinner), this is useful with AJAX requests.
* By default the loader be shown instead of the "Confirm" button.
*/
const showLoading = () => {
const showLoading = (buttonToReplace) => {
let popup = dom.getPopup()
if (!popup) {
Swal.fire()
}
popup = dom.getPopup()
const actions = dom.getActions()
const confirmButton = dom.getConfirmButton()
const loader = dom.getLoader()

if (!buttonToReplace && dom.isVisible(dom.getConfirmButton())) {
buttonToReplace = dom.getConfirmButton()
}

dom.show(actions)
dom.hide(confirmButton)
if (buttonToReplace) {
dom.hide(buttonToReplace)
loader.setAttribute('data-button-to-replace', buttonToReplace.className)
}
loader.parentNode.insertBefore(loader, buttonToReplace)
dom.addClass([popup, actions], swalClasses.loading)

dom.show(loader)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/dom/domUtils.js
@@ -1,4 +1,4 @@
import { getTimerProgressBar } from './getters.js'
import { getTimerProgressBar, getConfirmButton, getDenyButton, getCancelButton } from './getters.js'
import { swalClasses, iconTypes } from '../classes.js'
import { toArray, objectValues, warn } from '../utils.js'

Expand Down Expand Up @@ -155,7 +155,8 @@ export const toggle = (elem, condition, display) => {
// borrowed from jquery $(elem).is(':visible') implementation
export const isVisible = (elem) => !!(elem && (elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length))

/* istanbul ignore next */
export const allButtonsAreHidden = () => !isVisible(getConfirmButton()) && !isVisible(getDenyButton()) && !isVisible(getCancelButton())

export const isScrollable = (elem) => !!(elem.scrollHeight > elem.clientHeight)

// borrowed from https://stackoverflow.com/a/46352119
Expand Down
18 changes: 12 additions & 6 deletions sweetalert2.d.ts
Expand Up @@ -137,17 +137,17 @@ declare module 'sweetalert2' {
/**
* Gets the "Confirm" button.
*/
function getConfirmButton(): HTMLElement | null;
function getConfirmButton(): HTMLButtonElement | null;

/**
* Gets the "Deny" button.
*/
function getDenyButton(): HTMLElement | null;
function getDenyButton(): HTMLButtonElement | null;

/**
* Gets the "Cancel" button.
*/
function getCancelButton(): HTMLElement | null;
function getCancelButton(): HTMLButtonElement | null;

/**
* Gets actions (buttons) wrapper.
Expand Down Expand Up @@ -180,12 +180,18 @@ declare module 'sweetalert2' {
function disableButtons(): void;

/**
* Disables buttons and show loader. This is useful with AJAX requests.
* Shows loader (spinner), this is useful with AJAX requests.
*
* By default the loader be shown instead of the "Confirm" button, but if you want
* another button to be replaced with a loader, just pass it like this:
* ```
* Swal.showLoading(Swal.getDenyButton())
* ```
*/
function showLoading(): void;
function showLoading(buttonToReplace?: HTMLButtonElement): void;

/**
* Enables buttons and hide loader.
* Hides loader and shows back the button which was hidden by .showLoading()
*/
function hideLoading(): void;

Expand Down
2 changes: 2 additions & 0 deletions test/ts/simple-usage.ts
@@ -1,3 +1,5 @@
import Swal from 'sweetalert2'

Swal.fire()

Swal.showLoading(Swal.getConfirmButton())

0 comments on commit 73c0582

Please sign in to comment.