ant-design-vue/components/modal/confirm.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-06-15 15:47:49 +00:00
import { createApp } from 'vue';
2019-01-12 03:33:27 +00:00
import ConfirmDialog from './ConfirmDialog';
2019-04-17 02:21:28 +00:00
import { destroyFns } from './Modal';
2020-06-15 15:47:49 +00:00
import Omit from 'omit.js';
2018-05-04 08:02:31 +00:00
2019-01-12 03:33:27 +00:00
export default function confirm(config) {
const div = document.createElement('div');
const el = document.createElement('div');
div.appendChild(el);
document.body.appendChild(div);
let currentConfig = { ...Omit(config, ['parentContext']), close, visible: true };
2018-03-07 13:36:15 +00:00
2019-01-12 03:33:27 +00:00
let confirmDialogInstance = null;
2020-06-15 15:47:49 +00:00
let confirmDialogProps = {};
2019-01-12 03:33:27 +00:00
function close(...args) {
destroy(...args);
2018-03-06 11:14:41 +00:00
}
2019-01-12 03:33:27 +00:00
function update(newConfig) {
currentConfig = {
...currentConfig,
...newConfig,
2019-01-12 03:33:27 +00:00
};
2020-06-15 15:47:49 +00:00
Object.assign(confirmDialogInstance, currentConfig);
}
2019-01-12 03:33:27 +00:00
function destroy(...args) {
2018-03-06 11:14:41 +00:00
if (confirmDialogInstance && div.parentNode) {
2020-06-15 15:47:49 +00:00
confirmDialogInstance.unmount(div);
2019-01-12 03:33:27 +00:00
confirmDialogInstance = null;
div.parentNode.removeChild(div);
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) {
2019-01-12 03:33:27 +00:00
config.onCancel(...args);
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
}
2019-01-12 03:33:27 +00:00
function render(props) {
2020-06-15 15:47:49 +00:00
confirmDialogProps = props;
return createApp({
parent: config.parentContext,
2019-01-12 03:33:27 +00:00
data() {
return { confirmDialogProps };
},
render() {
// 先解构,避免报错,原因不详
2019-01-12 03:33:27 +00:00
const cdProps = { ...this.confirmDialogProps };
return <ConfirmDialog {...cdProps} />;
2018-03-06 11:14:41 +00:00
},
2020-06-15 15:47:49 +00:00
}).mount(el);
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,
update,
2019-01-12 03:33:27 +00:00
};
2018-03-06 11:14:41 +00:00
}