Skip to content

Commit

Permalink
feat: add iconHtml mount (#147)
Browse files Browse the repository at this point in the history
* feat: add iconHtml mount

* do not create unnnecessary react elements

* fix mount

* require sweetalert2 ^10.14.1 in peerDeps
  • Loading branch information
limonte authored Feb 12, 2021
1 parent 85205ac commit 8dd92fa
Show file tree
Hide file tree
Showing 8 changed files with 839 additions and 843 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following options can be React elements:
- cancelButtonText
- footer
- closeButtonHtml
- iconHtml

## Installation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"peerDependencies": {
"react": "^16.0.0 || ^17.0.0",
"react-dom": "^16.0.0 || ^17.0.0",
"sweetalert2": "^9.7.0 || ^10.0.0"
"sweetalert2": "^9.7.0 || ^10.14.1"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function withReactContent (ParentSwal) {
const superOpenHook = params[openHookName] || noop
params[openHookName] = (element) => {
domElement = getter(ParentSwal)
ReactDOM.render(reactElement, domElement)
domElement && ReactDOM.render(reactElement, domElement)
superOpenHook(element)
}

Expand Down
4 changes: 4 additions & 0 deletions src/mounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export const mounts = [
key: 'closeButtonHtml',
getter: swal => swal.getCloseButton(),
},
{
key: 'iconHtml',
getter: swal => swal.getIcon() ? swal.getIcon().querySelector('.swal2-icon-content') : null,
},
]
3 changes: 3 additions & 0 deletions src/sweetalert2-react-content.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ interface ReactOptions {
title?: ReactElementOr<'title'>;
html?: ReactElementOr<'html'>;
confirmButtonText?: ReactElementOr<'confirmButtonText'>;
denyButtonText?: ReactElementOr<'denyButtonText'>;
cancelButtonText?: ReactElementOr<'cancelButtonText'>;
footer?: ReactElementOr<'footer'>;
closeButtonHtml?: ReactElementOr<'closeButtonHtml'>;
iconHtml?: ReactElementOr<'iconHtml'>;
}

// Overwrite<T, U> will take the properties of U and add to it the properties of T that are not in U.
Expand Down
5 changes: 5 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('integration', () => {
await MySwal.fire({
title: <span>title</span>,
html: <span>html</span>,
icon: 'success',
iconHtml: <span>@</span>,
confirmButtonText: <span>confirmButtonText</span>,
denyButtonText: <span>denyButtonText</span>,
cancelButtonText: <span>cancelButtonText</span>,
Expand All @@ -38,6 +40,9 @@ describe('integration', () => {
expect(MySwal.getCancelButton().innerHTML).toEqual(
'<span>cancelButtonText</span>',
)
expect(MySwal.getIcon().innerHTML).toEqual(
'<div class="swal2-icon-content"><span>@</span></div>',
)
expect(MySwal.getFooter().innerHTML).toEqual('<span>footer</span>')
expect(MySwal.getCloseButton().innerHTML).toEqual('<span>closeButtonHtml</span>')
MySwal.clickConfirm()
Expand Down
8 changes: 5 additions & 3 deletions test/test-build.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta charset="UTF-8">
<script src="../node_modules/sweetalert2/dist/sweetalert2.all.min.js"></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="../node_modules/react/umd/react.development.js"></script>
<script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
<script src="../dist/sweetalert2-react-content.umd.js"></script>
</head>
<body>
Expand All @@ -13,7 +13,9 @@
let MySwal = withReactContent(Swal)
Promise.resolve().then(async () => {
await MySwal.fire({
title: React.createElement('i', {}, ['Hello World']),
icon: 'success',
iconHtml: React.createElement('b', {key: 'iconHtml'}, ['P']),
title: React.createElement('i', {key: 'title'}, ['Hello World']),
didOpen: (popup) => {
console.log('didOpen', popup)
},
Expand Down
1,657 changes: 819 additions & 838 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 8dd92fa

Please sign in to comment.