linkmore-design
Version:
π πlmη»δ»ΆεΊγπ
159 lines (157 loc) β’ 5.62 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
var _excluded = ["okText", "cancelText", "prefixCls"];
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
import { render as reactRender, unmount as reactUnmount } from 'rc-util/lib/React/render';
import * as React from 'react';
import { globalConfig } from "../config-provider";
import warning from "../_util/warning";
import ConfirmDialog from "./ConfirmDialog";
import destroyFns from "./destroyFns";
import { getConfirmLocale } from "./locale";
var defaultRootPrefixCls = '';
function getRootPrefixCls() {
return defaultRootPrefixCls;
}
export default function confirm(config) {
var container = document.createDocumentFragment();
// eslint-disable-next-line @typescript-eslint/no-use-before-define
var currentConfig = _objectSpread(_objectSpread({}, config), {}, {
close: close,
open: true
});
var timeoutId;
function destroy() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var triggerCancel = args.some(function (param) {
return param && param.triggerCancel;
});
if (config.onCancel && triggerCancel) {
config.onCancel.apply(config, [function () {}].concat(_toConsumableArray(args.slice(1))));
}
for (var i = 0; i < destroyFns.length; i++) {
var fn = destroyFns[i];
// eslint-disable-next-line @typescript-eslint/no-use-before-define
if (fn === close) {
destroyFns.splice(i, 1);
break;
}
}
reactUnmount(container);
}
function render(_ref) {
var okText = _ref.okText,
cancelText = _ref.cancelText,
customizePrefixCls = _ref.prefixCls,
props = _objectWithoutProperties(_ref, _excluded);
clearTimeout(timeoutId);
/**
* https://github.com/ant-design/ant-design/issues/23623
*
* Sync render blocks React event. Let's make this async.
*/
timeoutId = setTimeout(function () {
var runtimeLocale = getConfirmLocale();
var _globalConfig = globalConfig(),
getPrefixCls = _globalConfig.getPrefixCls,
getIconPrefixCls = _globalConfig.getIconPrefixCls;
// because Modal.config set rootPrefixCls, which is different from other components
var rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
var prefixCls = customizePrefixCls || "".concat(rootPrefixCls, "-modal");
var iconPrefixCls = getIconPrefixCls();
reactRender( /*#__PURE__*/React.createElement(ConfirmDialog, _extends({}, props, {
prefixCls: prefixCls,
rootPrefixCls: rootPrefixCls,
iconPrefixCls: iconPrefixCls,
okText: okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText),
cancelText: cancelText || runtimeLocale.cancelText
})), container);
});
}
function close() {
var _this = this;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
currentConfig = _objectSpread(_objectSpread({}, currentConfig), {}, {
open: false,
afterClose: function afterClose() {
if (typeof config.afterClose === 'function') {
config.afterClose();
}
destroy.apply(_this, args);
}
});
// Legacy support
if (currentConfig.visible) {
delete currentConfig.visible;
}
render(currentConfig);
}
function update(configUpdate) {
if (typeof configUpdate === 'function') {
currentConfig = configUpdate(currentConfig);
} else {
currentConfig = _objectSpread(_objectSpread({}, currentConfig), configUpdate);
}
render(currentConfig);
}
render(currentConfig);
destroyFns.push(close);
return {
destroy: close,
update: update
};
}
export function withWarn(props) {
return _objectSpread(_objectSpread({
icon: /*#__PURE__*/React.createElement(ExclamationCircleOutlined, null),
okCancel: false
}, props), {}, {
type: 'warning'
});
}
export function withInfo(props) {
return _objectSpread(_objectSpread({
icon: /*#__PURE__*/React.createElement(InfoCircleOutlined, null),
okCancel: false
}, props), {}, {
type: 'info'
});
}
export function withSuccess(props) {
return _objectSpread(_objectSpread({
icon: /*#__PURE__*/React.createElement(CheckCircleOutlined, null),
okCancel: false
}, props), {}, {
type: 'success'
});
}
export function withError(props) {
return _objectSpread(_objectSpread({
icon: /*#__PURE__*/React.createElement(CloseCircleOutlined, null),
okCancel: false
}, props), {}, {
type: 'error'
});
}
export function withConfirm(props) {
return _objectSpread(_objectSpread({
icon: /*#__PURE__*/React.createElement(ExclamationCircleOutlined, null),
okCancel: true
}, props), {}, {
type: 'confirm'
});
}
export function modalGlobalConfig(_ref2) {
var rootPrefixCls = _ref2.rootPrefixCls;
warning(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.');
defaultRootPrefixCls = rootPrefixCls;
}