Skip to content

Commit

Permalink
feat(api): add update() method (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Jan 18, 2019
1 parent ca9d197 commit 56a137d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/instanceMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './instanceMethods/enable-disable-elements.js'
export * from './instanceMethods/show-reset-validation-error.js'
export * from './instanceMethods/progress-steps.js'
export * from './instanceMethods/_main.js'

export * from './instanceMethods/update.js'
44 changes: 44 additions & 0 deletions src/instanceMethods/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as dom from '../../src/utils/dom/index'
import { warn } from '../../src/utils/utils'
import sweetAlert from '../sweetalert2'
import privateProps from '../privateProps'

/**
* Updates popup options.
*/
export function update (params) {
const validUpdatableParams = {}

// assign valid params from `params` to `defaults`
Object.keys(params).forEach(param => {
if (sweetAlert.isUpdatableParameter(param)) {
validUpdatableParams[param] = params[param]
} else {
warn(`Invalid parameter to update: "${param}". Updatable params are listed here: TODO (@limonte) add link`)
}
})

const innerParams = privateProps.innerParams.get(this)
const updatedParams = Object.assign({}, innerParams, validUpdatableParams)

// Actions
dom.renderActions(updatedParams)

// Content
dom.renderContent(updatedParams)

// Icon
dom.renderIcon(updatedParams)

// Image
dom.renderImage(updatedParams)

// Progress steps
dom.renderProgressSteps(updatedParams)

// Title
dom.renderTitle(updatedParams)


privateProps.innerParams.set(this, updatedParams)
}
1 change: 1 addition & 0 deletions src/staticMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './staticMethods/showLoading.js'
export * from './staticMethods/timer.js'
export {
isValidParameter,
isUpdatableParameter,
isDeprecatedParameter
} from './utils/params.js'
33 changes: 33 additions & 0 deletions src/utils/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,39 @@ export const isValidParameter = (paramName) => {
return defaultParams.hasOwnProperty(paramName)
}

/**
* Is valid parameter for Swal.update() method
* @param {String} paramName
*/
export const isUpdatableParameter = (paramName) => {
return [
'title',
'titleText',
'text',
'html',
'type',
'showConfirmButton',
'showCancelButton',
'confirmButtonText',
'confirmButtonAriaLabel',
'confirmButtonColor',
'confirmButtonClass',
'cancelButtonText',
'cancelButtonAriaLabel',
'cancelButtonColor',
'cancelButtonClass',
'buttonsStyling',
'reverseButtons',
'imageUrl',
'imageWidth',
'imageHeigth',
'imageAlt',
'imageClass',
'progressSteps',
'currentProgressStep'
].indexOf(paramName) !== -1
}

/**
* Is deprecated parameter
* @param {String} paramName
Expand Down
18 changes: 18 additions & 0 deletions sweetalert2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ declare module 'sweetalert2' {
*/
function isVisible(): boolean;

/**
* Updates popup options.
* See the SweetAlertOptions interface for the list of accepted fields and values.
*
* ex.
* swal.update({
* type: 'error'
* })
*/
function update(newSettings: SweetAlertOptions): void;

/**
* Closes the currently open SweetAlert2 modal programmatically.
*
Expand Down Expand Up @@ -280,6 +291,13 @@ declare module 'sweetalert2' {
*/
function isValidParameter(paramName: string): boolean;

/**
* Determines if a given parameter name is valid for Swal.update() method.
*
* @param paramName The parameter to check
*/
function isUpdatableParameter(paramName: string): boolean;

/**
* Normalizes the arguments you can give to Swal.fire() in an object of type SweetAlertOptions.
* ex:
Expand Down
32 changes: 32 additions & 0 deletions test/qunit/methods/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { Swal, SwalWithoutAnimation, isVisible } = require('../helpers')

QUnit.test('update() method', (assert) => {
SwalWithoutAnimation.fire()

Swal.update({
title: 'New title',
html: 'New content',
type: 'success',
showConfirmButton: false,
showCancelButton: true,
cancelButtonText: 'New cancel button text',
imageUrl: '/assets/swal2-logo.png'
})

assert.equal(Swal.getTitle().textContent, 'New title')
assert.equal(Swal.getContent().textContent, 'New content')

assert.ok(isVisible(Swal.getIcons().filter(icon => icon.classList.contains('swal2-success'))[0]))

assert.ok(isVisible(Swal.getImage()))
assert.ok(Swal.getImage().src.indexOf('/assets/swal2-logo.png') > 0)

assert.notOk(isVisible(Swal.getConfirmButton()))
assert.ok(isVisible(Swal.getCancelButton()))
assert.equal(Swal.getCancelButton().textContent, 'New cancel button text')
})

QUnit.test('isUpdatableParameter() method', (assert) => {
assert.ok(Swal.isUpdatableParameter('title'))
assert.notOk(Swal.isUpdatableParameter('preConfirm'))
})

0 comments on commit 56a137d

Please sign in to comment.