1
+ import type { Component } from 'vue'
1
2
import { computed , markRaw , nextTick , reactive , useAttrs } from 'vue'
2
3
import { tryOnUnmounted } from '@vueuse/core'
3
4
import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
4
- import type { ComponentProps , ComponentType , ModalSlot , ModalSlotOptions , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
5
+ import type { ModalSlotOptions , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
5
6
import { activeVfm , getActiveVfm } from './plugin'
6
- import { isString } from '~/utils'
7
+ import type { ComponentProps } from './Component'
8
+ import { isString , objectEntries } from '~/utils'
7
9
8
10
/**
9
11
* Returns the vfm instance. Equivalent to using `$vfm` inside
@@ -23,24 +25,24 @@ export function useVfm(): Vfm {
23
25
return vfm !
24
26
}
25
27
26
- function withMarkRaw < T extends ComponentType > ( options : Partial < UseModalOptions < T > > , DefaultComponent : ComponentType = VueFinalModal ) {
28
+ function withMarkRaw < T extends Component > ( options : Partial < UseModalOptions < T > > , DefaultComponent : Component = VueFinalModal ) {
27
29
const { component, slots : innerSlots , ...rest } = options
28
30
29
- const slots = typeof innerSlots === 'undefined'
31
+ const slots : UseModalOptions < T > [ 'slots' ] = typeof innerSlots === 'undefined'
30
32
? { }
31
- : Object . fromEntries < ModalSlot > ( Object . entries ( innerSlots ) . map ( ( [ name , maybeComponent ] ) => {
33
+ : Object . fromEntries ( objectEntries ( innerSlots ) . map ( ( [ name , maybeComponent ] ) => {
32
34
if ( isString ( maybeComponent ) )
33
35
return [ name , maybeComponent ] as const
34
36
35
- if ( 'component' in maybeComponent ) {
37
+ if ( isModalSlotOptions ( maybeComponent ) ) {
36
38
return [ name , {
37
39
...maybeComponent ,
38
40
component : markRaw ( maybeComponent . component ) ,
39
41
} ]
40
42
}
41
43
42
- return [ name , markRaw ( maybeComponent ) ]
43
- } ) )
44
+ return [ name , markRaw ( maybeComponent as Component ) ]
45
+ } ) ) as UseModalOptions < T > [ 'slots' ]
44
46
45
47
return {
46
48
...rest ,
@@ -52,7 +54,7 @@ function withMarkRaw<T extends ComponentType>(options: Partial<UseModalOptions<T
52
54
/**
53
55
* Create a dynamic modal.
54
56
*/
55
- export function useModal < T extends ComponentType = typeof VueFinalModal > ( _options : UseModalOptions < T > ) : UseModalReturnType < T > {
57
+ export function useModal < T extends Component = typeof VueFinalModal > ( _options : UseModalOptions < T > ) : UseModalReturnType < T > {
56
58
const options = reactive ( {
57
59
id : Symbol ( __DEV__ ? 'useModal' : '' ) ,
58
60
modelValue : ! ! _options ?. defaultModelValue ,
@@ -124,7 +126,7 @@ export function useModal<T extends ComponentType = typeof VueFinalModal>(_option
124
126
125
127
// patch options.slots
126
128
if ( slots ) {
127
- Object . entries ( slots ) . forEach ( ( [ name , slot ] ) => {
129
+ objectEntries ( slots ) . forEach ( ( [ name , slot ] ) => {
128
130
const originSlot = options . slots ! [ name ]
129
131
if ( isString ( originSlot ) )
130
132
options . slots ! [ name ] = slot
@@ -136,7 +138,7 @@ export function useModal<T extends ComponentType = typeof VueFinalModal>(_option
136
138
}
137
139
}
138
140
139
- function patchComponentOptions < T extends ComponentType > (
141
+ function patchComponentOptions < T extends Component > (
140
142
options : UseModalOptions < T > | ModalSlotOptions ,
141
143
newOptions : Partial < UseModalOptions < T > > | ModalSlotOptions ,
142
144
) {
@@ -171,15 +173,18 @@ export function useModal<T extends ComponentType = typeof VueFinalModal>(_option
171
173
}
172
174
}
173
175
174
- export function useModalSlot < T extends ComponentType > ( options : {
176
+ export function useModalSlot < T extends Component > ( options : {
175
177
component : T
176
178
attrs ?: ComponentProps < T >
177
179
} ) {
178
180
return options
179
181
}
180
182
181
- function isModalSlotOptions ( value : any ) : value is ModalSlotOptions {
182
- return 'component' in value || 'attrs' in value
183
+ export function isModalSlotOptions ( value : unknown ) : value is ModalSlotOptions {
184
+ if ( typeof value === 'object' && value !== null )
185
+ return 'component' in value
186
+ else
187
+ return false
183
188
}
184
189
185
190
export function pickModalProps ( props : any , modalProps : any ) {
0 commit comments