2021-09-08 17:12:11 +08:00
|
|
|
import { createVNode, render as vueRender } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { ModalFuncProps } from './Modal';
|
|
|
|
import { destroyFns } from './Modal';
|
2021-09-25 16:51:32 +08:00
|
|
|
import ConfigProvider, { globalConfig } from '../config-provider';
|
|
|
|
import omit from '../_util/omit';
|
2020-06-15 23:47:49 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
const defaultRootPrefixCls = '';
|
|
|
|
|
|
|
|
function getRootPrefixCls() {
|
|
|
|
return defaultRootPrefixCls;
|
|
|
|
}
|
2018-05-04 16:02:31 +08:00
|
|
|
|
2021-09-08 17:12:11 +08:00
|
|
|
const confirm = (config: ModalFuncProps) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
2021-09-08 17:12:11 +08:00
|
|
|
let currentConfig = {
|
2021-09-25 16:51:32 +08:00
|
|
|
...omit(config, ['parentContext', 'appContext']),
|
2021-09-08 17:12:11 +08:00
|
|
|
close,
|
|
|
|
visible: true,
|
|
|
|
} as any;
|
2018-03-07 21:36:15 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
let confirmDialogInstance = null;
|
2020-10-19 23:09:36 +08:00
|
|
|
function close(this: typeof close, ...args: any[]) {
|
2020-08-01 16:06:13 +08:00
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
visible: false,
|
|
|
|
afterClose: destroy.bind(this, ...args),
|
|
|
|
};
|
|
|
|
update(currentConfig);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2020-10-19 23:09:36 +08:00
|
|
|
function update(newConfig: ModalFuncProps) {
|
2018-12-09 17:34:27 +08:00
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
...newConfig,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-09-08 17:12:11 +08:00
|
|
|
if (confirmDialogInstance) {
|
|
|
|
Object.assign(confirmDialogInstance.component.props, currentConfig);
|
|
|
|
confirmDialogInstance.component.update();
|
|
|
|
}
|
2018-12-09 17:34:27 +08:00
|
|
|
}
|
2020-10-19 23:09:36 +08:00
|
|
|
function destroy(...args: any[]) {
|
2018-03-06 19:14:41 +08:00
|
|
|
if (confirmDialogInstance && div.parentNode) {
|
2021-09-25 16:51:32 +08:00
|
|
|
// destroy
|
|
|
|
vueRender(null, div);
|
2021-09-08 17:12:11 +08:00
|
|
|
confirmDialogInstance.component.update();
|
2019-01-12 11:33:27 +08:00
|
|
|
confirmDialogInstance = null;
|
|
|
|
div.parentNode.removeChild(div);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2018-03-06 19:14:41 +08:00
|
|
|
if (config.onCancel && triggerCancel) {
|
2019-01-12 11:33:27 +08:00
|
|
|
config.onCancel(...args);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2019-04-17 10:21:28 +08:00
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
|
|
|
const fn = destroyFns[i];
|
|
|
|
if (fn === close) {
|
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2021-09-25 16:51:32 +08:00
|
|
|
const Wrapper = (p: ModalFuncProps) => {
|
|
|
|
const { getPrefixCls } = globalConfig();
|
|
|
|
const rootPrefixCls = getPrefixCls(undefined, getRootPrefixCls());
|
|
|
|
const prefixCls = p.prefixCls || `${rootPrefixCls}-modal`;
|
|
|
|
return (
|
|
|
|
<ConfigProvider prefixCls={rootPrefixCls}>
|
|
|
|
<ConfirmDialog {...p} prefixCls={prefixCls}></ConfirmDialog>
|
|
|
|
</ConfigProvider>
|
|
|
|
);
|
2021-09-08 17:12:11 +08:00
|
|
|
};
|
2020-10-19 23:09:36 +08:00
|
|
|
function render(props: ModalFuncProps) {
|
2021-09-25 16:51:32 +08:00
|
|
|
const vm = createVNode(Wrapper, { ...props });
|
2021-09-08 17:12:11 +08:00
|
|
|
vm.appContext = config.parentContext || config.appContext || vm.appContext;
|
|
|
|
vueRender(vm, div);
|
|
|
|
return vm;
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
confirmDialogInstance = render(currentConfig);
|
2019-04-17 10:21:28 +08:00
|
|
|
destroyFns.push(close);
|
2018-03-06 19:14:41 +08:00
|
|
|
return {
|
|
|
|
destroy: close,
|
2018-12-09 17:34:27 +08:00
|
|
|
update,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-09-08 17:12:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default confirm;
|