@@ -2,9 +2,9 @@ import type { Component, VNode } from 'vue'
22import { computed , h , markRaw , nextTick , reactive , useAttrs } from 'vue'
33import { tryOnUnmounted } from '@vueuse/core'
44import VueFinalModal from './components/VueFinalModal/VueFinalModal.vue'
5- import type { ModalSlotOptions , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
5+ import type { C2VOptions , UseModalOptions , UseModalOptionsPrivate , UseModalReturnType , Vfm } from './Modal'
66import { activeVfm , getActiveVfm } from './plugin'
7- import type { ComponentEmit , ComponentProps } from './Component'
7+ import type { ComponentEmit , ComponentProps , ComponentSlots } from './Component'
88import { DynamicModal } from './components/DynamicModal'
99import { isString , objectEntries } from '~/utils'
1010
@@ -35,7 +35,7 @@ function withMarkRaw<T extends Component>(options: Partial<UseModalOptions<T>>,
3535 if ( isString ( maybeComponent ) )
3636 return [ name , maybeComponent ] as const
3737
38- if ( isModalSlotOptions ( maybeComponent ) ) {
38+ if ( isC2VOptions ( maybeComponent ) ) {
3939 return [ name , {
4040 ...maybeComponent ,
4141 component : markRaw ( maybeComponent . component ) ,
@@ -125,7 +125,7 @@ export function useModal<T extends Component = typeof VueFinalModal>(_options: U
125125 const originSlot = options . slots ! [ name ]
126126 if ( isString ( originSlot ) )
127127 options . slots ! [ name ] = slot
128- else if ( isModalSlotOptions ( originSlot ) && isModalSlotOptions ( slot ) )
128+ else if ( isC2VOptions ( originSlot ) && isC2VOptions ( slot ) )
129129 patchComponentOptions ( originSlot , slot )
130130 else
131131 options . slots ! [ name ] = slot
@@ -143,8 +143,8 @@ export function useModal<T extends Component = typeof VueFinalModal>(_options: U
143143}
144144
145145function patchComponentOptions < T extends Component > (
146- options : UseModalOptions < T > | ModalSlotOptions ,
147- newOptions : Partial < UseModalOptions < T > > | ModalSlotOptions ,
146+ options : UseModalOptions < T > | C2VOptions < Component > ,
147+ newOptions : Partial < UseModalOptions < T > > | C2VOptions < Component > ,
148148) {
149149 if ( newOptions . component )
150150 options . component = newOptions . component
@@ -182,20 +182,34 @@ function removeVNode(vNode: VNode): void {
182182 vfm . h . remove ( vNode )
183183}
184184
185- export function useModalSlot < T extends Component > ( options : {
186- component : T
187- attrs ?: ComponentProps < T >
188- } ) {
189- return options
190- }
191-
192- export function isModalSlotOptions ( value : unknown ) : value is ModalSlotOptions {
185+ export function isC2VOptions < T extends Component > ( value : unknown ) : value is C2VOptions < T > {
193186 if ( typeof value === 'object' && value !== null )
194187 return 'component' in value
195188 else
196189 return false
197190}
198191
192+ /** Convert Component Options to VNode */
193+ export function c2v < T extends Component > ( options : C2VOptions < T > ) {
194+ return options
195+ }
196+
197+ export function getSlots < T extends Component > ( slots : {
198+ [ K in keyof ComponentSlots < T > ] ?: string | Component | C2VOptions < Component >
199+ } ) {
200+ return objectEntries ( slots || { } ) . reduce ( ( acc , cur ) => {
201+ const slotName = cur [ 0 ] as string
202+ const slot = cur [ 1 ] as string | Component | C2VOptions < any >
203+ if ( isString ( slot ) )
204+ acc [ slotName ] = ( ) => h ( 'div' , { innerHTML : slot } )
205+ else if ( isC2VOptions ( slot ) )
206+ acc [ slotName ] = ( ) => h ( slot . component , slot . attrs , slot . slots ? getSlots ( slot . slots ) : undefined )
207+ else
208+ acc [ slotName ] = ( ) => h ( slot )
209+ return acc
210+ } , { } as Record < string , ( ) => VNode > )
211+ }
212+
199213export function pickModalProps ( props : Record < string , any > , modalProps : Record < string , any > ) {
200214 return Object . keys ( modalProps ) . reduce < Record < string , any > > ( ( acc , propName ) => {
201215 acc [ propName ] = props ?. [ propName ]
0 commit comments