2019-08-31 03:26:16 +08:00
|
|
|
import { ComponentInstance, currentInstance } from './component'
|
2019-05-31 18:07:43 +08:00
|
|
|
import { VNode, NormalizedChildren, normalizeVNode, VNodeChild } from './vnode'
|
2019-06-03 19:58:12 +08:00
|
|
|
import { isArray, isFunction } from '@vue/shared'
|
2019-08-22 23:12:37 +08:00
|
|
|
import { ShapeFlags } from './shapeFlags'
|
2019-08-31 03:26:16 +08:00
|
|
|
import { warn } from './warning'
|
2019-05-31 18:07:43 +08:00
|
|
|
|
|
|
|
export type Slot = (...args: any[]) => VNode[]
|
|
|
|
export type Slots = Readonly<{
|
|
|
|
[name: string]: Slot
|
|
|
|
}>
|
|
|
|
export type RawSlots = {
|
|
|
|
[name: string]: unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
const normalizeSlotValue = (value: unknown): VNode[] =>
|
|
|
|
isArray(value)
|
|
|
|
? value.map(normalizeVNode)
|
|
|
|
: [normalizeVNode(value as VNodeChild)]
|
|
|
|
|
2019-08-31 03:26:16 +08:00
|
|
|
const normalizeSlot = (key: string, rawSlot: Function): Slot => (
|
|
|
|
props: any
|
|
|
|
) => {
|
|
|
|
if (__DEV__ && currentInstance != null) {
|
|
|
|
warn(
|
|
|
|
`Slot "${key}" invoked outside of the render function: ` +
|
|
|
|
`this will not track dependencies used in the slot. ` +
|
|
|
|
`Invoke the slot function inside the render function instead.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return normalizeSlotValue(rawSlot(props))
|
|
|
|
}
|
2019-05-31 18:07:43 +08:00
|
|
|
|
|
|
|
export function resolveSlots(
|
|
|
|
instance: ComponentInstance,
|
|
|
|
children: NormalizedChildren
|
|
|
|
) {
|
|
|
|
let slots: Slots | void
|
2019-08-22 23:12:37 +08:00
|
|
|
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
|
2019-05-31 18:07:43 +08:00
|
|
|
if ((children as any)._normalized) {
|
2019-06-03 19:59:15 +08:00
|
|
|
// pre-normalized slots object generated by compiler
|
2019-05-31 18:07:43 +08:00
|
|
|
slots = children as Slots
|
|
|
|
} else {
|
|
|
|
slots = {}
|
2019-06-03 19:58:12 +08:00
|
|
|
for (const key in children as RawSlots) {
|
|
|
|
let value = (children as RawSlots)[key]
|
2019-05-31 18:07:43 +08:00
|
|
|
if (isFunction(value)) {
|
2019-08-31 03:26:16 +08:00
|
|
|
;(slots as any)[key] = normalizeSlot(key, value)
|
2019-05-31 18:07:43 +08:00
|
|
|
} else {
|
|
|
|
if (__DEV__) {
|
2019-08-31 03:34:57 +08:00
|
|
|
warn(
|
|
|
|
`Non-function value encountered for slot "${key}". ` +
|
|
|
|
`Prefer function slots for better performance.`
|
|
|
|
)
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
|
|
|
value = normalizeSlotValue(value)
|
|
|
|
;(slots as any)[key] = () => value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 19:58:12 +08:00
|
|
|
} else if (children !== null) {
|
|
|
|
// non slot object children (direct value) passed to a component
|
2019-05-31 18:07:43 +08:00
|
|
|
if (__DEV__) {
|
2019-08-31 03:34:57 +08:00
|
|
|
warn(
|
|
|
|
`Non-function value encountered for default slot. ` +
|
|
|
|
`Prefer function slots for better performance.`
|
|
|
|
)
|
2019-05-31 18:07:43 +08:00
|
|
|
}
|
|
|
|
const normalized = normalizeSlotValue(children)
|
|
|
|
slots = { default: () => normalized }
|
|
|
|
}
|
|
|
|
if (slots !== void 0) {
|
|
|
|
instance.slots = slots
|
|
|
|
}
|
|
|
|
}
|