vue3-core/packages/runtime-core/src/helpers/createSlots.ts

45 lines
1.2 KiB
TypeScript

import { Slot } from '../componentSlots'
import { isArray } from '@vue/shared'
interface CompiledSlotDescriptor {
name: string
fn: Slot
key?: string
}
/**
* Compiler runtime helper for creating dynamic slots object
* @private
*/
export function createSlots(
slots: Record<string, Slot>,
dynamicSlots: (
| CompiledSlotDescriptor
| CompiledSlotDescriptor[]
| undefined
)[]
): Record<string, Slot> {
for (let i = 0; i < dynamicSlots.length; i++) {
const slot = dynamicSlots[i]
// array of dynamic slot generated by <template v-for="..." #[...]>
if (isArray(slot)) {
for (let j = 0; j < slot.length; j++) {
slots[slot[j].name] = slot[j].fn
}
} else if (slot) {
// conditional single slot generated by <template v-if="..." #foo>
slots[slot.name] = slot.key
? (...args: any[]) => {
const res = slot.fn(...args)
// attach branch key so each conditional branch is considered a
// different fragment
// #6651 res can be undefined in SSR in string push mode
if (res) (res as any).key = slot.key
return res
}
: slot.fn
}
}
return slots
}