feat: drawer support destroy on close

This commit is contained in:
Netfan 2024-12-30 21:39:00 +08:00
parent e86a7906fe
commit de17007788
3 changed files with 38 additions and 5 deletions

View File

@ -126,9 +126,13 @@ export type ExtendedDrawerApi = {
export interface DrawerApiOptions extends DrawerState {
/**
*
*
*/
connectedComponent?: Component;
/**
* 使 connectedComponent
*/
destroyOnClose?: boolean;
/**
* false
* @returns

View File

@ -4,7 +4,15 @@ import type {
ExtendedDrawerApi,
} from './drawer';
import { defineComponent, h, inject, nextTick, provide, reactive } from 'vue';
import {
defineComponent,
h,
inject,
nextTick,
provide,
reactive,
ref,
} from 'vue';
import { useStore } from '@vben-core/shared/store';
@ -22,6 +30,7 @@ export function useVbenDrawer<
const { connectedComponent } = options;
if (connectedComponent) {
const extendedApi = reactive({});
const isDrawerReady = ref(true);
const Drawer = defineComponent(
(props: TParentDrawerProps, { attrs, slots }) => {
provide(USER_DRAWER_INJECT_KEY, {
@ -31,13 +40,23 @@ export function useVbenDrawer<
Object.setPrototypeOf(extendedApi, api);
},
options,
async reCreateDrawer() {
isDrawerReady.value = false;
await nextTick();
isDrawerReady.value = true;
},
});
checkProps(extendedApi as ExtendedDrawerApi, {
...props,
...attrs,
...slots,
});
return () => h(connectedComponent, { ...props, ...attrs }, slots);
return () =>
h(
isDrawerReady.value ? connectedComponent : 'div',
{ ...props, ...attrs },
slots,
);
},
{
inheritAttrs: false,
@ -58,6 +77,14 @@ export function useVbenDrawer<
options.onOpenChange?.(isOpen);
injectData.options?.onOpenChange?.(isOpen);
};
const onClosed = mergedOptions.onClosed;
mergedOptions.onClosed = () => {
onClosed?.();
if (mergedOptions.destroyOnClose) {
injectData.reCreateDrawer?.();
}
};
const api = new DrawerApi(mergedOptions);
const extendedApi: ExtendedDrawerApi = api as never;

View File

@ -77,9 +77,11 @@ export function useVbenModal<TParentModalProps extends ModalProps = ModalProps>(
injectData.options?.onOpenChange?.(isOpen);
};
const onClosed = mergedOptions.onClosed;
mergedOptions.onClosed = () => {
options.onClosed?.();
if (options.destroyOnClose) {
onClosed?.();
if (mergedOptions.destroyOnClose) {
injectData.reCreateModal?.();
}
};