-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathnormalize-scoped-slots.js
44 lines (40 loc) · 1 KB
/
normalize-scoped-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
44
/* @flow */
import { normalizeChildren } from 'core/vdom/helpers/normalize-children'
export function normalizeScopedSlots (
slots: { [key: string]: Function } | void,
normalSlots: { [key: string]: Array<VNode> }
): any {
let res
if (!slots) {
res = {}
} else if (slots._normalized) {
return slots
} else {
res = {}
for (const key in slots) {
if (slots[key] && key[0] !== '$') {
res[key] = normalizeScopedSlot(slots[key])
}
}
}
// expose normal slots on scopedSlots
for (const key in normalSlots) {
if (!(key in res)) {
res[key] = proxyNormalSlot(normalSlots, key)
}
}
res._normalized = true
res.$stable = slots && slots.$stable
return res
}
function normalizeScopedSlot(fn: Function): Function {
return scope => {
const res = fn(scope)
return res && typeof res === 'object' && !Array.isArray(res)
? [res] // single vnode
: normalizeChildren(res)
}
}
function proxyNormalSlot(slots, key) {
return () => slots[key]
}