Skip to content

Commit

Permalink
feat: add Swal.getIcon()
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Mar 15, 2019
1 parent ba4a485 commit acc42a0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/staticMethods/dom.js
Expand Up @@ -7,6 +7,7 @@ export {
getTitle,
getContent,
getImage,
getIcon,
getIcons,
getCloseButton,
getActions,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/dom/getters.js
Expand Up @@ -20,6 +20,11 @@ export const getIcons = () => {
return toArray(popup.querySelectorAll('.' + swalClasses.icon))
}

export const getIcon = () => {
const visibleIcon = getIcons().filter(icon => isVisible(icon))
return visibleIcon.length ? visibleIcon[0] : null
}

export const getTitle = () => elementByClass(swalClasses.title)

export const getContent = () => elementByClass(swalClasses.content)
Expand Down
5 changes: 5 additions & 0 deletions sweetalert2.d.ts
Expand Up @@ -87,6 +87,11 @@ declare module 'sweetalert2' {
*/
function getCloseButton(): HTMLElement;

/**
* Gets the current visible icon.
*/
function getIcon(): HTMLElement | null;

/**
* Gets all icons. The current visible icon will have `style="display: flex"`,
* all other will be hidden by `style="display: none"`.
Expand Down
6 changes: 6 additions & 0 deletions test/qunit/methods/getIcon.js
@@ -0,0 +1,6 @@
import { $, Swal } from '../helpers.js'

QUnit.test('getIcon() method', (assert) => {
Swal.fire({ type: 'success' })
assert.equal(Swal.getIcon(), $('.swal2-success'))
})
5 changes: 3 additions & 2 deletions test/qunit/methods/update.js
@@ -1,4 +1,4 @@
const { Swal, SwalWithoutAnimation, isVisible } = require('../helpers')
const { $, Swal, SwalWithoutAnimation, isVisible } = require('../helpers')

QUnit.test('update() method', (assert) => {
SwalWithoutAnimation.fire()
Expand All @@ -16,7 +16,8 @@ QUnit.test('update() method', (assert) => {
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.getIcon()))
assert.equal(Swal.getIcon(), $('.swal2-success'))

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

0 comments on commit acc42a0

Please sign in to comment.