Skip to content

Commit

Permalink
BREAKING CHANGE: remove useRejections and expectRejections (#1362)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove useRejections and expectRejections (#1362)
  • Loading branch information
limonte committed Jan 18, 2019
1 parent d05bf33 commit f050caf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 252 deletions.
77 changes: 20 additions & 57 deletions src/instanceMethods/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,11 @@ export function _main (userParams) {
// functions to handle all resolving/rejecting/settling
const succeedWith = (value) => {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose) // TODO: make closePopup an *instance* method
if (innerParams.useRejections) {
resolve(value)
} else {
resolve({ value })
}
resolve({ value })
}
const dismissWith = (dismiss) => {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose)
if (innerParams.useRejections) {
reject(dismiss)
} else {
resolve({ dismiss })
}
resolve({ dismiss })
}
const errorWith = (error) => {
constructor.closePopup(innerParams.onClose, innerParams.onAfterClose)
Expand Down Expand Up @@ -107,28 +99,16 @@ export function _main (userParams) {
if (innerParams.preConfirm) {
this.resetValidationMessage()
const preConfirmPromise = Promise.resolve().then(() => innerParams.preConfirm(value, innerParams.extraParams))
if (innerParams.expectRejections) {
preConfirmPromise.then(
(preConfirmValue) => succeedWith(preConfirmValue || value),
(validationMessage) => {
preConfirmPromise.then(
(preConfirmValue) => {
if (dom.isVisible(domCache.validationMessage) || preConfirmValue === false) {
this.hideLoading()
if (validationMessage) {
this.showValidationMessage(validationMessage)
}
} else {
succeedWith(preConfirmValue || value)
}
)
} else {
preConfirmPromise.then(
(preConfirmValue) => {
if (dom.isVisible(domCache.validationMessage) || preConfirmValue === false) {
this.hideLoading()
} else {
succeedWith(preConfirmValue || value)
}
},
(error) => errorWith(error)
)
}
},
(error) => errorWith(error)
)
} else {
succeedWith(value)
}
Expand All @@ -152,35 +132,18 @@ export function _main (userParams) {
if (innerParams.inputValidator) {
this.disableInput()
const validationPromise = Promise.resolve().then(() => innerParams.inputValidator(inputValue, innerParams.extraParams))
if (innerParams.expectRejections) {
validationPromise.then(
() => {
this.enableButtons()
this.enableInput()
validationPromise.then(
(validationMessage) => {
this.enableButtons()
this.enableInput()
if (validationMessage) {
this.showValidationMessage(validationMessage)
} else {
confirm(inputValue)
},
(validationMessage) => {
this.enableButtons()
this.enableInput()
if (validationMessage) {
this.showValidationMessage(validationMessage)
}
}
)
} else {
validationPromise.then(
(validationMessage) => {
this.enableButtons()
this.enableInput()
if (validationMessage) {
this.showValidationMessage(validationMessage)
} else {
confirm(inputValue)
}
},
error => errorWith(error)
)
}
},
error => errorWith(error)
)
} else if (!this.getInput().checkValidity()) {
this.enableButtons()
this.showValidationMessage(innerParams.validationMessage)
Expand Down
6 changes: 1 addition & 5 deletions src/utils/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ const defaultParams = {
onBeforeOpen: null,
onAfterClose: null,
onOpen: null,
onClose: null,
useRejections: false,
expectRejections: false
onClose: null
}

export const deprecatedParams = [
'useRejections',
'expectRejections',
'extraParams'
]

Expand Down
2 changes: 1 addition & 1 deletion src/utils/setParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function setParameters (params) {
if (!params.inputValidator) {
Object.keys(defaultInputValidators).forEach((key) => {
if (params.input === key) {
params.inputValidator = params.expectRejections ? defaultInputValidators[key] : sweetAlert.adaptInputValidator(defaultInputValidators[key])
params.inputValidator = sweetAlert.adaptInputValidator(defaultInputValidators[key])
}
})
}
Expand Down
17 changes: 1 addition & 16 deletions sweetalert2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ declare module 'sweetalert2' {
* timer: 2000
* })
*/
function swal(settings: SweetAlertOptions & { useRejections?: false }): Promise<SweetAlertResult>;

/**
* @deprecated
* swal() overload for legacy alerts that use { useRejections: true }.
*/
function swal(settings: SweetAlertOptions & { useRejections: true }): Promise<any>;
function swal(settings: SweetAlertOptions): Promise<SweetAlertResult>;

/**
* A namespace inside the default function, containing utility function for controlling the currently-displayed
Expand Down Expand Up @@ -861,15 +855,6 @@ declare module 'sweetalert2' {
* @default null
*/
onClose?: (modalElement: HTMLElement) => void;

/**
* Determines whether given `inputValidator` and `preConfirm` functions should be expected to to signal
* validation message by rejecting, or by their respective means (see documentation for each option).
*
* @default false
* @deprecated
*/
expectRejections?: boolean;
}

export default swal;
Expand Down
171 changes: 0 additions & 171 deletions test/qunit/deprecated.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/qunit/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,15 +690,15 @@ QUnit.test('on errors in *async* user-defined functions, cleans up and propagate
}

// inputValidator
const rejectedPromise = Swal({ input: 'text', expectRejections: false, inputValidator: erroringFunction })
const rejectedPromise = Swal({ input: 'text', inputValidator: erroringFunction })
Swal.clickConfirm()
rejectedPromise.catch((error) => {
assert.equal(error, expectedError) // error is bubbled up back to user code
setTimeout(() => {
assert.notOk(Swal.isVisible()) // display is cleaned up

// preConfirm
const rejectedPromise = Swal({ expectRejections: false, preConfirm: erroringFunction })
const rejectedPromise = Swal({ preConfirm: erroringFunction })
Swal.clickConfirm()
rejectedPromise.catch((error) => {
assert.equal(error, expectedError) // error is bubbled up back to user code
Expand Down

0 comments on commit f050caf

Please sign in to comment.