ModalService.ts
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {Modal} from "ant-design-vue/es";
import _extends from "@babel/runtime/helpers/esm/extends";
import _defaults from "@babel/runtime/helpers/esm/defaults";
import {ModalFuncProps} from 'ant-design-vue/lib/modal/Modal';
import {createVNode} from "vue";
import {VNodeProps, VNodeTypes} from '@vue/runtime-core';
import {Data} from 'ant-design-vue/lib/_util/type';
import type {ButtonProps as ButtonPropsType} from "ant-design-vue/lib/button/buttonTypes";
export default interface ModalProps extends ModalFuncProps {
component: VNodeTypes;
props?: (Data & VNodeProps) | null;
emits?: { [event: string]: (...args: any[]) => void }
}
const DEFAULT_MODAL_PROPS: ModalFuncProps = {
open: false,
width: 1024,
okText: '确定',
cancelText: '取消',
closable: true,
maskClosable: false,
mask: true,
keyboard: true,
icon: null,
class: 'dynamic-modal'
}
export class ModalService {
/**
* 适用于表单,有确认和关闭按钮
* @param modalProps
*/
public static confirm(modalProps: ModalProps) {
modalProps = _defaults(modalProps, DEFAULT_MODAL_PROPS);
const vNode = modalProps.content = createVNode(modalProps.component, modalProps.props);
if (!modalProps.onOk) {
// @ts-ignore
modalProps.okButtonProps = {style: {display: 'none'}};
}
const configs = _extends(_extends({}, modalProps), {
onOk: (...args: any[]) => {
if (modalProps.onOk) {
modalProps.onOk(vNode.component.proxy, args);
}
// 禁止确定时关闭弹窗
return new Promise((resolve, reject) => reject());
},
onCancel: (...args: any[]) => {
if (modalProps.onCancel) {
return modalProps.onCancel(vNode.component.proxy);
}
}
});
const modal = Modal.confirm(configs);
if (modalProps.emits) {
vNode.component.emit = ((event, args) => {
if (modalProps.emits[event]) {
modalProps.emits[event](args);
}
});
}
return modal;
}
/**
* 适用于信息弹框,单个按钮
* @param modalProps
*/
public static info(modalProps: ModalProps) {
modalProps = _defaults(modalProps, DEFAULT_MODAL_PROPS);
const vNode = modalProps.content = createVNode(modalProps.component, modalProps.props);
const configs = _extends(_extends({}, modalProps), {
onOk: (...args: any[]) => {
if (modalProps.onOk) {
modalProps.onOk(vNode.component.proxy, args);
}
},
onCancel: (...args: any[]) => {
if (modalProps.onCancel) {
return modalProps.onCancel(vNode.component.proxy);
}
}
});
const modal = Modal.info(configs);
if (modalProps.emits) {
vNode.component.emit = ((event, args) => {
if (modalProps.emits[event]) {
modalProps.emits[event](args);
}
});
}
return modal;
}
}