perf: beforeClose of drawer support promise (#5932)

* perf: the beforeClose function of drawer is consistent with that of modal

* refactor: drawer test update
This commit is contained in:
LinaBell
2025-04-16 11:27:13 +08:00
committed by Netfan
parent 7e99c62abf
commit fe2310300c
3 changed files with 6 additions and 6 deletions

View File

@@ -54,7 +54,6 @@ describe('drawerApi', () => {
}); });
it('should close the drawer if onBeforeClose allows it', () => { it('should close the drawer if onBeforeClose allows it', () => {
drawerApi.open();
drawerApi.close(); drawerApi.close();
expect(drawerApi.store.state.isOpen).toBe(false); expect(drawerApi.store.state.isOpen).toBe(false);
}); });

View File

@@ -86,12 +86,13 @@ export class DrawerApi {
} }
/** /**
* 关闭弹窗 * 关闭抽屉
* @description 关闭抽屉时会调用 onBeforeClose 钩子函数,如果 onBeforeClose 返回 false则不关闭弹窗
*/ */
close() { async close() {
// 通过 onBeforeClose 钩子函数来判断是否允许关闭弹窗 // 通过 onBeforeClose 钩子函数来判断是否允许关闭弹窗
// 如果 onBeforeClose 返回 false则不关闭弹窗 // 如果 onBeforeClose 返回 false则不关闭弹窗
const allowClose = this.api.onBeforeClose?.() ?? true; const allowClose = (await this.api.onBeforeClose?.()) ?? true;
if (allowClose) { if (allowClose) {
this.store.setState((prev) => ({ this.store.setState((prev) => ({
...prev, ...prev,

View File

@@ -1,6 +1,6 @@
import type { Component, Ref } from 'vue'; import type { Component, Ref } from 'vue';
import type { ClassType } from '@vben-core/typings'; import type { ClassType, MaybePromise } from '@vben-core/typings';
import type { DrawerApi } from './drawer-api'; import type { DrawerApi } from './drawer-api';
@@ -151,7 +151,7 @@ export interface DrawerApiOptions extends DrawerState {
* 关闭前的回调,返回 false 可以阻止关闭 * 关闭前的回调,返回 false 可以阻止关闭
* @returns * @returns
*/ */
onBeforeClose?: () => void; onBeforeClose?: () => MaybePromise<boolean | undefined>;
/** /**
* 点击取消按钮的回调 * 点击取消按钮的回调
*/ */