UNPKG

@alicloudfe/components

Version:

A react component library for aliyun

273 lines (272 loc) 14.7 kB
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 __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; }; import React, { useEffect, useState, useRef } from 'react'; import ReactDOM from 'react-dom'; import { ConfigProvider, Drawer as NextDrawer } from '@alifd/next'; import { withThemeClass } from '../utils/withThemeClass'; import hoistNonReactStatics from 'hoist-non-react-statics'; import { default as Button } from '../button'; import cls from 'classnames'; import { useCssVar } from '../utils/useCssVar'; import zhCN from '../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.createElement("div", { className: prefix + "drawer-header-container" }, React.createElement("span", { className: prefix + "drawer-header-title" }, title), React.createElement("span", { className: prefix + "drawer-header-extra" }, extra, extra && React.createElement("div", { className: prefix + "drawer-header-line" })))); } return title; }; var Drawer = withThemeClass(React.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 ? zhCN.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 = useState(visible), customVisible = _l[0], setCustomVisible = _l[1]; var _m = useState(false), okLoadingState = _m[0], setOkLoadingState = _m[1]; var _o = useState(false), cancelLoadingState = _o[0], setCancelLoadingState = _o[1]; var customRef = useRef(null); var theme = 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 = ReactDOM.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; // 绑定监听器 useEffect(function () { var _a; setFooterShadow(ref !== null && ref !== void 0 ? ref : customRef); var drawerDom = ReactDOM.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; } }; }); useEffect(function () { setCustomVisible(visible); }, [visible]); var drawerCustomClassName = cls((_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 = cls((_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.createElement(NextDrawer, __assign({}, filterProps, { title: renderTitle(prefix, theme, title, extra), ref: ref ? ref : customRef, visible: customVisible, width: getCustomWidth(), className: drawerCustomClassName }), theme.startsWith('yunxiao-v5') && sidebar && React.createElement("div", { className: 'next-drawer-sidebar' }, sidebar), children, (onOk || onCancel || renderFooter) && (React.createElement("div", { className: drawerFooterClassName }, onOk && !renderFooter && (React.createElement(Button, __assign({ type: "primary", onClick: typeof onOk === 'function' ? onOk : undefined, style: { marginRight: 8 }, loading: okLoadingState }, okBtnProps), okText)), onCancel && !renderFooter && (React.createElement(Button, __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 = ConfigProvider.config(Drawer, { componentName: 'Drawer' }); var container = document.createElement('div'); container.setAttribute('id', prefix + "quick-drawer"); document.body.appendChild(container); // @ts-ignore var newContext = ConfigProvider.getContext(); ReactDOM.render(React.createElement(ConfigProvider, __assign({}, newContext), React.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); } }; }; hoistNonReactStatics(Drawer, NextDrawer); // @ts-ignore Drawer.displayName = NextDrawer.displayName; Drawer.show = show; export default Drawer;