Skip to content

Commit

Permalink
fix: complete the promise when calling showValidationMessage() inside…
Browse files Browse the repository at this point in the history
… preConfirm (#2409)
  • Loading branch information
limonte committed Feb 21, 2022
1 parent 2d37516 commit e2384a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cypress/integration/params/preConfirm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ describe('preConfirm', () => {
Swal.clickConfirm()
expect(Swal.isVisible()).to.be.true
})

it('should complete the promise when calling showValidationMessage() inside preConfirm', (done) => {
Swal.fire({
showCancelButton: true,
preConfirm: () => {
Swal.showValidationMessage('Now click the cancel button')
},
didOpen: () => {
Swal.clickConfirm()
},
}).then(() => {
done()
})
Swal.clickCancel()
})
})
3 changes: 3 additions & 0 deletions src/buttons-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { asPromise, capitalizeFirstLetter, error } from './utils/utils.js'
import { showLoading } from './staticMethods/showLoading.js'
import { DismissReason } from './utils/DismissReason.js'
import privateProps from './privateProps.js'
import { handleAwaitingPromise } from './instanceMethods.js'

export const handleConfirmButtonClick = (instance) => {
const innerParams = privateProps.innerParams.get(instance)
Expand Down Expand Up @@ -86,6 +87,7 @@ const deny = (instance, value) => {
.then((preDenyValue) => {
if (preDenyValue === false) {
instance.hideLoading()
handleAwaitingPromise(instance)
} else {
instance.closePopup({ isDenied: true, value: typeof preDenyValue === 'undefined' ? value : preDenyValue })
}
Expand Down Expand Up @@ -121,6 +123,7 @@ const confirm = (instance, value) => {
.then((preConfirmValue) => {
if (isVisible(getValidationMessage()) || preConfirmValue === false) {
instance.hideLoading()
handleAwaitingPromise(instance)
} else {
succeedWith(instance, typeof preConfirmValue === 'undefined' ? value : preConfirmValue)
}
Expand Down
2 changes: 1 addition & 1 deletion src/instanceMethods/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function rejectPromise(error) {
}
}

const handleAwaitingPromise = (instance) => {
export const handleAwaitingPromise = (instance) => {
if (instance.isAwaitingPromise()) {
privateProps.awaitingPromise.delete(instance)
// The instance might have been previously partly destroyed, we must resume the destroy process in this case #2335
Expand Down

0 comments on commit e2384a6

Please sign in to comment.