2021-09-08 09:12:11 +00:00
|
|
|
import { createVNode, render as vueRender } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { ModalFuncProps } from './Modal';
|
|
|
|
import { destroyFns } from './Modal';
|
2021-10-26 08:12:56 +00:00
|
|
|
import ConfigProvider, { globalConfigForApi } from '../config-provider';
|
2021-09-25 08:51:32 +00:00
|
|
|
import omit from '../_util/omit';
|
2023-01-28 02:02:40 +00:00
|
|
|
|
|
|
|
import { getConfirmLocale } from './locale';
|
2022-01-06 08:08:53 +00:00
|
|
|
|
|
|
|
type ConfigUpdate = ModalFuncProps | ((prevConfig: ModalFuncProps) => ModalFuncProps);
|
|
|
|
|
|
|
|
export type ModalFunc = (props: ModalFuncProps) => {
|
|
|
|
destroy: () => void;
|
|
|
|
update: (configUpdate: ConfigUpdate) => void;
|
|
|
|
};
|
2020-06-15 15:47:49 +00:00
|
|
|
|
2021-09-08 09:12:11 +00:00
|
|
|
const confirm = (config: ModalFuncProps) => {
|
2022-01-06 08:08:53 +00:00
|
|
|
const container = document.createDocumentFragment();
|
2021-09-08 09:12:11 +00:00
|
|
|
let currentConfig = {
|
2021-09-25 08:51:32 +00:00
|
|
|
...omit(config, ['parentContext', 'appContext']),
|
2021-09-08 09:12:11 +00:00
|
|
|
close,
|
2023-01-28 02:02:40 +00:00
|
|
|
open: true,
|
2021-09-08 09:12:11 +00:00
|
|
|
} as any;
|
2019-01-12 03:33:27 +00:00
|
|
|
let confirmDialogInstance = null;
|
2020-10-19 15:09:36 +00:00
|
|
|
function destroy(...args: any[]) {
|
2022-01-06 08:08:53 +00:00
|
|
|
if (confirmDialogInstance) {
|
2021-09-25 08:51:32 +00:00
|
|
|
// destroy
|
2022-01-06 08:08:53 +00:00
|
|
|
vueRender(null, container as any);
|
2021-09-08 09:12:11 +00:00
|
|
|
confirmDialogInstance.component.update();
|
2019-01-12 03:33:27 +00:00
|
|
|
confirmDialogInstance = null;
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2018-03-06 11:14:41 +00:00
|
|
|
if (config.onCancel && triggerCancel) {
|
2023-01-28 02:02:40 +00:00
|
|
|
config.onCancel(() => {}, ...args.slice(1));
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
2019-04-17 02:21:28 +00:00
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
|
|
|
const fn = destroyFns[i];
|
|
|
|
if (fn === close) {
|
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
2022-01-06 08:08:53 +00:00
|
|
|
|
|
|
|
function close(this: typeof close, ...args: any[]) {
|
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
2023-01-28 02:02:40 +00:00
|
|
|
open: false,
|
2022-01-06 08:08:53 +00:00
|
|
|
afterClose: () => {
|
|
|
|
if (typeof config.afterClose === 'function') {
|
|
|
|
config.afterClose();
|
|
|
|
}
|
|
|
|
destroy.apply(this, args);
|
|
|
|
},
|
|
|
|
};
|
2023-01-28 02:02:40 +00:00
|
|
|
// Legacy support
|
|
|
|
if (currentConfig.visible) {
|
|
|
|
delete currentConfig.visible;
|
|
|
|
}
|
2022-01-06 08:08:53 +00:00
|
|
|
update(currentConfig);
|
|
|
|
}
|
|
|
|
function update(configUpdate: ConfigUpdate) {
|
|
|
|
if (typeof configUpdate === 'function') {
|
|
|
|
currentConfig = configUpdate(currentConfig);
|
|
|
|
} else {
|
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
...configUpdate,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (confirmDialogInstance) {
|
|
|
|
Object.assign(confirmDialogInstance.component.props, currentConfig);
|
|
|
|
confirmDialogInstance.component.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const Wrapper = (p: ModalFuncProps) => {
|
2021-10-26 08:12:56 +00:00
|
|
|
const global = globalConfigForApi;
|
|
|
|
const rootPrefixCls = global.prefixCls;
|
2021-09-25 08:51:32 +00:00
|
|
|
const prefixCls = p.prefixCls || `${rootPrefixCls}-modal`;
|
2023-01-28 02:02:40 +00:00
|
|
|
const iconPrefixCls = global.iconPrefixCls;
|
|
|
|
const runtimeLocale = getConfirmLocale();
|
2021-09-25 08:51:32 +00:00
|
|
|
return (
|
2023-01-27 08:00:17 +00:00
|
|
|
<ConfigProvider {...(global as any)} prefixCls={rootPrefixCls}>
|
2023-01-28 02:02:40 +00:00
|
|
|
<ConfirmDialog
|
|
|
|
{...p}
|
|
|
|
rootPrefixCls={rootPrefixCls}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
iconPrefixCls={iconPrefixCls}
|
|
|
|
locale={runtimeLocale}
|
|
|
|
cancelText={p.cancelText || runtimeLocale.cancelText}
|
|
|
|
></ConfirmDialog>
|
2021-09-25 08:51:32 +00:00
|
|
|
</ConfigProvider>
|
|
|
|
);
|
2021-09-08 09:12:11 +00:00
|
|
|
};
|
2020-10-19 15:09:36 +00:00
|
|
|
function render(props: ModalFuncProps) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const vm = createVNode(Wrapper, { ...props });
|
2021-09-08 09:12:11 +00:00
|
|
|
vm.appContext = config.parentContext || config.appContext || vm.appContext;
|
2022-01-06 08:08:53 +00:00
|
|
|
vueRender(vm, container as any);
|
2021-09-08 09:12:11 +00:00
|
|
|
return vm;
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
confirmDialogInstance = render(currentConfig);
|
2019-04-17 02:21:28 +00:00
|
|
|
destroyFns.push(close);
|
2018-03-06 11:14:41 +00:00
|
|
|
return {
|
|
|
|
destroy: close,
|
2018-12-09 09:34:27 +00:00
|
|
|
update,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-09-08 09:12:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default confirm;
|
2022-01-06 08:08:53 +00:00
|
|
|
|
|
|
|
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',
|
|
|
|
};
|
|
|
|
}
|