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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
| import type { ModalApiOptions, ModalState } from './modal';
|
| import { Store } from '@vben-core/shared/store';
| import { bindMethods, isFunction } from '@vben-core/shared/utils';
|
| export class ModalApi {
| // 共享数据
| public sharedData: Record<'payload', any> = {
| payload: {},
| };
| public store: Store<ModalState>;
|
| private api: Pick<
| ModalApiOptions,
| | 'onBeforeClose'
| | 'onCancel'
| | 'onClosed'
| | 'onConfirm'
| | 'onOpenChange'
| | 'onOpened'
| >;
|
| // private prevState!: ModalState;
| private state!: ModalState;
|
| constructor(options: ModalApiOptions = {}) {
| const {
| connectedComponent: _,
| onBeforeClose,
| onCancel,
| onClosed,
| onConfirm,
| onOpenChange,
| onOpened,
| ...storeState
| } = options;
|
| const defaultState: ModalState = {
| bordered: true,
| centered: false,
| class: '',
| closeOnClickModal: true,
| closeOnPressEscape: true,
| confirmDisabled: false,
| confirmLoading: false,
| contentClass: '',
| draggable: false,
| footer: true,
| footerClass: '',
| fullscreen: false,
| fullscreenButton: true,
| header: true,
| headerClass: '',
| isOpen: false,
| loading: false,
| modal: true,
| openAutoFocus: false,
| showCancelButton: true,
| showConfirmButton: true,
| title: '',
| };
|
| this.store = new Store<ModalState>(
| {
| ...defaultState,
| ...storeState,
| },
| {
| onUpdate: () => {
| const state = this.store.state;
|
| // 每次更新状态时,都会调用 onOpenChange 回调函数
| if (state?.isOpen === this.state?.isOpen) {
| this.state = state;
| } else {
| this.state = state;
| this.api.onOpenChange?.(!!state?.isOpen);
| }
| },
| },
| );
|
| this.state = this.store.state;
|
| this.api = {
| onBeforeClose,
| onCancel,
| onClosed,
| onConfirm,
| onOpenChange,
| onOpened,
| };
| bindMethods(this);
| }
|
| /**
| * 关闭弹窗
| * @description 关闭弹窗时会调用 onBeforeClose 钩子函数,如果 onBeforeClose 返回 false,则不关闭弹窗
| */
| async close() {
| // 通过 onBeforeClose 钩子函数来判断是否允许关闭弹窗
| // 如果 onBeforeClose 返回 false,则不关闭弹窗
| const allowClose = (await this.api.onBeforeClose?.()) ?? true;
| if (allowClose) {
| this.store.setState((prev) => ({
| ...prev,
| isOpen: false,
| submitting: false,
| }));
| }
| }
|
| getData<T extends object = Record<string, any>>() {
| return (this.sharedData?.payload ?? {}) as T;
| }
|
| /**
| * 锁定弹窗状态(用于提交过程中的等待状态)
| * @description 锁定状态将禁用默认的取消按钮,使用spinner覆盖弹窗内容,隐藏关闭按钮,阻止手动关闭弹窗,将默认的提交按钮标记为loading状态
| * @param isLocked 是否锁定
| */
| lock(isLocked = true) {
| return this.setState({ submitting: isLocked });
| }
|
| modalLoading(loading: boolean) {
| this.store.setState((prev) => ({
| ...prev,
| confirmLoading: loading,
| loading,
| }));
| }
|
| /**
| * 取消操作
| */
| onCancel() {
| if (this.api.onCancel) {
| this.api.onCancel?.();
| } else {
| this.close();
| }
| }
|
| /**
| * 弹窗关闭动画播放完毕后的回调
| */
| onClosed() {
| if (!this.state.isOpen) {
| this.api.onClosed?.();
| }
| }
|
| /**
| * 确认操作
| */
| onConfirm() {
| this.api.onConfirm?.();
| }
|
| /**
| * 弹窗打开动画播放完毕后的回调
| */
| onOpened() {
| if (this.state.isOpen) {
| this.api.onOpened?.();
| }
| }
|
| open() {
| this.store.setState((prev) => ({ ...prev, isOpen: true }));
| }
|
| setData<T>(payload: T) {
| this.sharedData.payload = payload;
| return this;
| }
|
| setState(
| stateOrFn:
| | ((prev: ModalState) => Partial<ModalState>)
| | Partial<ModalState>,
| ) {
| if (isFunction(stateOrFn)) {
| this.store.setState(stateOrFn);
| } else {
| this.store.setState((prev) => ({ ...prev, ...stateOrFn }));
| }
| return this;
| }
|
| /**
| * 解除弹窗的锁定状态
| * @description 解除由lock方法设置的锁定状态,是lock(false)的别名
| */
| unlock() {
| return this.lock(false);
| }
| }
|
|