UNPKG

@amaui/ui-react

Version:
134 lines (133 loc) 11.3 kB
"use strict"; var __rest = (this && this.__rest) || function (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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const Button_1 = __importDefault(require("../Button")); const Context_1 = __importDefault(require("./Context")); const ModalHeader_1 = __importDefault(require("../ModalHeader")); const ModalFooter_1 = __importDefault(require("../ModalFooter")); const ModalTitle_1 = __importDefault(require("../ModalTitle")); const ModalMain_1 = __importDefault(require("../ModalMain")); const ModalText_1 = __importDefault(require("../ModalText")); const Modal_1 = __importDefault(require("../Modal")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { '& .amaui-ModalMain-root': { padding: `6px 0 10px` }, '& .amaui-ModalFooter-root': { padding: `12px 0 12px` }, '& .amaui-Modal-background:not(.amaui-Modal-background-invisible)': { background: theme.methods.palette.color.colorToRgb(theme.methods.palette.color.value('default', 10), theme.palette.light ? 20 : 40) } }, surface: { '&.amaui-Surface-root': { background: theme.palette.color.primary[theme.palette.light ? 99 : 5], paddingBottom: '8px' } } }), { name: 'amaui-Confirm' }); const Confirm = react_1.default.forwardRef((props_, ref) => { var _a, _b, _c, _d; const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiConfirm) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const ModalHeader = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ModalHeader) || ModalHeader_1.default; }, [theme]); const ModalFooter = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ModalFooter) || ModalFooter_1.default; }, [theme]); const ModalTitle = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ModalTitle) || ModalTitle_1.default; }, [theme]); const ModalMain = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ModalMain) || ModalMain_1.default; }, [theme]); const ModalText = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.ModalText) || ModalText_1.default; }, [theme]); const Modal = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Modal) || Modal_1.default; }, [theme]); const Button = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Button) || Button_1.default; }, [theme]); const { throwError, onOpen, onClose, className, children } = props, other = __rest(props, ["throwError", "onOpen", "onClose", "className", "children"]); const { classes } = useStyle(); const [openModal, setOpenModal] = react_1.default.useState(false); const refs = { value: react_1.default.useRef({}), modal: react_1.default.useRef(), props: react_1.default.useRef(undefined), promise: { resolve: react_1.default.useRef(undefined), reject: react_1.default.useRef(undefined) } }; refs.props.current = props; const open = react_1.default.useCallback((value) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; if (!openModal) { refs.modal.current = Object.assign({}, value); // Defaults refs.modal.current.name = (value === null || value === void 0 ? void 0 : value.name) !== undefined ? value === null || value === void 0 ? void 0 : value.name : 'Confirmation'; refs.modal.current.description = (value === null || value === void 0 ? void 0 : value.description) !== undefined ? value === null || value === void 0 ? void 0 : value.description : 'Are you sure you want to proceed?'; if (((_b = (_a = refs.modal.current.buttons) === null || _a === void 0 ? void 0 : _a.negative) === null || _b === void 0 ? void 0 : _b.text) === undefined) (0, utils_1.setObjectValue)(refs.modal.current, 'buttons.negative.text', ''); if (((_d = (_c = refs.modal.current.buttons) === null || _c === void 0 ? void 0 : _c.positive) === null || _d === void 0 ? void 0 : _d.text) === undefined) (0, utils_1.setObjectValue)(refs.modal.current, 'buttons.positive.text', ''); refs.modal.current.buttons.negative.text = ((_f = (_e = value === null || value === void 0 ? void 0 : value.buttons) === null || _e === void 0 ? void 0 : _e.negative) === null || _f === void 0 ? void 0 : _f.text) !== undefined ? (_h = (_g = value === null || value === void 0 ? void 0 : value.buttons) === null || _g === void 0 ? void 0 : _g.negative) === null || _h === void 0 ? void 0 : _h.text : 'Cancel'; refs.modal.current.buttons.positive.text = ((_k = (_j = value === null || value === void 0 ? void 0 : value.buttons) === null || _j === void 0 ? void 0 : _j.positive) === null || _k === void 0 ? void 0 : _k.text) !== undefined ? (_m = (_l = value === null || value === void 0 ? void 0 : value.buttons) === null || _l === void 0 ? void 0 : _l.positive) === null || _m === void 0 ? void 0 : _m.text : 'Confirm'; refs.modal.current.throwError = (value === null || value === void 0 ? void 0 : value.throwError) !== undefined ? value.throwError : refs.props.current.throwError; const promise = new Promise((resolve, reject) => { refs.promise.resolve.current = resolve; refs.promise.reject.current = reject; }); setOpenModal(true); if ((0, utils_1.is)('function', onOpen)) onOpen(); return promise; } }, [openModal, onOpen]); const close = react_1.default.useCallback((confirm = false) => { var _a, _b, _c, _d, _e, _f; if (openModal) { setOpenModal(false); if ((0, utils_1.is)('function', onClose)) onClose(); // Resolve or reject if (confirm) (_b = (_a = refs.promise.resolve).current) === null || _b === void 0 ? void 0 : _b.call(_a, confirm); else !refs.modal.current.throwError ? (_d = (_c = refs.promise.resolve).current) === null || _d === void 0 ? void 0 : _d.call(_c, false) : (_f = (_e = refs.promise.reject).current) === null || _f === void 0 ? void 0 : _f.call(_e); refs.promise.resolve.current = undefined; refs.promise.reject.current = undefined; } }, [openModal, onClose]); const _e = (refs.modal.current || {}), { name, description, buttons, throwError: throwError_, ButtonNegativeProps, ButtonPositiveProps, modal } = _e, otherModal = __rest(_e, ["name", "description", "buttons", "throwError", "ButtonNegativeProps", "ButtonPositiveProps", "modal"]); // Add to the value refs.value.current.open = open; refs.value.current.close = close; return ((0, jsx_runtime_1.jsxs)(Context_1.default.Provider, Object.assign({ value: refs.value.current }, { children: [children, (0, jsx_runtime_1.jsx)(Modal, Object.assign({ ref: ref, open: openModal, onClose: close, SurfaceProps: { tonal: true, color: 'primary', className: (0, style_react_1.classNames)([ classes.surface ]) }, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Confirm', theme) && [ `amaui-Confirm-root` ], className, classes.root ]) }, other, otherModal, { children: (0, utils_1.is)('function', modal) ? modal({ resolve: refs.promise.resolve.current, reject: refs.promise.reject.current }) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [name !== false && ((0, jsx_runtime_1.jsx)(ModalHeader, { children: (0, jsx_runtime_1.jsx)(ModalTitle, Object.assign({ version: 't1', weight: 500 }, { children: name || 'Confirmation' })) })), (0, jsx_runtime_1.jsx)(ModalMain, { children: (0, jsx_runtime_1.jsx)(ModalText, Object.assign({ version: 'b1', weight: 200 }, { children: description !== undefined ? description : 'Are you sure you want to proceed?' })) }), (0, jsx_runtime_1.jsxs)(ModalFooter, { children: [(0, jsx_runtime_1.jsx)(Button, Object.assign({ version: 'text', color: 'inherit', tonal: true, onClick: () => close(false) }, ButtonNegativeProps, { children: ((_a = buttons === null || buttons === void 0 ? void 0 : buttons.negative) === null || _a === void 0 ? void 0 : _a.text) !== undefined ? (_b = buttons === null || buttons === void 0 ? void 0 : buttons.negative) === null || _b === void 0 ? void 0 : _b.text : 'Cancel' })), (0, jsx_runtime_1.jsx)(Button, Object.assign({ version: 'text', color: 'inherit', tonal: true, onClick: () => close(true) }, ButtonPositiveProps, { children: ((_c = buttons === null || buttons === void 0 ? void 0 : buttons.positive) === null || _c === void 0 ? void 0 : _c.text) !== undefined ? (_d = buttons === null || buttons === void 0 ? void 0 : buttons.positive) === null || _d === void 0 ? void 0 : _d.text : 'Confirm' }))] })] })) }))] }))); }); Confirm.displayName = 'amaui-Confirm'; exports.default = Confirm;