You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/modal/confirm.tsx

74 lines
2.1 KiB

import { createVNode, render as vueRender } from 'vue';
import ConfirmDialog from './ConfirmDialog';
import type { ModalFuncProps } from './Modal';
import { destroyFns } from './Modal';
import Omit from 'omit.js';
const confirm = (config: ModalFuncProps) => {
const div = document.createElement('div');
document.body.appendChild(div);
let currentConfig = {
...Omit(config, ['parentContext', 'appContext']),
close,
visible: true,
} as any;
let confirmDialogInstance = null;
function close(this: typeof close, ...args: any[]) {
currentConfig = {
...currentConfig,
visible: false,
afterClose: destroy.bind(this, ...args),
};
update(currentConfig);
7 years ago
}
function update(newConfig: ModalFuncProps) {
currentConfig = {
...currentConfig,
...newConfig,
};
if (confirmDialogInstance) {
Object.assign(confirmDialogInstance.component.props, currentConfig);
confirmDialogInstance.component.update();
}
}
function destroy(...args: any[]) {
7 years ago
if (confirmDialogInstance && div.parentNode) {
Object.assign(confirmDialogInstance.component.props, { vIf: false }); // hack destroy
confirmDialogInstance.component.update();
confirmDialogInstance = null;
div.parentNode.removeChild(div);
7 years ago
}
const triggerCancel = args.some(param => param && param.triggerCancel);
7 years ago
if (config.onCancel && triggerCancel) {
config.onCancel(...args);
7 years ago
}
for (let i = 0; i < destroyFns.length; i++) {
const fn = destroyFns[i];
if (fn === close) {
destroyFns.splice(i, 1);
break;
}
}
7 years ago
}
const Wrapper = p => {
return p.vIf ? <ConfirmDialog {...p}></ConfirmDialog> : null;
};
function render(props: ModalFuncProps) {
const vm = createVNode(Wrapper, { ...props, vIf: true });
vm.appContext = config.parentContext || config.appContext || vm.appContext;
vueRender(vm, div);
return vm;
7 years ago
}
confirmDialogInstance = render(currentConfig);
destroyFns.push(close);
7 years ago
return {
destroy: close,
update,
};
};
export default confirm;