@alicloud/console-components
Version:
Alibaba Cloud React Components
226 lines (225 loc) • 9.77 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 __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 { Dialog as NextDialog } from '@alifd/next';
import ReactDOM from 'react-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
import React, { useEffect, useRef } from 'react';
import { useCssVar } from '../utils/useCssVar';
var sizeMap = {
mini: 400,
small: 600,
medium: 800,
large: 1200
};
var yunxiaoSizeMap = {
mini: 440,
small: 600,
medium: 800,
large: 1200
};
// 获取 size 大小
var getCustomWidth = function (size, theme) {
var sizeWidth = theme.startsWith('yunxiao') ? yunxiaoSizeMap[size] : sizeMap[size];
if (sizeWidth) {
return {
style: {
width: sizeWidth + "px"
}
};
}
return {};
};
// 设置阴影
var setFooterShadow = function (dom, prefix) {
var _a, _b;
if (dom) {
var dialogBodyDom = (_a = dom === null || dom === void 0 ? void 0 : dom.getElementsByClassName(prefix + "dialog-body")) === null || _a === void 0 ? void 0 : _a[0];
var dialogFooterDom = (_b = dom === null || dom === void 0 ? void 0 : dom.getElementsByClassName(prefix + "dialog-footer")) === null || _b === void 0 ? void 0 : _b[0];
if (dialogFooterDom) {
if ((dialogBodyDom === null || dialogBodyDom === void 0 ? void 0 : dialogBodyDom.clientHeight) < (dialogBodyDom === null || dialogBodyDom === void 0 ? void 0 : dialogBodyDom.scrollHeight)) {
dialogFooterDom.classList.add(prefix + "dialog-footer-has-shadow");
}
else {
dialogFooterDom.classList.remove(prefix + "dialog-footer-has-shadow");
}
}
}
};
var Dialog = function (props) {
var size = props.size, others = __rest(props, ["size"]);
var _a = props.prefix, prefix = _a === void 0 ? 'next-' : _a;
var theme = useCssVar('--alicloudfe-components-theme').trim();
var customRef = useRef(null);
// 有滚动条时底部显示阴影
var setFooterShadowOfRef = function () {
if (theme !== 'wind' && !theme.startsWith('xconsole')) {
var dialogDom = ReactDOM.findDOMNode(customRef.current);
setFooterShadow(dialogDom, prefix);
}
};
var observer = null;
// 绑定监听器
useEffect(function () {
var _a;
setFooterShadowOfRef();
var drawerDom = ReactDOM.findDOMNode(customRef.current);
var drawerBodyDom = (_a = drawerDom === null || drawerDom === void 0 ? void 0 : drawerDom.getElementsByClassName(prefix + "dialog-body")) === null || _a === void 0 ? void 0 : _a[0];
if (drawerBodyDom && !observer) {
observer = new MutationObserver(function () {
setFooterShadowOfRef();
});
observer.observe(drawerBodyDom, {
attributes: true,
attributeFilter: ['style'],
attributeOldValue: true,
childList: true,
subtree: true
});
}
// 销毁
return function () {
if (observer) {
observer.disconnect();
observer.takeRecords();
observer = null;
}
};
});
// 云效混合云主题样式主操作在右边
var defaultFooterActions = (function () {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark' ||
theme === 'hybridcloud' ||
theme === 'hybridcloud-dark')
return ['cancel', 'ok'];
return ['ok', 'cancel'];
})();
// 云效主题align在上方
var defaultAlign = (function () {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark')
return 'tc tc';
return 'cc cc';
})();
var defaultMinMargin = (function () {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark')
return 100;
return 40;
})();
return (React.createElement(NextDialog, __assign({}, getCustomWidth(size, theme), { footerActions: defaultFooterActions, align: defaultAlign, minMargin: defaultMinMargin, shouldUpdatePosition: true }, others, { ref: customRef })));
};
var showDefaultFooterActions = function (theme) {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark' ||
theme === 'hybridcloud' ||
theme === 'hybridcloud-dark')
return ['cancel', 'ok'];
return ['ok', 'cancel'];
};
var showDefaultAlign = function (theme) {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark')
return 'tc tc';
return 'cc cc';
};
var showDefaultMinMargin = function (theme) {
if (theme === 'yunxiao' ||
theme === 'yunxiao-dark')
return 100;
return 40;
};
// 快捷调用的操作按钮顺序
var show = function (config) {
var _a;
var size = config.size, others = __rest(config, ["size"]);
var _b = config.prefix, prefix = _b === void 0 ? 'next-' : _b;
var theme = (_a = window
.getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim();
setTimeout(function () {
var _a, _b;
var doms = ((_a = document.getElementsByClassName('quick-show')) !== null && _a !== void 0 ? _a : []);
var _loop_1 = function (item) {
// 初始化判断是否有阴影
setFooterShadow(item, prefix);
// 绑定body监听器。监听高度变化
var dialogBodyDom = (_b = item === null || item === void 0 ? void 0 : item.getElementsByClassName(prefix + "dialog-body")) === null || _b === void 0 ? void 0 : _b[0];
var observer = new MutationObserver(function () {
setFooterShadow(item, prefix);
});
observer.observe(dialogBodyDom, {
attributes: true,
attributeFilter: ['style'],
attributeOldValue: true,
childList: true,
subtree: true
});
// 绑定dialog监听器,用于监听dialog被销毁时销毁所有监听器
var domObserver = new MutationObserver(function () {
var _a, _b;
if (((_b = (_a = item === null || item === void 0 ? void 0 : item.parentNode) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.tagName) !== 'BODY') {
observer.disconnect();
observer.takeRecords();
observer = null;
domObserver.disconnect();
domObserver.takeRecords();
domObserver = null;
}
});
domObserver.observe(document.body, {
attributes: true,
attributeFilter: ['style'],
attributeOldValue: true,
childList: true,
subtree: true
});
};
for (var _i = 0, doms_1 = doms; _i < doms_1.length; _i++) {
var item = doms_1[_i];
_loop_1(item);
}
});
return NextDialog.show(__assign(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), shouldUpdatePosition: true }), others), {
// 将Dialog.show与其他quick弹窗区分出来,单独做样式覆盖,
// 因为它的body是不包含Message的
className: ['quick-show', config.className].filter(Boolean).join(' ') }));
};
var confirm = function (config) {
var _a;
var theme = (_a = window
.getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim();
var size = config.size, others = __rest(config, ["size"]);
return NextDialog.confirm(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), messageProps: { type: 'notice' }, shouldUpdatePosition: true }), others));
};
var alert = function (config) {
var _a;
var theme = (_a = window
.getComputedStyle) === null || _a === void 0 ? void 0 : _a.call(window, window.document.body).getPropertyValue('--alicloudfe-components-theme').trim();
var size = config.size, others = __rest(config, ["size"]);
return NextDialog.alert(__assign(__assign(__assign({}, getCustomWidth(size, theme)), { footerActions: showDefaultFooterActions(theme), align: showDefaultAlign(theme), minMargin: showDefaultMinMargin(theme), messageProps: { type: 'warning' }, shouldUpdatePosition: true }), others));
};
hoistNonReactStatics(Dialog, NextDialog, { show: true, confirm: true });
Dialog.show = show;
Dialog.confirm = confirm;
Dialog.alert = alert;
export default Dialog;