forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-render-slot.js
62 lines (58 loc) · 1.74 KB
/
create-render-slot.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
59
60
61
62
// @flow
import Vue from 'vue'
import { compileToFunctions } from 'vue-template-compiler'
const _renderSlot = Vue.prototype._t
function createVNodes (
vm: Component,
slotValue: Component | string
): ?Array<VNode> {
if (typeof slotValue === 'string') {
const compiledResult = compileToFunctions(`<div>${slotValue}{{ }}</div>`)
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns
const vnodes = compiledResult.render.call(
vm._renderProxy, vm.$createElement
).children
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
return vnodes
}
return [vm.$createElement(slotValue)]
}
export default function createRenderSlot (
options: Object
): (
name: string,
fallback: ?Array<VNode>,
props: ?Object,
bindObject: ?Object
) => ?Array<VNode> {
return function renderSlot (
name: string,
fallback: ?Array<VNode>,
props: ?Object,
bindObject: ?Object
): ?Array<VNode> {
if (options.slots && options.slots[name]) {
this.$slots[name] = []
const slotsValue = options.slots[name]
if (Array.isArray(slotsValue)) {
slotsValue.forEach((value) => {
if (typeof value === 'string') {
const vnodes = createVNodes(this, value)
if (Array.isArray(vnodes)) {
this.$slots[name].push(...vnodes)
}
} else {
this.$slots[name].push(this.$createElement(value))
}
})
} else {
const vnodes = createVNodes(this, slotsValue)
if (Array.isArray(vnodes)) {
this.$slots[name] = vnodes
}
}
}
return _renderSlot.call(this, name, fallback, props, bindObject)
}
}