UNPKG

@alicloudfe/components

Version:

A react component library for aliyun

297 lines (296 loc) 16.1 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(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); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 }); var react_1 = __importStar(require("react")); var react_dom_1 = __importDefault(require("react-dom")); var next_1 = require("@alifd/next"); var withThemeClass_1 = require("../utils/withThemeClass"); var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics")); var button_1 = __importDefault(require("../button")); var classnames_1 = __importDefault(require("classnames")); var useCssVar_1 = require("../utils/useCssVar"); var zh_cn_1 = __importDefault(require("../locale/zh-cn")); var sizeMap = { mini: 400, small: 600, medium: 800, large: 1200 }; var yunxiaoSizeMap = { mini: 480, small: 640, medium: 800, large: '70%' }; // 渲染表头 var renderTitle = function (prefix, theme, title, extra) { if (!(theme.startsWith('hybridcloud') || theme.startsWith('hybridcloud-dark'))) { return title; } if (extra) { return (react_1.default.createElement("div", { className: prefix + "drawer-header-container" }, react_1.default.createElement("span", { className: prefix + "drawer-header-title" }, title), react_1.default.createElement("span", { className: prefix + "drawer-header-extra" }, extra, extra && react_1.default.createElement("div", { className: prefix + "drawer-header-line" })))); } return title; }; var Drawer = withThemeClass_1.withThemeClass(react_1.default.forwardRef(function (props, ref) { var _a, _b; var _c = props.visible, visible = _c === void 0 ? false : _c, onOk = props.onOk, onCancel = props.onCancel, renderFooter = props.renderFooter, hasFooterLine = props.hasFooterLine, footerAlign = props.footerAlign, children = props.children, _d = props.locale, locale = _d === void 0 ? zh_cn_1.default.Drawer : _d, _e = props.okText, okText = _e === void 0 ? locale.ok : _e, _f = props.cancelText, cancelText = _f === void 0 ? locale.cancel : _f, _g = props.cancelBtnProps, cancelBtnProps = _g === void 0 ? {} : _g, _h = props.okBtnProps, okBtnProps = _h === void 0 ? {} : _h, footerClass = props.footerClass, _j = props.size, size = _j === void 0 ? 'mini' : _j, width = props.width, className = props.className, actionRef = props.actionRef, sidebar = props.sidebar, title = props.title, extra = props.extra, filterProps = __rest(props, ["visible", "onOk", "onCancel", "renderFooter", "hasFooterLine", "footerAlign", "children", "locale", "okText", "cancelText", "cancelBtnProps", "okBtnProps", "footerClass", "size", "width", "className", "actionRef", "sidebar", "title", "extra"]); var _k = props.prefix, prefix = _k === void 0 ? 'next-' : _k; // console.log('locale', locale) var _l = react_1.useState(visible), customVisible = _l[0], setCustomVisible = _l[1]; var _m = react_1.useState(false), okLoadingState = _m[0], setOkLoadingState = _m[1]; var _o = react_1.useState(false), cancelLoadingState = _o[0], setCancelLoadingState = _o[1]; var customRef = react_1.useRef(null); var theme = useCssVar_1.useCssVar('--alicloudfe-components-theme').trim(); // 传递指定显示/隐藏方法 var close = function () { setCustomVisible(false); }; var show = function () { setCustomVisible(true); }; var setOKLoading = function (loading) { setOkLoadingState(loading); }; var setCancelLoading = function (loading) { setCancelLoading(loading); }; actionRef === null || actionRef === void 0 ? void 0 : actionRef(show, close, setOKLoading, setCancelLoading); // 超出一屏 设置 footer 阴影 var setFooterShadow = function (iRef) { var _a, _b, _c, _d; if ((iRef === null || iRef === void 0 ? void 0 : iRef.current) && theme !== 'wind' && !theme.startsWith('xconsole')) { var drawerDom = react_dom_1.default.findDOMNode(iRef.current); var drawerFirstDom = (_b = (_a = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "drawer")) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.firstChild; if (drawerFirstDom) { drawerFirstDom.style.overflow = 'hidden'; } var drawerBodyDom = (_c = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "drawer-body")) === null || _c === void 0 ? void 0 : _c[0]; if (drawerBodyDom) { drawerBodyDom.style.overflow = 'auto'; } var drawerFooterDom = (_d = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "drawer-footer")) === null || _d === void 0 ? void 0 : _d[0]; if (drawerFooterDom) { if ((drawerBodyDom === null || drawerBodyDom === void 0 ? void 0 : drawerBodyDom.clientHeight) < (drawerBodyDom === null || drawerBodyDom === void 0 ? void 0 : drawerBodyDom.scrollHeight)) { drawerFooterDom.classList.add(prefix + "drawer-footer-has-shadow"); } else { drawerFooterDom.classList.remove(prefix + "drawer-footer-has-shadow"); } } } }; var observer = null; // 绑定监听器 react_1.useEffect(function () { var _a; setFooterShadow(ref !== null && ref !== void 0 ? ref : customRef); var drawerDom = react_dom_1.default.findDOMNode((ref !== null && ref !== void 0 ? ref : customRef).current); var drawerBodyDom = (_a = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "drawer-body")) === null || _a === void 0 ? void 0 : _a[0]; if (drawerBodyDom && !observer) { observer = new MutationObserver(function () { setFooterShadow(ref !== null && ref !== void 0 ? ref : customRef); }); observer.observe(drawerBodyDom, { attributes: true, attributeFilter: ['style'], attributeOldValue: true, childList: true, subtree: true }); } // 销毁 return function () { if (observer) { observer.disconnect(); observer.takeRecords(); observer = null; } }; }); react_1.useEffect(function () { setCustomVisible(visible); }, [visible]); var drawerCustomClassName = classnames_1.default((_a = {}, _a[prefix + "drawer-has-footer"] = onOk || onCancel || renderFooter, _a[className] = !!className, _a[prefix + "drawer-large"] = size === 'large', _a[prefix + "drawer-has-sidebar"] = !!sidebar && theme === 'yunxiao-v5', _a)); var drawerFooterClassName = classnames_1.default((_b = {}, _b[prefix + "drawer-footer"] = true, _b[prefix + "drawer-footer-line"] = hasFooterLine, _b[prefix + "drawer-footer-right"] = footerAlign === 'right', _b[prefix + "drawer-footer-left"] = footerAlign === 'left', _b[prefix + "drawer-footer-center"] = footerAlign === 'center', _b[footerClass] = !!footerClass, _b)); var getCustomWidth = function () { if (width) { return width; } if (size) { if (theme === 'yunxiao-v5') { return yunxiaoSizeMap[size] || yunxiaoSizeMap['mini']; } else { return sizeMap[size] || sizeMap['mini']; } } }; return (react_1.default.createElement(next_1.Drawer, __assign({}, filterProps, { title: renderTitle(prefix, theme, title, extra), ref: ref ? ref : customRef, visible: customVisible, width: getCustomWidth(), className: drawerCustomClassName }), theme.startsWith('yunxiao-v5') && sidebar && react_1.default.createElement("div", { className: 'next-drawer-sidebar' }, sidebar), children, (onOk || onCancel || renderFooter) && (react_1.default.createElement("div", { className: drawerFooterClassName }, onOk && !renderFooter && (react_1.default.createElement(button_1.default, __assign({ type: "primary", onClick: typeof onOk === 'function' ? onOk : undefined, style: { marginRight: 8 }, loading: okLoadingState }, okBtnProps), okText)), onCancel && !renderFooter && (react_1.default.createElement(button_1.default, __assign({ onClick: typeof onCancel === 'function' ? onCancel : undefined, loading: cancelLoadingState }, cancelBtnProps), cancelText)), renderFooter && renderFooter)))); })); // 快捷调用 var show = function (props) { var onOk = props.onOk, onCancel = props.onCancel, onClose = props.onClose, content = props.content, others = __rest(props, ["onOk", "onCancel", "onClose", "content"]); var _a = props.prefix, prefix = _a === void 0 ? 'next-' : _a; var customOnOK; var customOnCancel; var actionRef; // 合并 customOnOK。 如果返回值为 true 自动关闭 Drawer customOnOK = function (event) { var _a, _b, _c; if (onOk && typeof onOk === 'function') { var result = onOk === null || onOk === void 0 ? void 0 : onOk(event); if (result instanceof Promise) { (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, true); result .then(function (state) { var _a, _b, _c; if (state !== false) { (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, false); (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef); return; } (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _c === void 0 ? void 0 : _c.call(actionRef, false); }) .catch(function () { }); } if (typeof result === 'boolean' && result) { (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef); return; } } else { (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _c === void 0 ? void 0 : _c.call(actionRef); } }; customOnCancel = function (event) { var _a, _b, _c, _d; if (onCancel && typeof onCancel === 'function') { var result = onCancel === null || onCancel === void 0 ? void 0 : onCancel(event); if (!result) { (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _a === void 0 ? void 0 : _a.call(actionRef); return; } if (result instanceof Promise) { (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setCancelLoading) === null || _b === void 0 ? void 0 : _b.call(actionRef, true); result .then(function (state) { var _a, _b; if (state !== false) { (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setCancelLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, false); (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef); } return; }) .catch(function () { }); } if (typeof result === 'boolean' && result) { (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _c === void 0 ? void 0 : _c.call(actionRef); return; } } else { (_d = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _d === void 0 ? void 0 : _d.call(actionRef); } }; var ConfigModal = next_1.ConfigProvider.config(Drawer, { componentName: 'Drawer' }); var container = document.createElement('div'); container.setAttribute('id', prefix + "quick-drawer"); document.body.appendChild(container); // @ts-ignore var newContext = next_1.ConfigProvider.getContext(); react_dom_1.default.render(react_1.default.createElement(next_1.ConfigProvider, __assign({}, newContext), react_1.default.createElement(ConfigModal, __assign({}, others, { visible: true, actionRef: function (show, close, setOKLoading, setCancelLoading) { actionRef = { show: show, close: close, setOKLoading: setOKLoading, setCancelLoading: setCancelLoading }; }, // 原先快捷调用默认不传也有 onOk: onOk !== false ? customOnOK : undefined, onCancel: onCancel !== false ? customOnCancel : undefined, onClose: onClose !== null && onClose !== void 0 ? onClose : (function () { var _a; (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _a === void 0 ? void 0 : _a.call(actionRef); }) }), content)), container); return { hide: function () { var _a; (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _a === void 0 ? void 0 : _a.call(actionRef); }, show: function () { var _a; (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.show) === null || _a === void 0 ? void 0 : _a.call(actionRef); } }; }; hoist_non_react_statics_1.default(Drawer, next_1.Drawer); // @ts-ignore Drawer.displayName = next_1.Drawer.displayName; Drawer.show = show; exports.default = Drawer;