ant-design-vue/components/modal/confirm.js
tangjinzhou da0f17d1b6 Merge remote-tracking branch 'origin/master' into feat-1.4.0
# Conflicts:
#	components/affix/index.jsx
#	components/alert/index.jsx
#	components/back-top/index.jsx
#	components/carousel/index.jsx
#	components/cascader/index.jsx
#	components/comment/index.jsx
#	components/config-provider/index.jsx
#	components/divider/index.jsx
#	components/drawer/index.jsx
#	components/form/Form.jsx
#	components/input-number/index.jsx
#	components/menu/index.jsx
#	components/modal/confirm.js
#	components/modal/index.js
#	components/popconfirm/index.jsx
#	components/popover/index.jsx
#	components/rate/index.jsx
#	components/steps/index.jsx
#	components/switch/index.jsx
#	components/time-picker/index.jsx
#	components/tree-select/index.jsx
#	site/components.js
2019-08-28 22:36:44 +08:00

67 lines
1.7 KiB
JavaScript

import Vue from 'vue';
import ConfirmDialog from './ConfirmDialog';
import { destroyFns } from './Modal';
import Base from '../base';
export default function confirm(config) {
const div = document.createElement('div');
const el = document.createElement('div');
div.appendChild(el);
document.body.appendChild(div);
let currentConfig = { ...config, close, visible: true };
let confirmDialogInstance = null;
const confirmDialogProps = { props: {} };
function close(...args) {
destroy(...args);
}
function update(newConfig) {
currentConfig = {
...currentConfig,
...newConfig,
};
confirmDialogProps.props = currentConfig;
}
function destroy(...args) {
if (confirmDialogInstance && div.parentNode) {
confirmDialogInstance.$destroy();
confirmDialogInstance = null;
div.parentNode.removeChild(div);
}
const triggerCancel = args.some(param => param && param.triggerCancel);
if (config.onCancel && triggerCancel) {
config.onCancel(...args);
}
for (let i = 0; i < destroyFns.length; i++) {
const fn = destroyFns[i];
if (fn === close) {
destroyFns.splice(i, 1);
break;
}
}
}
function render(props) {
confirmDialogProps.props = props;
const V = Base.Vue || Vue;
return new V({
el: el,
data() {
return { confirmDialogProps };
},
render() {
// 先解构,避免报错,原因不详
const cdProps = { ...this.confirmDialogProps };
return <ConfirmDialog {...cdProps} />;
},
});
}
confirmDialogInstance = render(currentConfig);
destroyFns.push(close);
return {
destroy: close,
update,
};
}