UNPKG

@alicloud/console-components

Version:
246 lines (245 loc) 12.9 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 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, filterProps = __rest(props // console.log('locale', locale) , ["visible", "onOk", "onCancel", "renderFooter", "hasFooterLine", "footerAlign", "children", "locale", "okText", "cancelText", "cancelBtnProps", "okBtnProps", "footerClass", "size", "width", "className", "actionRef"]); // console.log('locale', locale) var _k = useState(visible), customVisible = _k[0], setCustomVisible = _k[1]; var _l = useState(false), okLoadingState = _l[0], setOkLoadingState = _l[1]; var _m = useState(false), cancelLoadingState = _m[0], setCancelLoadingState = _m[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('next-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('next-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('next-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('next-drawer-footer-has-shadow'); } else { drawerFooterDom.classList.remove('next-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('next-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 = { 'next-drawer-has-footer': onOk || onCancel || renderFooter }, _a[className] = !!className, _a)); var drawerFooterClassName = cls((_b = { 'next-drawer-footer': true, 'next-drawer-footer-line': hasFooterLine, 'next-drawer-footer-right': footerAlign === 'right', 'next-drawer-footer-left': footerAlign === 'left', 'next-drawer-footer-center': footerAlign === 'center' }, _b[footerClass] = !!footerClass, _b)); var getCustomWidth = function () { if (width) { return width; } if (size) { switch (size) { case 'mini': return 400; case 'small': return 600; case 'medium': return 800; case 'large': return 1200; default: return 400; } } }; return (React.createElement(NextDrawer, __assign({}, filterProps, { ref: ref ? ref : customRef, visible: customVisible, width: getCustomWidth(), className: drawerCustomClassName }), children, (onOk || onCancel || renderFooter) && (React.createElement("div", { className: drawerFooterClassName }, onOk && !renderFooter && (React.createElement(Button, __assign({ type: "primary", onClick: onOk, style: { marginRight: 8 }, loading: okLoadingState }, okBtnProps), okText)), onCancel && !renderFooter && (React.createElement(Button, __assign({ onClick: onCancel, 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 customOnOK; var customOnCancel; var actionRef; // 合并 customOnOK。 如果返回值为 true 自动关闭 Drawer customOnOK = function (event) { var _a, _b, _c; if (onOk) { 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; if (onCancel) { var result = onCancel === null || onCancel === void 0 ? void 0 : onCancel(event); if (result instanceof Promise) { (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setCancelLoading) === null || _a === void 0 ? void 0 : _a.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) { (_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); } }; var ConfigModal = ConfigProvider.config(Drawer, { componentName: 'Drawer' }); var container = document.createElement('div'); container.setAttribute('id', 'next-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: customOnOK, onCancel: customOnCancel, 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;