Skip to content

Commit 8f3881e

Browse files
authored
perf: beforeClose of drawer support promise (vbenjs#5932)
* perf: the beforeClose function of drawer is consistent with that of modal * refactor: drawer test update
1 parent 5252480 commit 8f3881e

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

packages/@core/ui-kit/popup-ui/src/drawer/__tests__/drawer-api.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ describe('drawerApi', () => {
5454
});
5555

5656
it('should close the drawer if onBeforeClose allows it', () => {
57-
drawerApi.open();
5857
drawerApi.close();
5958
expect(drawerApi.store.state.isOpen).toBe(false);
6059
});

packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ export class DrawerApi {
8686
}
8787

8888
/**
89-
* 关闭弹窗
89+
* 关闭抽屉
90+
* @description 关闭抽屉时会调用 onBeforeClose 钩子函数,如果 onBeforeClose 返回 false,则不关闭弹窗
9091
*/
91-
close() {
92+
async close() {
9293
// 通过 onBeforeClose 钩子函数来判断是否允许关闭弹窗
9394
// 如果 onBeforeClose 返回 false,则不关闭弹窗
94-
const allowClose = this.api.onBeforeClose?.() ?? true;
95+
const allowClose = (await this.api.onBeforeClose?.()) ?? true;
9596
if (allowClose) {
9697
this.store.setState((prev) => ({
9798
...prev,

packages/@core/ui-kit/popup-ui/src/drawer/drawer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Component, Ref } from 'vue';
22

3-
import type { ClassType } from '@vben-core/typings';
3+
import type { ClassType, MaybePromise } from '@vben-core/typings';
44

55
import type { DrawerApi } from './drawer-api';
66

@@ -151,7 +151,7 @@ export interface DrawerApiOptions extends DrawerState {
151151
* 关闭前的回调,返回 false 可以阻止关闭
152152
* @returns
153153
*/
154-
onBeforeClose?: () => void;
154+
onBeforeClose?: () => MaybePromise<boolean | undefined>;
155155
/**
156156
* 点击取消按钮的回调
157157
*/

0 commit comments

Comments
 (0)