2022-06-22 14:57:09 +08:00
|
|
|
|
import { render as reactRender, unmount as reactUnmount } from 'rc-util/lib/React/render';
|
2023-05-06 15:49:37 +08:00
|
|
|
|
import * as React from 'react';
|
2022-05-10 15:43:29 +08:00
|
|
|
|
import warning from '../_util/warning';
|
2023-05-18 23:53:34 +08:00
|
|
|
|
import { globalConfig, warnContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2021-08-17 16:22:42 +08:00
|
|
|
|
import destroyFns from './destroyFns';
|
2023-06-05 09:57:26 +08:00
|
|
|
|
import type { ModalFuncProps } from './interface';
|
2022-06-22 14:57:09 +08:00
|
|
|
|
import { getConfirmLocale } from './locale';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
|
let defaultRootPrefixCls = '';
|
2020-07-14 11:14:46 +08:00
|
|
|
|
|
2020-07-14 18:43:55 +08:00
|
|
|
|
function getRootPrefixCls() {
|
|
|
|
|
return defaultRootPrefixCls;
|
2020-07-14 11:14:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 14:35:00 +08:00
|
|
|
|
type ConfigUpdate = ModalFuncProps | ((prevConfig: ModalFuncProps) => ModalFuncProps);
|
|
|
|
|
|
2021-08-17 16:22:42 +08:00
|
|
|
|
export type ModalFunc = (props: ModalFuncProps) => {
|
2020-02-03 13:00:35 +08:00
|
|
|
|
destroy: () => void;
|
2020-10-24 14:35:00 +08:00
|
|
|
|
update: (configUpdate: ConfigUpdate) => void;
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
2016-08-14 15:19:47 +08:00
|
|
|
|
|
2021-01-24 17:40:30 +08:00
|
|
|
|
export type ModalStaticFunctions = Record<NonNullable<ModalFuncProps['type']>, ModalFunc>;
|
2015-09-18 16:00:12 +08:00
|
|
|
|
|
2017-12-02 22:10:27 +08:00
|
|
|
|
export default function confirm(config: ModalFuncProps) {
|
2023-03-10 14:12:49 +08:00
|
|
|
|
// Warning if exist theme
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
warnContext('Modal');
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 22:43:44 +08:00
|
|
|
|
const container = document.createDocumentFragment();
|
2020-09-16 11:43:55 +08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
2022-08-23 16:55:57 +08:00
|
|
|
|
let currentConfig = { ...config, close, open: true } as any;
|
2022-09-29 16:23:38 +08:00
|
|
|
|
let timeoutId: NodeJS.Timeout;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
|
|
function destroy(...args: any[]) {
|
2022-11-19 13:47:33 +08:00
|
|
|
|
const triggerCancel = args.some((param) => param && param.triggerCancel);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
if (config.onCancel && triggerCancel) {
|
2022-07-23 16:04:23 +08:00
|
|
|
|
config.onCancel(() => {}, ...args.slice(1));
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
2018-12-20 17:43:22 +08:00
|
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
2018-12-26 21:36:36 +08:00
|
|
|
|
const fn = destroyFns[i];
|
2020-09-16 11:43:55 +08:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
2019-01-30 10:46:30 +08:00
|
|
|
|
if (fn === close) {
|
2018-12-26 21:36:36 +08:00
|
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
|
break;
|
2018-12-03 17:08:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
|
|
|
|
reactUnmount(container);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 14:24:29 +08:00
|
|
|
|
function render({
|
|
|
|
|
okText,
|
|
|
|
|
cancelText,
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
getContainer,
|
|
|
|
|
...props
|
|
|
|
|
}: any) {
|
2022-09-29 16:23:38 +08:00
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
|
2020-05-03 00:53:32 +08:00
|
|
|
|
/**
|
|
|
|
|
* https://github.com/ant-design/ant-design/issues/23623
|
2020-12-28 15:30:18 +08:00
|
|
|
|
*
|
2020-05-03 00:53:32 +08:00
|
|
|
|
* Sync render blocks React event. Let's make this async.
|
|
|
|
|
*/
|
2022-09-29 16:23:38 +08:00
|
|
|
|
timeoutId = setTimeout(() => {
|
2020-05-03 00:53:32 +08:00
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
2023-05-18 23:53:34 +08:00
|
|
|
|
const { getPrefixCls, getIconPrefixCls, getTheme } = globalConfig();
|
2021-02-09 21:49:15 +08:00
|
|
|
|
// because Modal.config set rootPrefixCls, which is different from other components
|
|
|
|
|
const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
|
|
|
|
|
const prefixCls = customizePrefixCls || `${rootPrefixCls}-modal`;
|
2021-06-09 15:36:59 +08:00
|
|
|
|
const iconPrefixCls = getIconPrefixCls();
|
2023-05-18 23:53:34 +08:00
|
|
|
|
const theme = getTheme();
|
2021-02-09 21:49:15 +08:00
|
|
|
|
|
2023-05-25 14:24:29 +08:00
|
|
|
|
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.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:07:15 +08:00
|
|
|
|
reactRender(
|
2020-05-03 00:53:32 +08:00
|
|
|
|
<ConfirmDialog
|
|
|
|
|
{...props}
|
2023-05-25 14:24:29 +08:00
|
|
|
|
getContainer={mergedGetContainer}
|
2021-02-09 21:49:15 +08:00
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
rootPrefixCls={rootPrefixCls}
|
2021-06-09 15:36:59 +08:00
|
|
|
|
iconPrefixCls={iconPrefixCls}
|
2022-07-15 20:51:07 +08:00
|
|
|
|
okText={okText}
|
|
|
|
|
locale={runtimeLocale}
|
2023-05-18 23:53:34 +08:00
|
|
|
|
theme={theme}
|
2020-05-03 00:53:32 +08:00
|
|
|
|
cancelText={cancelText || runtimeLocale.cancelText}
|
|
|
|
|
/>,
|
2021-11-19 22:43:44 +08:00
|
|
|
|
container,
|
2020-05-03 00:53:32 +08:00
|
|
|
|
);
|
|
|
|
|
});
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
|
function close(...args: any[]) {
|
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
2022-08-23 16:55:57 +08:00
|
|
|
|
open: false,
|
2020-12-07 13:44:06 +08:00
|
|
|
|
afterClose: () => {
|
|
|
|
|
if (typeof config.afterClose === 'function') {
|
|
|
|
|
config.afterClose();
|
|
|
|
|
}
|
2022-04-06 11:07:15 +08:00
|
|
|
|
|
2020-12-07 13:44:06 +08:00
|
|
|
|
destroy.apply(this, args);
|
|
|
|
|
},
|
2019-08-05 18:38:10 +08:00
|
|
|
|
};
|
2022-09-18 11:22:05 +08:00
|
|
|
|
|
|
|
|
|
// Legacy support
|
|
|
|
|
if (currentConfig.visible) {
|
|
|
|
|
delete currentConfig.visible;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 13:00:35 +08:00
|
|
|
|
render(currentConfig);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 14:35:00 +08:00
|
|
|
|
function update(configUpdate: ConfigUpdate) {
|
|
|
|
|
if (typeof configUpdate === 'function') {
|
|
|
|
|
currentConfig = configUpdate(currentConfig);
|
|
|
|
|
} else {
|
|
|
|
|
currentConfig = {
|
|
|
|
|
...currentConfig,
|
|
|
|
|
...configUpdate,
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
render(currentConfig);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 21:51:19 +08:00
|
|
|
|
render(currentConfig);
|
2016-06-21 18:50:07 +08:00
|
|
|
|
|
2018-12-26 21:36:36 +08:00
|
|
|
|
destroyFns.push(close);
|
2018-12-03 17:08:48 +08:00
|
|
|
|
|
2016-06-21 18:50:07 +08:00
|
|
|
|
return {
|
|
|
|
|
destroy: close,
|
2018-08-25 21:51:19 +08:00
|
|
|
|
update,
|
2016-06-21 18:50:07 +08:00
|
|
|
|
};
|
2015-07-23 22:23:11 +08:00
|
|
|
|
}
|
2020-02-03 13:00:35 +08:00
|
|
|
|
|
|
|
|
|
export function withWarn(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
2021-01-24 17:40:30 +08:00
|
|
|
|
type: 'warning',
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function withInfo(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
2021-01-24 17:40:30 +08:00
|
|
|
|
type: 'info',
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function withSuccess(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
2021-01-24 17:40:30 +08:00
|
|
|
|
type: 'success',
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function withError(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
2021-01-24 17:40:30 +08:00
|
|
|
|
type: 'error',
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function withConfirm(props: ModalFuncProps): ModalFuncProps {
|
|
|
|
|
return {
|
|
|
|
|
...props,
|
2021-01-24 17:40:30 +08:00
|
|
|
|
type: 'confirm',
|
2020-02-03 13:00:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2020-07-14 11:14:46 +08:00
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
|
export function modalGlobalConfig({ rootPrefixCls }: { rootPrefixCls: string }) {
|
2022-05-10 15:43:29 +08:00
|
|
|
|
warning(false, 'Modal', 'Modal.config is deprecated. Please use ConfigProvider.config instead.');
|
2021-02-09 21:49:15 +08:00
|
|
|
|
defaultRootPrefixCls = rootPrefixCls;
|
2020-07-14 11:14:46 +08:00
|
|
|
|
}
|