antd
Version:
An enterprise-class UI design language and React components implementation
175 lines (174 loc) • 5.5 kB
JavaScript
;
"use client";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = confirm;
exports.withWarn = exports.withSuccess = exports.withInfo = exports.withError = exports.withConfirm = exports.modalGlobalConfig = void 0;
var _react = _interopRequireWildcard(require("react"));
var _util = require("@rc-component/util");
var _is = require("../_util/is");
var _warning = _interopRequireDefault(require("../_util/warning"));
var _configProvider = _interopRequireWildcard(require("../config-provider"));
var _ConfirmDialog = _interopRequireDefault(require("./ConfirmDialog"));
var _destroyFns = _interopRequireDefault(require("./destroyFns"));
var _locale = require("./locale");
let defaultRootPrefixCls = '';
const getRootPrefixCls = () => {
return defaultRootPrefixCls;
};
const ConfirmDialogWrapper = props => {
const {
prefixCls: customizePrefixCls,
getContainer,
direction
} = props;
const runtimeLocale = (0, _locale.getConfirmLocale)();
const config = (0, _react.useContext)(_configProvider.ConfigContext);
const rootPrefixCls = getRootPrefixCls() || config.getPrefixCls();
// because Modal.config set rootPrefixCls, which is different from other components
const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
let mergedGetContainer = getContainer;
if (mergedGetContainer === false) {
mergedGetContainer = undefined;
if (process.env.NODE_ENV !== 'production') {
process.env.NODE_ENV !== "production" ? (0, _warning.default)(false, 'Modal', 'Static method not support `getContainer` to be `false` since it do not have context env.') : void 0;
}
}
return /*#__PURE__*/_react.default.createElement(_ConfirmDialog.default, {
...props,
rootPrefixCls: rootPrefixCls,
prefixCls: prefixCls,
iconPrefixCls: config.iconPrefixCls,
theme: config.theme,
direction: direction ?? config.direction,
locale: config.locale?.Modal ?? runtimeLocale,
getContainer: mergedGetContainer
});
};
function confirm(config) {
const global = (0, _configProvider.globalConfig)();
if (process.env.NODE_ENV !== 'production' && !global.holderRender) {
(0, _configProvider.warnContext)('Modal');
}
const container = document.createDocumentFragment();
let closeDialog;
let currentConfig;
let timeoutId;
const destroy = (...args) => {
const triggerCancel = args.some(param => param?.triggerCancel);
if (triggerCancel) {
config.onCancel?.(() => {}, ...args.slice(1));
}
for (let i = 0; i < _destroyFns.default.length; i++) {
const fn = _destroyFns.default[i];
if (fn === closeDialog) {
_destroyFns.default.splice(i, 1);
break;
}
}
(0, _util.unmount)(container).then(() => {
// Do nothing
});
};
const scheduleRender = props => {
clearTimeout(timeoutId);
/**
* https://github.com/ant-design/ant-design/issues/23623
*
* Sync render blocks React event. Let's make this async.
*/
timeoutId = setTimeout(() => {
const rootPrefixCls = global.getPrefixCls(undefined, getRootPrefixCls());
const iconPrefixCls = global.getIconPrefixCls();
const theme = global.getTheme();
const dom = /*#__PURE__*/_react.default.createElement(ConfirmDialogWrapper, {
...props
});
(0, _util.render)(/*#__PURE__*/_react.default.createElement(_configProvider.default, {
prefixCls: rootPrefixCls,
iconPrefixCls: iconPrefixCls,
theme: theme
}, (0, _is.isFunction)(global.holderRender) ? global.holderRender(dom) : dom), container);
});
};
closeDialog = (...args) => {
currentConfig = {
...currentConfig,
open: false,
afterClose: () => {
if ((0, _is.isFunction)(config?.afterClose)) {
config.afterClose();
}
destroy.apply(void 0, args);
}
};
scheduleRender(currentConfig);
};
currentConfig = {
...config,
close: closeDialog,
open: true
};
const updateDialog = configUpdate => {
if ((0, _is.isFunction)(configUpdate)) {
currentConfig = configUpdate(currentConfig);
} else {
currentConfig = {
...currentConfig,
...configUpdate
};
}
scheduleRender(currentConfig);
};
scheduleRender(currentConfig);
_destroyFns.default.push(closeDialog);
return {
destroy: closeDialog,
update: updateDialog
};
}
const withWarn = props => {
return {
...props,
type: 'warning'
};
};
exports.withWarn = withWarn;
const withInfo = props => {
return {
...props,
type: 'info'
};
};
exports.withInfo = withInfo;
const withSuccess = props => {
return {
...props,
type: 'success'
};
};
exports.withSuccess = withSuccess;
const withError = props => {
return {
...props,
type: 'error'
};
};
exports.withError = withError;
const withConfirm = props => {
return {
...props,
type: 'confirm'
};
};
exports.withConfirm = withConfirm;
const modalGlobalConfig = ({
rootPrefixCls
}) => {
process.env.NODE_ENV !== "production" ? (0, _warning.default)(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.') : void 0;
defaultRootPrefixCls = rootPrefixCls;
};
exports.modalGlobalConfig = modalGlobalConfig;