1
1
import type { ExtendedModalApi , ModalApiOptions , ModalProps } from './modal' ;
2
2
3
- import { defineComponent , h , inject , nextTick , provide , reactive } from 'vue' ;
3
+ import {
4
+ defineComponent ,
5
+ h ,
6
+ inject ,
7
+ nextTick ,
8
+ provide ,
9
+ reactive ,
10
+ ref ,
11
+ } from 'vue' ;
4
12
5
13
import { useStore } from '@vben-core/shared/store' ;
6
14
@@ -24,6 +32,7 @@ export function useVbenModal<TParentModalProps extends ModalProps = ModalProps>(
24
32
const { connectedComponent } = options ;
25
33
if ( connectedComponent ) {
26
34
const extendedApi = reactive ( { } ) ;
35
+ const isModalReady = ref ( true ) ;
27
36
const Modal = defineComponent (
28
37
( props : TParentModalProps , { attrs, slots } ) => {
29
38
provide ( USER_MODAL_INJECT_KEY , {
@@ -33,6 +42,11 @@ export function useVbenModal<TParentModalProps extends ModalProps = ModalProps>(
33
42
Object . setPrototypeOf ( extendedApi , api ) ;
34
43
} ,
35
44
options,
45
+ async reCreateModal ( ) {
46
+ isModalReady . value = false ;
47
+ await nextTick ( ) ;
48
+ isModalReady . value = true ;
49
+ } ,
36
50
} ) ;
37
51
checkProps ( extendedApi as ExtendedModalApi , {
38
52
...props ,
@@ -41,7 +55,7 @@ export function useVbenModal<TParentModalProps extends ModalProps = ModalProps>(
41
55
} ) ;
42
56
return ( ) =>
43
57
h (
44
- connectedComponent ,
58
+ isModalReady . value ? connectedComponent : 'div' ,
45
59
{
46
60
...props ,
47
61
...attrs ,
@@ -70,6 +84,13 @@ export function useVbenModal<TParentModalProps extends ModalProps = ModalProps>(
70
84
injectData . options ?. onOpenChange ?.( isOpen ) ;
71
85
} ;
72
86
87
+ mergedOptions . onClosed = ( ) => {
88
+ options . onClosed ?.( ) ;
89
+ if ( options . destroyOnClose ) {
90
+ injectData . reCreateModal ?.( ) ;
91
+ }
92
+ } ;
93
+
73
94
const api = new ModalApi ( mergedOptions ) ;
74
95
75
96
const extendedApi : ExtendedModalApi = api as never ;
0 commit comments