UNPKG

ultra-design

Version:
195 lines (161 loc) 6.33 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import _pt from 'prop-types'; import _regeneratorRuntime from '@babel/runtime/regenerator'; import { useContext, useState, useEffect } from 'react'; import ReactDOM from 'react-dom'; import { Info } from '@icon-park/react'; import ConfirmModal from './modal-confirm.js'; import ModalComponent from './modal.js'; import { ConfigContext } from '../config-provider/config-provider.js'; import { mergeProps } from '../utils/mergeProps.js'; import { emitter } from '../utils/mitt.js'; import { jsx, jsxs, Fragment } from '@emotion/react/jsx-runtime'; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } var newConfig = {}; var unmountRoot = function unmountRoot() { var root = document.getElementById('ultra-confirm'); if (root) { ReactDOM.unmountComponentAtNode(root); return root; } }; var createRoot = function createRoot() { var root = document.createElement('div'); root.id = 'ultra-confirm'; document.body.appendChild(root); return root; }; function confirm(data) { var root = unmountRoot() || createRoot(); var options = {}; Object.assign(options, data); var ConfirmModalWrapper = function ConfirmModalWrapper(props) { var options = props.options; var context = useContext(ConfigContext); var config = mergeProps(context, newConfig, options); var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), visible = _useState2[0], setVisible = _useState2[1]; var _useState3 = useState(config), _useState4 = _slicedToArray(_useState3, 2), confirmOptions = _useState4[0], setConfirmModalOptions = _useState4[1]; var data = { visible: visible, title: jsxs(Fragment, { children: [jsx(Info, { fill: "#13c2c2" }), " ", confirmOptions.title] }), onOk: function () { var _onOk = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) { var _confirmOptions$onOk; var result; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return (_confirmOptions$onOk = confirmOptions.onOk) === null || _confirmOptions$onOk === void 0 ? void 0 : _confirmOptions$onOk.call(confirmOptions, e); case 2: result = _context.sent; if (!(result === false)) { _context.next = 5; break; } return _context.abrupt("return"); case 5: setVisible(false); case 6: case "end": return _context.stop(); } } }, _callee); })); function onOk(_x) { return _onOk.apply(this, arguments); } return onOk; }(), onClose: function () { var _onClose = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(e) { var _confirmOptions$onClo; var result; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return (_confirmOptions$onClo = confirmOptions.onClose) === null || _confirmOptions$onClo === void 0 ? void 0 : _confirmOptions$onClo.call(confirmOptions, e); case 2: result = _context2.sent; if (!(result === false)) { _context2.next = 5; break; } return _context2.abrupt("return"); case 5: setVisible(false); case 6: case "end": return _context2.stop(); } } }, _callee2); })); function onClose(_x2) { return _onClose.apply(this, arguments); } return onClose; }(), children: confirmOptions.content, cancelButton: confirmOptions.cancelButton, confirmButton: confirmOptions.confirmButton }; useEffect(function () { emitter.on('modal-config', function (e) { setConfirmModalOptions(function (o) { return mergeProps(o, e); }); }); return function () { emitter.all.clear(); }; }, []); return jsx(ConfirmModal, _objectSpread(_objectSpread({}, confirmOptions), data)); }; ConfirmModalWrapper.propTypes = { options: _pt.shape({ title: _pt.node, content: _pt.node.isRequired, onClose: _pt.func, onOk: _pt.func }).isRequired }; ReactDOM.render(jsx(ConfirmModalWrapper, { options: options }), root); } var iconMap = { confirm: jsx(Info, { fill: "#13c2c2" }) }; var Modal = ModalComponent; Modal.config = function (config) { newConfig = config; emitter.emit('modal-config', config); }; for (var key in iconMap) { var k = key; Modal[k] = function (options) { confirm(options); }; } var Modal$1 = Modal; export { Modal$1 as default };