forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-slots.js
43 lines (37 loc) · 983 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
40
41
42
43
// @flow
import { compileToFunctions } from 'vue-template-compiler'
function startsWithTag (str) {
return str && 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.reduce((accInner, slotDef) => {
return accInner.concat(createVNodesForSlot(h, slotDef, key))
}, [])
return acc.concat(nodes)
} else {
return acc.concat(createVNodesForSlot(h, content, key))
}
}, [])
}