@alicloud/console-components
Version:
Alibaba Cloud React Components
270 lines (269 loc) • 14.4 kB
JavaScript
;
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 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, 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 = react_1.useState(visible), customVisible = _k[0], setCustomVisible = _k[1];
var _l = react_1.useState(false), okLoadingState = _l[0], setOkLoadingState = _l[1];
var _m = react_1.useState(false), cancelLoadingState = _m[0], setCancelLoadingState = _m[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('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;
// 绑定监听器
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('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;
}
};
});
react_1.useEffect(function () {
setCustomVisible(visible);
}, [visible]);
var drawerCustomClassName = classnames_1.default((_a = {
'next-drawer-has-footer': onOk || onCancel || renderFooter
},
_a[className] = !!className,
_a));
var drawerFooterClassName = classnames_1.default((_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_1.default.createElement(next_1.Drawer, __assign({}, filterProps, { ref: ref ? ref : customRef, visible: customVisible, width: getCustomWidth(), className: drawerCustomClassName }),
children,
(onOk || onCancel || renderFooter) && (react_1.default.createElement("div", { className: drawerFooterClassName },
onOk && !renderFooter && (react_1.default.createElement(button_1.default, __assign({ type: "primary", onClick: onOk, style: { marginRight: 8 }, loading: okLoadingState }, okBtnProps), okText)),
onCancel && !renderFooter && (react_1.default.createElement(button_1.default, __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 = next_1.ConfigProvider.config(Drawer, { componentName: 'Drawer' });
var container = document.createElement('div');
container.setAttribute('id', 'next-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: 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);
}
};
};
hoist_non_react_statics_1.default(Drawer, next_1.Drawer);
// @ts-ignore
Drawer.displayName = next_1.Drawer.displayName;
Drawer.show = show;
exports.default = Drawer;