ant-design/components/modal/confirm.tsx
叶枫 8950642664
feat: config support holderRender (#46596)
* feat: config support container

* feat: add test

* feat: reset demo

* feat: test

* feat: test

* feat: test

* feat: test

* feat: md

* feat: limit

* fix: prefix

* feat: limit

* feat: message support app config

* feat: notification support app config

* feat: add maxCount test

* feat: icons

* feat: 增加 2 个 todo

* feat: holderRender

* feat: md

* feat: message 兼容旧逻辑

* feat: notification 兼容完成

* feat: modal 兼容完成

* feat: 优化 modal

* feat: 优化代码

* feat: 优化代码

* feat: 给 CP 添加 demo

* feat: 优先级完成/demo迁移完成

* feat: review

* feat: review

* feat: 优化 modal 代码

* feat: 支持 rtl

* feat: 修改注释

* feat: 优化单测

* feat: 优先级注释

* feat: remove getPrefixCls

* feat: demo

* feat: demo

* feat: demo

---------

Co-authored-by: MadCcc <madccc@foxmail.com>
2024-01-02 17:43:27 +08:00

198 lines
5.2 KiB
TypeScript

import React, { useContext } from 'react';
import { render as reactRender, unmount as reactUnmount } from 'rc-util/lib/React/render';
import warning from '../_util/warning';
import ConfigProvider, { ConfigContext, globalConfig, warnContext } from '../config-provider';
import type { ConfirmDialogProps } from './ConfirmDialog';
import ConfirmDialog from './ConfirmDialog';
import destroyFns from './destroyFns';
import type { ModalFuncProps } from './interface';
import { getConfirmLocale } from './locale';
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>;
const ConfirmDialogWrapper: React.FC<ConfirmDialogProps> = (props) => {
const { prefixCls: customizePrefixCls, getContainer, direction } = props;
const runtimeLocale = getConfirmLocale();
const config = useContext(ConfigContext);
const rootPrefixCls = customizePrefixCls || 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') {
warning(
false,
'Modal',
'Static method not support `getContainer` to be `false` since it do not have context env.',
);
}
}
return (
<ConfirmDialog
{...props}
rootPrefixCls={rootPrefixCls}
prefixCls={prefixCls}
iconPrefixCls={config.iconPrefixCls}
theme={config.theme}
direction={direction ?? config.direction}
locale={config.locale?.Modal ?? runtimeLocale}
getContainer={mergedGetContainer}
/>
);
};
export default function confirm(config: ModalFuncProps) {
const global = globalConfig();
if (process.env.NODE_ENV !== 'production' && !global.holderRender) {
warnContext('Modal');
}
const container = document.createDocumentFragment();
// eslint-disable-next-line @typescript-eslint/no-use-before-define
let currentConfig = { ...config, close, open: true } as any;
let timeoutId: ReturnType<typeof setTimeout>;
function destroy(...args: any[]) {
const triggerCancel = args.some((param) => param && param.triggerCancel);
if (config.onCancel && triggerCancel) {
config.onCancel(() => {}, ...args.slice(1));
}
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(props: any) {
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.getRootPrefixCls();
const iconPrefixCls = global.getIconPrefixCls();
const theme = global.getTheme();
const dom = <ConfirmDialogWrapper {...props} />;
reactRender(
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={iconPrefixCls} theme={theme}>
{global.holderRender ? global.holderRender(dom) : dom}
</ConfigProvider>,
container,
);
});
}
function close(...args: any[]) {
currentConfig = {
...currentConfig,
open: false,
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: 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 {
...props,
type: 'warning',
};
}
export function withInfo(props: ModalFuncProps): ModalFuncProps {
return {
...props,
type: 'info',
};
}
export function withSuccess(props: ModalFuncProps): ModalFuncProps {
return {
...props,
type: 'success',
};
}
export function withError(props: ModalFuncProps): ModalFuncProps {
return {
...props,
type: 'error',
};
}
export function withConfirm(props: ModalFuncProps): ModalFuncProps {
return {
...props,
type: 'confirm',
};
}
export function modalGlobalConfig({ rootPrefixCls }: { rootPrefixCls: string }) {
warning(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.');
defaultRootPrefixCls = rootPrefixCls;
}