Browse Source
perf: `beforeClose` of drawer support promise (#5932)
* perf: the beforeClose function of drawer is consistent with that of modal
* refactor: drawer test update
pull/5977/head
LinaBell
1 year ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with
6 additions and
6 deletions
-
packages/@core/ui-kit/popup-ui/src/drawer/__tests__/drawer-api.test.ts
-
packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts
-
packages/@core/ui-kit/popup-ui/src/drawer/drawer.ts
|
|
|
@ -54,7 +54,6 @@ describe('drawerApi', () => { |
|
|
|
}); |
|
|
|
|
|
|
|
it('should close the drawer if onBeforeClose allows it', () => { |
|
|
|
drawerApi.open(); |
|
|
|
drawerApi.close(); |
|
|
|
expect(drawerApi.store.state.isOpen).toBe(false); |
|
|
|
}); |
|
|
|
|
|
|
|
@ -86,12 +86,13 @@ export class DrawerApi { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 关闭弹窗 |
|
|
|
* 关闭抽屉 |
|
|
|
* @description 关闭抽屉时会调用 onBeforeClose 钩子函数,如果 onBeforeClose 返回 false,则不关闭弹窗 |
|
|
|
*/ |
|
|
|
close() { |
|
|
|
async close() { |
|
|
|
// 通过 onBeforeClose 钩子函数来判断是否允许关闭弹窗
|
|
|
|
// 如果 onBeforeClose 返回 false,则不关闭弹窗
|
|
|
|
const allowClose = this.api.onBeforeClose?.() ?? true; |
|
|
|
const allowClose = (await this.api.onBeforeClose?.()) ?? true; |
|
|
|
if (allowClose) { |
|
|
|
this.store.setState((prev) => ({ |
|
|
|
...prev, |
|
|
|
|
|
|
|
@ -1,6 +1,6 @@ |
|
|
|
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'; |
|
|
|
|
|
|
|
@ -151,7 +151,7 @@ export interface DrawerApiOptions extends DrawerState { |
|
|
|
* 关闭前的回调,返回 false 可以阻止关闭 |
|
|
|
* @returns |
|
|
|
*/ |
|
|
|
onBeforeClose?: () => void; |
|
|
|
onBeforeClose?: () => MaybePromise<boolean | undefined>; |
|
|
|
/** |
|
|
|
* 点击取消按钮的回调 |
|
|
|
*/ |
|
|
|
|