-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathcreateSlots.ts
More file actions
58 lines (53 loc) · 1.46 KB
/
createSlots.ts
File metadata and controls
58 lines (53 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { isArray } from '@vue/shared'
import type { VNode } from '../vnode'
// #6651 res can be undefined in SSR in string push mode
type SSRSlot = (...args: any[]) => VNode[] | undefined
interface CompiledSlotDescriptor {
name: string
fn: SSRSlot
key?: string
}
/**
* Compiler runtime helper for creating dynamic slots object
* @private
*/
export function createSlots(
slots: Record<string, SSRSlot>,
dynamicSlots: (
| CompiledSlotDescriptor
| CompiledSlotDescriptor[]
| undefined
)[],
order?: string[],
): Record<string, SSRSlot> {
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
if (res) (res as any).key = slot.key
return res
}
: slot.fn
}
}
if (order) {
order.forEach(slotName => {
if (slotName in slots) {
const reorderedSlot = slots[slotName]
delete slots[slotName]
slots[slotName] = reorderedSlot
}
})
}
return slots
}