mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
338ec7dad7
* pref: better code style for production * refactor `devWarning` * don't use `useEffect` only wrap `devWarning` * chore: add 'noop' to coverage * chore: add test cases for devWarning * chore: add test case * chore: update test cases for devWarning * chore: restore test script command * fix: remove 'throw new Error' * should not use `throw` for browser * chore: update test case for AutoComplete * perf: add prefix for `devWarning` * update RegExp for UMD * add prefix for ES and CJS * chore: better code style * perf: * upgrade antd-tools * remove `injectWarningCondition` * rename `devWarning` to `warning` * chore: better code style * chore: better code style * chore: restore hasValidName
165 lines
4.6 KiB
TypeScript
165 lines
4.6 KiB
TypeScript
import * as React from 'react';
|
||
import { render as reactRender, unmount as reactUnmount } from 'rc-util/lib/React/render';
|
||
import InfoCircleOutlined from '@ant-design/icons/InfoCircleOutlined';
|
||
import CheckCircleOutlined from '@ant-design/icons/CheckCircleOutlined';
|
||
import CloseCircleOutlined from '@ant-design/icons/CloseCircleOutlined';
|
||
import ExclamationCircleOutlined from '@ant-design/icons/ExclamationCircleOutlined';
|
||
import { getConfirmLocale } from './locale';
|
||
import type { ModalFuncProps } from './Modal';
|
||
import ConfirmDialog from './ConfirmDialog';
|
||
import { globalConfig } from '../config-provider';
|
||
import warning from '../_util/warning';
|
||
import destroyFns from './destroyFns';
|
||
|
||
let defaultRootPrefixCls = '';
|
||
|
||
function getRootPrefixCls() {
|
||
return defaultRootPrefixCls;
|
||
}
|
||
|
||
type ConfigUpdate = ModalFuncProps | ((prevConfig: ModalFuncProps) => ModalFuncProps);
|
||
|
||
export type ModalFunc = (props: ModalFuncProps) => {
|
||
destroy: () => void;
|
||
update: (configUpdate: ConfigUpdate) => void;
|
||
};
|
||
|
||
export type ModalStaticFunctions = Record<NonNullable<ModalFuncProps['type']>, ModalFunc>;
|
||
|
||
export default function confirm(config: ModalFuncProps) {
|
||
const container = document.createDocumentFragment();
|
||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||
let currentConfig = { ...config, close, visible: true } as any;
|
||
|
||
function destroy(...args: any[]) {
|
||
const triggerCancel = args.some(param => param && param.triggerCancel);
|
||
if (config.onCancel && triggerCancel) {
|
||
config.onCancel(...args);
|
||
}
|
||
for (let i = 0; i < destroyFns.length; i++) {
|
||
const 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({ okText, cancelText, prefixCls: customizePrefixCls, ...props }: any) {
|
||
/**
|
||
* https://github.com/ant-design/ant-design/issues/23623
|
||
*
|
||
* Sync render blocks React event. Let's make this async.
|
||
*/
|
||
setTimeout(() => {
|
||
const runtimeLocale = getConfirmLocale();
|
||
const { getPrefixCls, getIconPrefixCls } = globalConfig();
|
||
// because Modal.config set rootPrefixCls, which is different from other components
|
||
const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
|
||
const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
|
||
const iconPrefixCls = getIconPrefixCls();
|
||
|
||
reactRender(
|
||
<ConfirmDialog
|
||
{...props}
|
||
prefixCls={prefixCls}
|
||
rootPrefixCls={rootPrefixCls}
|
||
iconPrefixCls={iconPrefixCls}
|
||
okText={okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText)}
|
||
cancelText={cancelText || runtimeLocale.cancelText}
|
||
/>,
|
||
container,
|
||
);
|
||
});
|
||
}
|
||
|
||
function close(...args: any[]) {
|
||
currentConfig = {
|
||
...currentConfig,
|
||
visible: false,
|
||
afterClose: () => {
|
||
if (typeof config.afterClose === 'function') {
|
||
config.afterClose();
|
||
}
|
||
|
||
destroy.apply(this, args);
|
||
},
|
||
};
|
||
render(currentConfig);
|
||
}
|
||
|
||
function update(configUpdate: ConfigUpdate) {
|
||
if (typeof configUpdate === 'function') {
|
||
currentConfig = configUpdate(currentConfig);
|
||
} else {
|
||
currentConfig = {
|
||
...currentConfig,
|
||
...configUpdate,
|
||
};
|
||
}
|
||
render(currentConfig);
|
||
}
|
||
|
||
render(currentConfig);
|
||
|
||
destroyFns.push(close);
|
||
|
||
return {
|
||
destroy: close,
|
||
update,
|
||
};
|
||
}
|
||
|
||
export function withWarn(props: ModalFuncProps): ModalFuncProps {
|
||
return {
|
||
icon: <ExclamationCircleOutlined />,
|
||
okCancel: false,
|
||
...props,
|
||
type: 'warning',
|
||
};
|
||
}
|
||
|
||
export function withInfo(props: ModalFuncProps): ModalFuncProps {
|
||
return {
|
||
icon: <InfoCircleOutlined />,
|
||
okCancel: false,
|
||
...props,
|
||
type: 'info',
|
||
};
|
||
}
|
||
|
||
export function withSuccess(props: ModalFuncProps): ModalFuncProps {
|
||
return {
|
||
icon: <CheckCircleOutlined />,
|
||
okCancel: false,
|
||
...props,
|
||
type: 'success',
|
||
};
|
||
}
|
||
|
||
export function withError(props: ModalFuncProps): ModalFuncProps {
|
||
return {
|
||
icon: <CloseCircleOutlined />,
|
||
okCancel: false,
|
||
...props,
|
||
type: 'error',
|
||
};
|
||
}
|
||
|
||
export function withConfirm(props: ModalFuncProps): ModalFuncProps {
|
||
return {
|
||
icon: <ExclamationCircleOutlined />,
|
||
okCancel: true,
|
||
...props,
|
||
type: 'confirm',
|
||
};
|
||
}
|
||
|
||
export function modalGlobalConfig({ rootPrefixCls }: { rootPrefixCls: string }) {
|
||
warning(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.');
|
||
defaultRootPrefixCls = rootPrefixCls;
|
||
}
|