2017-11-17 14:38:54 +08:00
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2020-03-02 12:09:38 +08:00
|
|
|
|
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';
|
2016-12-19 14:01:52 +08:00
|
|
|
|
import { getConfirmLocale } from './locale';
|
2021-08-17 16:22:42 +08:00
|
|
|
|
import type { ModalFuncProps } from './Modal';
|
2020-02-03 13:00:35 +08:00
|
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2021-02-09 21:49:15 +08:00
|
|
|
|
import { globalConfig } from '../config-provider';
|
2021-03-02 11:53:12 +08:00
|
|
|
|
import devWarning from '../_util/devWarning';
|
2021-08-17 16:22:42 +08:00
|
|
|
|
import destroyFns from './destroyFns';
|
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) {
|
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
|
2018-08-25 21:51:19 +08:00
|
|
|
|
let currentConfig = { ...config, close, visible: true } as any;
|
2017-12-02 22:10:27 +08:00
|
|
|
|
|
|
|
|
|
function destroy(...args: any[]) {
|
2021-11-19 22:43:44 +08:00
|
|
|
|
ReactDOM.unmountComponentAtNode(container);
|
2018-12-12 03:51:31 +08:00
|
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2017-12-02 22:10:27 +08:00
|
|
|
|
if (config.onCancel && triggerCancel) {
|
|
|
|
|
config.onCancel(...args);
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-02 22:10:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 21:49:15 +08:00
|
|
|
|
function render({ okText, cancelText, prefixCls: customizePrefixCls, ...props }: any) {
|
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.
|
|
|
|
|
*/
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
2021-06-09 15:36:59 +08:00
|
|
|
|
const { getPrefixCls, getIconPrefixCls } = 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();
|
2021-02-09 21:49:15 +08:00
|
|
|
|
|
2020-05-03 00:53:32 +08:00
|
|
|
|
ReactDOM.render(
|
|
|
|
|
<ConfirmDialog
|
|
|
|
|
{...props}
|
2021-02-09 21:49:15 +08:00
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
rootPrefixCls={rootPrefixCls}
|
2021-06-09 15:36:59 +08:00
|
|
|
|
iconPrefixCls={iconPrefixCls}
|
2020-05-03 00:53:32 +08:00
|
|
|
|
okText={okText || (props.okCancel ? runtimeLocale.okText : runtimeLocale.justOkText)}
|
|
|
|
|
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,
|
|
|
|
|
visible: false,
|
2020-12-07 13:44:06 +08:00
|
|
|
|
afterClose: () => {
|
|
|
|
|
if (typeof config.afterClose === 'function') {
|
|
|
|
|
config.afterClose();
|
|
|
|
|
}
|
|
|
|
|
destroy.apply(this, args);
|
|
|
|
|
},
|
2019-08-05 18:38:10 +08:00
|
|
|
|
};
|
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 {
|
|
|
|
|
icon: <ExclamationCircleOutlined />,
|
|
|
|
|
okCancel: false,
|
|
|
|
|
...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 {
|
|
|
|
|
icon: <InfoCircleOutlined />,
|
|
|
|
|
okCancel: false,
|
|
|
|
|
...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 {
|
|
|
|
|
icon: <CheckCircleOutlined />,
|
|
|
|
|
okCancel: false,
|
|
|
|
|
...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 {
|
|
|
|
|
icon: <CloseCircleOutlined />,
|
|
|
|
|
okCancel: false,
|
|
|
|
|
...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 {
|
2020-06-12 18:11:42 +08:00
|
|
|
|
icon: <ExclamationCircleOutlined />,
|
2020-02-03 13:00:35 +08:00
|
|
|
|
okCancel: true,
|
|
|
|
|
...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 }) {
|
2021-03-02 11:53:12 +08:00
|
|
|
|
devWarning(
|
|
|
|
|
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
|
|
|
|
}
|