Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use didOpen and didDestroy instead of deprecated onOpen and onDestroy #126

Merged
merged 5 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const MySwal = withReactContent(Swal)
MySwal.fire({
title: <p>Hello World</p>,
footer: 'Copyright 2018',
onOpen: () => {
didOpen: () => {
// `MySwal` is a subclass of `Swal`
// with all the same instance & static methods
MySwal.clickConfirm()
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export default [false, true].map(minify => {
format: 'umd',
name: 'sweetalert2ReactContent',
globals: {
react: 'React',
'react': 'React',
'react-dom': 'ReactDOM',
},
},
].map(({ format, ...rest }) => {
const fileExt = format + (minify ? '.min' : '') + '.js'
const fileExt = `${format + (minify ? '.min' : '')}.js`
const file = `dist/sweetalert2-react-content.${fileExt}`
return {
format,
Expand Down
17 changes: 8 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,25 @@ export default function withReactContent (ParentSwal) {
_main (params) {
params = Object.assign({}, params)

params.onOpen = params.onOpen || noop
params.onDestroy = params.onDestroy || noop

mounts.forEach(({ key, getter }) => {
if (React.isValidElement(params[key])) {
const reactElement = params[key]
params[key] = ' '

let domElement

const superOnOpen = params.onOpen
params.onOpen = (element) => {
const openHookName = params.onOpen || !ParentSwal.isValidParameter('didOpen') ? 'onOpen' : 'didOpen' // support legacy onOpen hook
const superOpenHook = params[openHookName] || noop
params[openHookName] = (element) => {
domElement = getter(ParentSwal)
ReactDOM.render(reactElement, domElement)
superOnOpen(element)
superOpenHook(element)
}

const superOnDestroy = params.onDestroy
params.onDestroy = (element) => {
superOnDestroy(element)
const destroyHookName = params.onDestroy || !ParentSwal.isValidParameter('didDestroy') ? 'onDestroy' : 'didDestroy' // support legacy onDestroy hook
const superDestroyHook = params[destroyHookName] || noop
params[destroyHookName] = (element) => {
superDestroyHook(element)
if (domElement) {
ReactDOM.unmountComponentAtNode(domElement)
}
Expand Down
6 changes: 3 additions & 3 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('integration', () => {
cancelButtonText: <span>cancelButtonText</span>,
closeButtonHtml: <span>closeButtonHtml</span>,
footer: <span>footer</span>,
onOpen: () => {
didOpen: () => {
expect(MySwal.getTitle().innerHTML).toEqual('<span>title</span>')
expect(MySwal.getHtmlContainer().innerHTML).toEqual('<span>html</span>')
expect(MySwal.getConfirmButton().innerHTML).toEqual(
Expand All @@ -50,7 +50,7 @@ describe('integration', () => {
await MySwal.fire({
title: <span>React element</span>,
footer: 'plain text',
onOpen: () => {
didOpen: () => {
expect(MySwal.getTitle().innerHTML).toEqual(
'<span>React element</span>',
)
Expand All @@ -69,7 +69,7 @@ describe('integration', () => {
await MySwal.fire({
title: <span>React element</span>,
footer: 'plain text',
onOpen: () => {
didOpen: () => {
expect(MySwal.getTitle().innerHTML).toEqual(
'<span>React element</span>',
)
Expand Down
27 changes: 27 additions & 0 deletions test/test-build-v9.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="../node_modules/react/umd/react.production.min.js"></script>
<script src="../node_modules/react-dom/umd/react-dom.production.min.js"></script>
<script src="../dist/sweetalert2-react-content.umd.js"></script>
</head>
<body>
<script>
let withReactContent = sweetalert2ReactContent
let MySwal = withReactContent(Swal)
Promise.resolve().then(async () => {
await MySwal.fire({
title: React.createElement('i', {}, ['Hello World']),
onOpen: (popup) => {
console.log('onOpen', popup)
},
onDestroy: () => {
console.log('onDestroy')
},
})
}).catch(console.error)
</script>
</body>
</html>
10 changes: 9 additions & 1 deletion test/test-build.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
let withReactContent = sweetalert2ReactContent
let MySwal = withReactContent(Swal)
Promise.resolve().then(async () => {
await MySwal.fire(React.createElement('strong', {}, ['Hello World']))
await MySwal.fire({
title: React.createElement('i', {}, ['Hello World']),
didOpen: (popup) => {
console.log('didOpen', popup)
},
didDestroy: () => {
console.log('didDestroy')
},
})
}).catch(console.error)
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion test/util/swalUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const SwalWithoutAnimation = Swal.mixin({
async function cleanSwalState () {
await SwalWithoutAnimation.fire({
title: 'clear',
onOpen: () => Swal.clickConfirm(),
didOpen: () => Swal.clickConfirm(),
})
}

Expand Down
Loading