-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathadd-slots.js
39 lines (32 loc) · 942 Bytes
/
add-slots.js
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
// @flow
import { compileToFunctions } from 'vue-template-compiler'
function startsWithTag (str: SlotValue): boolean {
return typeof str === 'string' && str.trim()[0] === '<'
}
function createVNodesForSlot (
h: Function,
slotValue: SlotValue,
name: string
): VNode | string {
if (typeof slotValue === 'string' && !startsWithTag(slotValue)) {
return slotValue
}
const el =
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue
const vnode = h(el)
vnode.data.slot = name
return vnode
}
export function createSlotVNodes (
h: Function,
slots: SlotsObject
): Array<VNode | string> {
return Object.keys(slots).reduce((acc, key) => {
const content = slots[key]
if (Array.isArray(content)) {
const nodes = content.map(slotDef => createVNodesForSlot(h, slotDef, key))
return acc.concat(nodes)
}
return acc.concat(createVNodesForSlot(h, content, key))
}, [])
}