UNPKG

react-sweetalert2

Version:
101 lines (92 loc) 4.01 kB
import React, { useState, useEffect } from 'react'; import Swal from 'sweetalert2'; import ReactDOM from 'react-dom'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } var useForceRerendering = function () { var _a = useState(false), rerender = _a[0], setRerender = _a[1]; return function () { return setRerender(!rerender); }; }; var withSwal = function (Component) { return React.forwardRef(function (props, ref) { return (React.createElement(Component, __assign({ ref: ref, swal: Swal }, props))); }); }; function SweetAlert2(props) { var forceRerendering = useForceRerendering(); useEffect(function () { if (props.show) { mountSwal(); if (props.showLoading) { Swal.showLoading(); } } else { Swal.close(); } }, [props.show]); useEffect(function () { if (props.show && props.showLoading) { Swal.showLoading(); } else { Swal.hideLoading(); } }, [props.showLoading]); useEffect(function () { var swalContainer = Swal.getHtmlContainer(); if (swalContainer) { var showContainer = props.text || props.html || props.children; swalContainer.style.display = showContainer ? 'block' : 'none'; } }, [props.text, props.html, props.children]); function mountSwal() { props.show; props.showLoading; var onConfirm = props.onConfirm, willOpen = props.willOpen, onResolve = props.onResolve, onError = props.onError; props.children; var rest = __rest(props, ["show", "showLoading", "onConfirm", "willOpen", "onResolve", "onError", "children"]); rest['willOpen'] = function (el) { forceRerendering(); willOpen && willOpen(el); }; Swal.fire(rest) .then(function (result) { if (result.isConfirmed) onConfirm && onConfirm(result); onResolve && onResolve(result); }) .catch(function (error) { return onError && onError(error); }); } var swalContainer = Swal.getHtmlContainer(); if (swalContainer && !props.html) { return ReactDOM.createPortal(props.children, swalContainer); } return React.createElement(React.Fragment, null); } export { SweetAlert2 as default, withSwal };