1
1
import { isString , tryOnUnmounted } from '@vueuse/core'
2
2
import { computed , getCurrentInstance , inject , markRaw , reactive , useAttrs } from 'vue'
3
- import type { Component , Raw } from 'vue'
3
+ import type { Component } from 'vue'
4
4
import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
5
5
import type CoreModal from './components/CoreModal/CoreModal.vue'
6
6
import { internalVfmSymbol , vfmSymbol } from './injectionSymbols'
7
7
8
- import type { ComponentProps , IOverloadedUseModalFn , InternalVfm , ModalSlot , ModalSlotOptions , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
8
+ import type { ComponentProps , Constructor , InternalVfm , ModalSlot , ModalSlotOptions , RawProps , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
9
9
10
10
/**
11
11
* Returns the vfm instance. Equivalent to using `$vfm` inside
@@ -22,7 +22,7 @@ export function useInternalVfm(): InternalVfm {
22
22
return inject ( internalVfmSymbol ) !
23
23
}
24
24
25
- function withMarkRaw ( options : Partial < UseModalOptions > , DefaultComponent : Component = VueFinalModal ) {
25
+ function withMarkRaw < P > ( options : Partial < UseModalOptions < P > > , DefaultComponent : Component = VueFinalModal ) {
26
26
const { component, slots : innerSlots , ...rest } = options
27
27
28
28
const slots = typeof innerSlots === 'undefined'
@@ -43,23 +43,23 @@ function withMarkRaw(options: Partial<UseModalOptions>, DefaultComponent: Compon
43
43
44
44
return {
45
45
...rest ,
46
- component : markRaw ( component || DefaultComponent ) ,
46
+ component : markRaw ( component || DefaultComponent ) as Constructor < P > ,
47
47
slots,
48
48
}
49
49
}
50
50
51
51
/**
52
52
* Create a dynamic modal.
53
53
*/
54
- export const useModal : IOverloadedUseModalFn = function ( _options : UseModalOptions ) : UseModalReturnType {
54
+ export function useModal < P = InstanceType < typeof VueFinalModal > [ '$props' ] > ( _options : UseModalOptions < P > ) : UseModalReturnType < P > {
55
55
const options = reactive ( {
56
56
id : Symbol ( 'useModal' ) ,
57
57
modelValue : ! ! _options ?. defaultModelValue ,
58
- resolveOpened : ( ) => { } ,
59
- resolveClosed : ( ) => { } ,
58
+ resolveOpened : ( ) => { } ,
59
+ resolveClosed : ( ) => { } ,
60
60
attrs : { } ,
61
- ...withMarkRaw ( _options ) ,
62
- } ) as UseModalOptions & UseModalOptionsPrivate
61
+ ...withMarkRaw < P > ( _options ) ,
62
+ } ) as UseModalOptions < P > & UseModalOptionsPrivate
63
63
64
64
if ( ! options . context ) {
65
65
const currentInstance = getCurrentInstance ( )
@@ -89,7 +89,7 @@ export const useModal: IOverloadedUseModalFn = function (_options: UseModalOptio
89
89
} )
90
90
}
91
91
92
- function patchOptions ( _options : Partial < UseModalOptions > ) {
92
+ function patchOptions ( _options : Partial < Omit < UseModalOptions < P > , 'defaultModelValue' | 'context' > > ) {
93
93
const { slots, ...rest } = withMarkRaw ( _options , options . component )
94
94
95
95
// patch options.component and options.attrs
@@ -132,6 +132,13 @@ export const useModal: IOverloadedUseModalFn = function (_options: UseModalOptio
132
132
return modal
133
133
}
134
134
135
+ export function useModalSlot < P > ( options : {
136
+ component : Constructor < P >
137
+ attrs ?: ( RawProps & P ) | ( { } extends P ? null : never )
138
+ } ) {
139
+ return options
140
+ }
141
+
135
142
function patchAttrs < T extends Record < string , any > > ( attrs : T , newAttrs : Partial < T > ) : T {
136
143
Object . entries ( newAttrs ) . forEach ( ( [ key , value ] ) => {
137
144
attrs [ key as keyof T ] = value
@@ -140,12 +147,10 @@ function patchAttrs<T extends Record<string, any>>(attrs: T, newAttrs: Partial<T
140
147
return attrs
141
148
}
142
149
143
- type ComponentOptions = {
144
- component ?: Raw < Component >
145
- attrs ?: Record < string , any >
146
- }
147
-
148
- function patchComponentOptions ( options : ComponentOptions | ModalSlotOptions , newOptions : ComponentOptions | ModalSlotOptions ) {
150
+ function patchComponentOptions < P > (
151
+ options : Omit < UseModalOptions < P > , 'defaultModelValue' | 'context' > | ModalSlotOptions ,
152
+ newOptions : Partial < Omit < UseModalOptions < P > , 'defaultModelValue' | 'context' > > | ModalSlotOptions ,
153
+ ) {
149
154
if ( newOptions . component )
150
155
options . component = newOptions . component
151
156
0 commit comments