forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-slot-vnodes.js
58 lines (53 loc) · 1.48 KB
/
create-slot-vnodes.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @flow
import { compileToFunctions } from 'vue-template-compiler'
function createVNodes (
vm: Component,
slotValue: string
): Array<VNode> {
const el = compileToFunctions(`<div>${slotValue}</div>`)
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
const _staticTrees = vm._renderProxy._staticTrees
vm._renderProxy._staticTrees = []
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns
const vnode = el.render.call(vm._renderProxy, vm.$createElement)
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
vm._renderProxy._staticTrees = _staticTrees
return vnode.children
}
function createVNodesForSlot (
vm: Component,
slotValue: SlotValue,
name: string,
): VNode | Array<VNode> {
let vnode
if (typeof slotValue === 'string') {
const vnodes = createVNodes(vm, slotValue)
if (vnodes.length > 1) {
return vnodes
}
vnode = vnodes[0]
} else {
vnode = vm.$createElement(slotValue)
}
if (vnode.data) {
vnode.data.slot = name
} else {
vnode.data = { slot: name }
}
return vnode
}
export function createSlotVNodes (
vm: Component,
slots: SlotsObject
): Array<VNode | Array<VNode>> {
return Object.keys(slots).reduce((acc, key) => {
const content = slots[key]
if (Array.isArray(content)) {
const nodes = content.map(
slotDef => createVNodesForSlot(vm, slotDef, key)
)
return acc.concat(nodes)
}
return acc.concat(createVNodesForSlot(vm, content, key))
}, [])
}