Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): integrate changes from sweetalert2/sweetalert2#1036
- Loading branch information
Showing
6 changed files
with
136 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
import swal from 'sweetalert2' | ||
import Swal from 'sweetalert2' | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import { mounts } from './mounts' | ||
|
||
const noop = () => {} | ||
|
||
export default function withReactContent(parentSwal = swal) { | ||
const argsToParams = args => { | ||
if (React.isValidElement(args[0]) || React.isValidElement(args[1])) { | ||
const params = {} | ||
;['title', 'html', 'type'].forEach((name, index) => { | ||
if (args[index] !== undefined) { | ||
params[name] = args[index] | ||
} | ||
}) | ||
return params | ||
} else { | ||
return parentSwal.argsToParams(args) | ||
export default function withReactContent(ParentSwal = Swal) { | ||
return class extends ParentSwal { | ||
static argsToParams(args) { | ||
if (React.isValidElement(args[0]) || React.isValidElement(args[1])) { | ||
const params = {} | ||
;['title', 'html', 'type'].forEach((name, index) => { | ||
if (args[index] !== undefined) { | ||
params[name] = args[index] | ||
} | ||
}) | ||
return params | ||
} else { | ||
return ParentSwal.argsToParams(args) | ||
} | ||
} | ||
} | ||
|
||
const fn = (...args) => { | ||
const params = Object.assign({}, argsToParams(args)) // safe to mutate this | ||
|
||
params.onOpen = params.onOpen || noop | ||
params.onClose = params.onClose || noop | ||
|
||
for (const { key, getter } of mounts) { | ||
if (React.isValidElement(params[key])) { | ||
const reactElement = params[key] | ||
params[key] = ' ' | ||
|
||
let domElement | ||
|
||
const childOnOpen = params.onOpen | ||
params.onOpen = () => { | ||
domElement = getter(parentSwal) | ||
ReactDOM.render(reactElement, domElement) | ||
childOnOpen() | ||
_main(params) { | ||
params = Object.assign({}, params) | ||
|
||
params.onOpen = params.onOpen || noop | ||
params.onClose = params.onClose || noop | ||
|
||
mounts.forEach(({ key, getter }) => { | ||
if (React.isValidElement(params[key])) { | ||
const reactElement = params[key] | ||
params[key] = ' ' | ||
|
||
let domElement | ||
|
||
const superOnOpen = params.onOpen | ||
params.onOpen = () => { | ||
domElement = getter(ParentSwal) | ||
ReactDOM.render(reactElement, domElement) | ||
superOnOpen() | ||
} | ||
|
||
const superOnClose = params.onClose | ||
params.onClose = () => { | ||
superOnClose() | ||
ReactDOM.unmountComponentAtNode(domElement) | ||
} | ||
} | ||
}) | ||
|
||
const childOnClose = params.onClose | ||
params.onClose = () => { | ||
childOnClose() | ||
ReactDOM.unmountComponentAtNode(domElement) | ||
} | ||
} | ||
return super._main(params) | ||
} | ||
return parentSwal(params) | ||
} | ||
|
||
return Object.assign(fn, parentSwal, { argsToParams }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import Swal from 'sweetalert2' | ||
import withReactContent from '../../src/index' | ||
import { describe, expect, it } from '../util/framework' | ||
import { | ||
cleanSwalState, | ||
getSwalContentContent, | ||
getVisibleSwalIconNames, | ||
} from '../util/swalUtil' | ||
import { timeout } from '../util/util' | ||
|
||
describe('integration', () => { | ||
it('renders React elements for each supported option', async () => { | ||
await cleanSwalState() | ||
const MySwal = withReactContent() | ||
await MySwal.fire({ | ||
animation: false, | ||
title: <span>title</span>, | ||
html: <span>html</span>, | ||
confirmButtonText: <span>confirmButtonText</span>, | ||
cancelButtonText: <span>cancelButtonText</span>, | ||
footer: <span>footer</span>, | ||
onOpen: () => { | ||
expect(MySwal.getTitle().innerHTML).toEqual('<span>title</span>') | ||
expect(MySwal.getContent().innerHTML).toEqual('<span>html</span>') | ||
expect(MySwal.getConfirmButton().innerHTML).toEqual( | ||
'<span>confirmButtonText</span>', | ||
) | ||
expect(MySwal.getCancelButton().innerHTML).toEqual( | ||
'<span>cancelButtonText</span>', | ||
) | ||
expect(MySwal.getFooter().innerHTML).toEqual('<span>footer</span>') | ||
MySwal.clickConfirm() | ||
}, | ||
}) | ||
}) | ||
it('can mix React and non-React params', async () => { | ||
await cleanSwalState() | ||
const MySwal = withReactContent() | ||
await MySwal.fire({ | ||
animation: false, | ||
title: <span>React element</span>, | ||
footer: 'plain text', | ||
onOpen: () => { | ||
expect(MySwal.getTitle().innerHTML).toEqual( | ||
'<span>React element</span>', | ||
) | ||
expect(MySwal.getFooter().innerHTML).toEqual('plain text') | ||
MySwal.clickConfirm() | ||
}, | ||
}) | ||
}) | ||
it('returns a class with the same instance & static properties as Swal', async () => { | ||
const MySwal = withReactContent() | ||
Object.keys(Swal).forEach(key => { | ||
expect(typeof MySwal[key]).toBe(typeof Swal[key]) | ||
}) | ||
Object.keys(Swal.prototype).forEach(key => { | ||
expect(typeof MySwal.prototype[key]).toBe(typeof Swal.prototype[key]) | ||
}) | ||
}) | ||
it('works with shorthand Swal calls', async () => { | ||
await cleanSwalState() | ||
const MySwal = withReactContent() | ||
const swal = MySwal.fire(<span>title</span>, <span>html</span>, 'info') | ||
await timeout(100) | ||
expect(MySwal.getTitle().innerHTML).toEqual('<span>title</span>') | ||
expect(MySwal.getContent().innerHTML).toEqual('<span>html</span>') | ||
expect(getVisibleSwalIconNames()).toEqual(['info']) | ||
MySwal.clickConfirm() | ||
await swal | ||
}) | ||
it('has no effect on normal shorthand Swal calls', async () => { | ||
await cleanSwalState() | ||
const MySwal = withReactContent() | ||
const swal = MySwal.fire('my title', 'my html', 'error') | ||
await timeout(100) | ||
expect(MySwal.getTitle().innerHTML).toEqual('my title') | ||
expect(getSwalContentContent().innerHTML).toEqual('my html') | ||
expect(getVisibleSwalIconNames()).toEqual(['error']) | ||
MySwal.clickConfirm() | ||
await swal | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.