Skip to content

Commit

Permalink
fix: types for SweetAlert class
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed May 28, 2023
1 parent 24ed5b1 commit 166077e
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 52 deletions.
12 changes: 11 additions & 1 deletion cypress/e2e/api.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ describe('API', () => {
it('instance properties and methods', () => {
const params = { input: 'text', inputValue: 'foo' }
const swal = Swal.fire(params)
expect(Object.keys(swal)).to.be.eql(['params'])
expect(Object.keys(swal)).contain.members([
'params',
'disableButtons',
'enableButtons',
'getInput',
'disableInput',
'enableInput',
'showValidationMessage',
'resetValidationMessage',
'close',
])
expect(swal.params).to.be.eql(params)
expect(swal.getInput().value).to.equal('foo')
})
Expand Down
29 changes: 23 additions & 6 deletions src/SweetAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import defaultParams, { showWarningsForParams } from './utils/params.js'
import setParameters from './utils/setParameters.js'
import { callIfFunction } from './utils/utils.js'

/** @type {SweetAlert} */
let currentInstance

class SweetAlert {
export class SweetAlert {
constructor(...args) {
// Prevent run in Node env
if (typeof window === 'undefined') {
Expand Down Expand Up @@ -80,6 +81,25 @@ class SweetAlert {
return swalPromise(currentInstance, domCache, innerParams)
}

disableButtons = instanceMethods.disableButtons
enableButtons = instanceMethods.enableButtons
getInput = instanceMethods.getInput
disableInput = instanceMethods.disableInput
enableInput = instanceMethods.enableInput
hideLoading = instanceMethods.hideLoading
disableLoading = instanceMethods.disableLoading
showValidationMessage = instanceMethods.showValidationMessage
resetValidationMessage = instanceMethods.resetValidationMessage
close = instanceMethods.close
closePopup = instanceMethods.closePopup
closeModal = instanceMethods.closeModal
closeToast = instanceMethods.closeToast
isAwaitingPromise = instanceMethods.isAwaitingPromise
handleAwaitingPromise = instanceMethods.handleAwaitingPromise
rejectPromise = instanceMethods.rejectPromise
update = instanceMethods.update
_destroy = instanceMethods._destroy

// `catch` cannot be the name of a module export, so we define our thenable methods here instead
then(onFulfilled) {
const promise = privateProps.promise.get(this)
Expand All @@ -93,7 +113,7 @@ class SweetAlert {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {DomCache} domCache
* @param {SweetAlertOptions} innerParams
* @returns {Promise}
Expand Down Expand Up @@ -162,7 +182,7 @@ const prepareParams = (userParams, mixinParams) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @returns {DomCache}
*/
const populateDomCache = (instance) => {
Expand Down Expand Up @@ -280,9 +300,6 @@ if (typeof window !== 'undefined' && /^ru\b/.test(navigator.language) && locatio
}
}

// Assign instance methods from src/instanceMethods/*.js to prototype
Object.assign(SweetAlert.prototype, instanceMethods)

// Assign static methods from src/staticMethods/*.js to constructor
Object.assign(SweetAlert, staticMethods)

Expand Down
18 changes: 9 additions & 9 deletions src/buttons-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getInputValue } from './utils/dom/inputUtils.js'
import { asPromise, capitalizeFirstLetter, error } from './utils/utils.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
export const handleConfirmButtonClick = (instance) => {
const innerParams = privateProps.innerParams.get(instance)
Expand All @@ -21,7 +21,7 @@ export const handleConfirmButtonClick = (instance) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
export const handleDenyButtonClick = (instance) => {
const innerParams = privateProps.innerParams.get(instance)
Expand All @@ -34,7 +34,7 @@ export const handleDenyButtonClick = (instance) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {Function} dismissWith
*/
export const handleCancelButtonClick = (instance, dismissWith) => {
Expand All @@ -43,7 +43,7 @@ export const handleCancelButtonClick = (instance, dismissWith) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {'confirm' | 'deny'} type
*/
const handleConfirmOrDenyWithInput = (instance, type) => {
Expand All @@ -66,7 +66,7 @@ const handleConfirmOrDenyWithInput = (instance, type) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {string | number | File | FileList | null} inputValue
* @param {'confirm' | 'deny'} type
*/
Expand All @@ -90,7 +90,7 @@ const handleInputValidator = (instance, inputValue, type) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {any} value
*/
const deny = (instance, value) => {
Expand Down Expand Up @@ -121,7 +121,7 @@ const deny = (instance, value) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {any} value
*/
const succeedWith = (instance, value) => {
Expand All @@ -130,7 +130,7 @@ const succeedWith = (instance, value) => {

/**
*
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {string} error
*/
const rejectWith = (instance, error) => {
Expand All @@ -140,7 +140,7 @@ const rejectWith = (instance, error) => {

/**
*
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {any} value
*/
const confirm = (instance, value) => {
Expand Down
6 changes: 3 additions & 3 deletions src/instanceMethods/_destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function _destroy() {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
const disposeSwal = (instance) => {
disposeWeakMaps(instance)
Expand All @@ -42,7 +42,7 @@ const disposeSwal = (instance) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
const disposeWeakMaps = (instance) => {
// If the current instance is awaiting a promise result, we keep the privateMethods to call them once the promise result is retrieved #2335
Expand All @@ -58,7 +58,7 @@ const disposeWeakMaps = (instance) => {

/**
* @param {object} obj
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
const unsetWeakMaps = (obj, instance) => {
for (const i in obj) {
Expand Down
10 changes: 5 additions & 5 deletions src/instanceMethods/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { undoIOSfix } from '../utils/iosFix.js'
import { undoScrollbar } from '../utils/scrollbarFix.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {HTMLElement} container
* @param {boolean} returnFocus
* @param {Function} didClose
Expand Down Expand Up @@ -120,7 +120,7 @@ export function rejectPromise(error) {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
*/
export const handleAwaitingPromise = (instance) => {
// @ts-ignore
Expand Down Expand Up @@ -159,7 +159,7 @@ const prepareResolveValue = (resolveValue) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {HTMLElement} popup
* @param {SweetAlertOptions} innerParams
*/
Expand All @@ -181,7 +181,7 @@ const handlePopupAnimation = (instance, popup, innerParams) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {HTMLElement} popup
* @param {HTMLElement} container
* @param {boolean} returnFocus
Expand All @@ -204,7 +204,7 @@ const animatePopup = (instance, popup, container, returnFocus, didClose) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {Function} didClose
*/
const triggerDidCloseAndDispose = (instance, didClose) => {
Expand Down
2 changes: 1 addition & 1 deletion src/instanceMethods/enable-disable-elements.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import privateProps from '../privateProps.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {string[]} buttons
* @param {boolean} disabled
*/
Expand Down
9 changes: 4 additions & 5 deletions src/instanceMethods/getInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import * as dom from '../utils/dom/index.js'
/**
* Gets the input DOM node, this method works with input parameter.
*
* @param {SweetAlert2} instance
* @returns {HTMLElement | null}
* @returns {HTMLInputElement | null}
*/
export function getInput(instance) {
const innerParams = privateProps.innerParams.get(instance || this)
const domCache = privateProps.domCache.get(instance || this)
export function getInput() {
const innerParams = privateProps.innerParams.get(this)
const domCache = privateProps.domCache.get(this)
if (!domCache) {
return null
}
Expand Down
6 changes: 3 additions & 3 deletions src/keydown-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const removeKeydownHandler = (globalState) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {GlobalState} globalState
* @param {SweetAlertOptions} innerParams
* @param {*} dismissWith
Expand Down Expand Up @@ -66,7 +66,7 @@ const arrowKeysNextButton = ['ArrowRight', 'ArrowDown']
const arrowKeysPreviousButton = ['ArrowLeft', 'ArrowUp']

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {KeyboardEvent} event
* @param {Function} dismissWith
*/
Expand Down Expand Up @@ -111,7 +111,7 @@ const keydownHandler = (instance, event, dismissWith) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {KeyboardEvent} event
* @param {SweetAlertOptions} innerParams
*/
Expand Down
2 changes: 1 addition & 1 deletion src/staticMethods/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* const {value: lastName} = await TextPrompt('What is your last name?')
*
* @param {SweetAlertOptions} mixinParams
* @returns {SweetAlert2}
* @returns {SweetAlert}
*/
export function mixin(mixinParams) {
class MixinSwal extends this {
Expand Down
4 changes: 2 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef { import('sweetalert2').default } SweetAlert2
* @typedef { import('./SweetAlert').SweetAlert } SweetAlert
* @typedef { import('sweetalert2').SweetAlertOptions } SweetAlertOptions
* @typedef { import('sweetalert2').SweetAlertIcon } SweetAlertIcon
* @typedef { import('sweetalert2').SweetAlertInput } SweetAlertInput
Expand All @@ -12,7 +12,7 @@

/**
* @typedef GlobalState
* @property {SweetAlert2} [currentInstance]
* @property {SweetAlert} [currentInstance]
* @property {Element} [previousActiveElement]
* @property {Timer} [timeout]
* @property {NodeJS.Timeout} [restoreFocusTimeout]
Expand Down
8 changes: 4 additions & 4 deletions src/utils/dom/inputUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as dom from './index.js'
*/

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const handleInputOptionsAndValue = (instance, params) => {
Expand All @@ -25,7 +25,7 @@ export const handleInputOptionsAndValue = (instance, params) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} innerParams
* @returns {string | number | File | FileList | null}
*/
Expand Down Expand Up @@ -66,7 +66,7 @@ const getFileValue = (input) =>
input.files.length ? (input.getAttribute('multiple') !== null ? input.files : input.files[0]) : null

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
const handleInputOptions = (instance, params) => {
Expand All @@ -91,7 +91,7 @@ const handleInputOptions = (instance, params) => {
}

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
const handleInputValue = (instance, params) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { renderProgressSteps } from './renderProgressSteps.js'
import { renderTitle } from './renderTitle.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const render = (instance, params) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/renderActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as dom from '../../dom/index.js'
import { capitalizeFirstLetter } from '../../utils.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const renderActions = (instance, params) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/renderCloseButton.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as dom from '../../dom/index.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const renderCloseButton = (instance, params) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/renderContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as dom from '../../dom/index.js'
import { warn } from '../../utils.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const renderContainer = (instance, params) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom/renderers/renderContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as dom from '../../dom/index.js'
import { renderInput } from './renderInput.js'

/**
* @param {SweetAlert2} instance
* @param {SweetAlert} instance
* @param {SweetAlertOptions} params
*/
export const renderContent = (instance, params) => {
Expand Down
Loading

0 comments on commit 166077e

Please sign in to comment.