2019-09-05 23:11:33 +08:00
|
|
|
import {
|
|
|
|
VNode,
|
2019-11-01 21:58:27 +08:00
|
|
|
VNodeProps,
|
2019-09-05 23:11:33 +08:00
|
|
|
createVNode,
|
|
|
|
VNodeChildren,
|
|
|
|
Fragment,
|
2019-10-10 22:17:16 +08:00
|
|
|
Portal,
|
2019-11-05 07:38:55 +08:00
|
|
|
isVNode
|
2019-09-05 23:11:33 +08:00
|
|
|
} from './vnode'
|
2019-11-05 07:38:55 +08:00
|
|
|
import { Suspense, SuspenseProps } from './components/Suspense'
|
2019-08-24 03:27:17 +08:00
|
|
|
import { isObject, isArray } from '@vue/shared'
|
2019-09-05 23:11:33 +08:00
|
|
|
import { RawSlots } from './componentSlots'
|
2019-09-06 23:19:22 +08:00
|
|
|
import { FunctionalComponent } from './component'
|
2019-09-05 23:11:33 +08:00
|
|
|
import {
|
|
|
|
ComponentOptionsWithoutProps,
|
|
|
|
ComponentOptionsWithArrayProps,
|
2019-10-08 21:26:09 +08:00
|
|
|
ComponentOptionsWithObjectProps,
|
2019-09-06 06:48:49 +08:00
|
|
|
ComponentOptions
|
2019-10-02 22:03:43 +08:00
|
|
|
} from './apiOptions'
|
2019-09-05 23:11:33 +08:00
|
|
|
import { ExtractPropTypes } from './componentProps'
|
2019-08-24 03:27:17 +08:00
|
|
|
|
|
|
|
// `h` is a more user-friendly version of `createVNode` that allows omitting the
|
|
|
|
// props when possible. It is intended for manually written render functions.
|
|
|
|
// Compiler-generated code uses `createVNode` because
|
|
|
|
// 1. it is monomorphic and avoids the extra call overhead
|
|
|
|
// 2. it allows specifying patchFlags for optimization
|
|
|
|
|
|
|
|
/*
|
|
|
|
// type only
|
|
|
|
h('div')
|
|
|
|
|
|
|
|
// type + props
|
|
|
|
h('div', {})
|
|
|
|
|
|
|
|
// type + omit props + children
|
|
|
|
// Omit props does NOT support named slots
|
|
|
|
h('div', []) // array
|
|
|
|
h('div', 'foo') // text
|
2019-10-10 22:17:16 +08:00
|
|
|
h('div', h('br')) // vnode
|
|
|
|
h(Component, () => {}) // default slot
|
2019-08-24 03:27:17 +08:00
|
|
|
|
|
|
|
// type + props + children
|
|
|
|
h('div', {}, []) // array
|
|
|
|
h('div', {}, 'foo') // text
|
2019-10-10 22:17:16 +08:00
|
|
|
h('div', {}, h('br')) // vnode
|
|
|
|
h(Component, {}, () => {}) // default slot
|
|
|
|
h(Component, {}, {}) // named slots
|
2019-08-24 03:27:17 +08:00
|
|
|
|
|
|
|
// named slots without props requires explicit `null` to avoid ambiguity
|
2019-10-10 22:17:16 +08:00
|
|
|
h(Component, null, {})
|
2019-08-24 03:27:17 +08:00
|
|
|
**/
|
|
|
|
|
2019-11-01 21:58:27 +08:00
|
|
|
type RawProps = VNodeProps & {
|
2019-09-05 23:11:33 +08:00
|
|
|
// used to differ from a single VNode object as children
|
|
|
|
_isVNode?: never
|
|
|
|
// used to differ from Array children
|
|
|
|
[Symbol.iterator]?: never
|
|
|
|
}
|
|
|
|
|
2019-11-01 21:58:27 +08:00
|
|
|
type RawChildren =
|
2019-09-07 00:58:31 +08:00
|
|
|
| string
|
|
|
|
| number
|
|
|
|
| boolean
|
2019-10-10 22:17:16 +08:00
|
|
|
| VNode
|
2019-09-07 00:58:31 +08:00
|
|
|
| VNodeChildren
|
|
|
|
| (() => any)
|
|
|
|
|
2019-09-05 23:11:33 +08:00
|
|
|
// fake constructor type returned from `createComponent`
|
|
|
|
interface Constructor<P = any> {
|
2019-11-05 07:38:55 +08:00
|
|
|
__isFragment?: never
|
|
|
|
__isPortal?: never
|
|
|
|
__isSuspense?: never
|
2019-09-05 23:11:33 +08:00
|
|
|
new (): { $props: P }
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following is a series of overloads for providing props validation of
|
|
|
|
// manually written render functions.
|
|
|
|
|
|
|
|
// element
|
2019-09-07 00:58:31 +08:00
|
|
|
export function h(type: string, children?: RawChildren): VNode
|
2019-09-05 23:11:33 +08:00
|
|
|
export function h(
|
|
|
|
type: string,
|
2019-09-07 00:58:31 +08:00
|
|
|
props?: RawProps | null,
|
|
|
|
children?: RawChildren
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
2019-11-02 10:54:01 +08:00
|
|
|
// fragment
|
|
|
|
export function h(type: typeof Fragment, children?: VNodeChildren): VNode
|
2019-09-05 23:11:33 +08:00
|
|
|
export function h(
|
|
|
|
type: typeof Fragment,
|
2019-11-02 10:54:01 +08:00
|
|
|
props?: RawProps | null,
|
|
|
|
children?: VNodeChildren
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
2019-11-02 10:54:01 +08:00
|
|
|
// portal (target prop is required)
|
2019-09-05 23:11:33 +08:00
|
|
|
export function h(
|
|
|
|
type: typeof Portal,
|
2019-11-02 10:54:01 +08:00
|
|
|
props: RawProps & { target: any },
|
|
|
|
children: RawChildren
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
2019-10-30 02:04:44 +08:00
|
|
|
// suspense
|
|
|
|
export function h(type: typeof Suspense, children?: RawChildren): VNode
|
|
|
|
export function h(
|
|
|
|
type: typeof Suspense,
|
2019-11-05 07:38:55 +08:00
|
|
|
props?: (RawProps & SuspenseProps) | null,
|
2019-10-30 02:04:44 +08:00
|
|
|
children?: RawChildren | RawSlots
|
|
|
|
): VNode
|
|
|
|
|
2019-09-05 23:11:33 +08:00
|
|
|
// functional component
|
2019-09-07 00:58:31 +08:00
|
|
|
export function h(type: FunctionalComponent, children?: RawChildren): VNode
|
2019-09-05 23:11:33 +08:00
|
|
|
export function h<P>(
|
|
|
|
type: FunctionalComponent<P>,
|
2019-11-02 10:54:01 +08:00
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
2019-09-07 00:58:31 +08:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
|
|
|
// stateful component
|
2019-09-07 00:58:31 +08:00
|
|
|
export function h(type: ComponentOptions, children?: RawChildren): VNode
|
2019-11-02 10:54:01 +08:00
|
|
|
export function h(
|
|
|
|
type: ComponentOptionsWithoutProps | ComponentOptionsWithArrayProps,
|
2019-11-01 00:43:05 +08:00
|
|
|
props?: RawProps | null,
|
2019-09-07 00:58:31 +08:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
2019-11-02 10:54:01 +08:00
|
|
|
export function h<O>(
|
|
|
|
type: ComponentOptionsWithObjectProps<O>,
|
|
|
|
props?:
|
|
|
|
| (RawProps & ExtractPropTypes<O>)
|
|
|
|
| ({} extends ExtractPropTypes<O> ? null : never),
|
2019-09-07 00:58:31 +08:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
|
|
|
// fake constructor type returned by `createComponent`
|
2019-09-07 00:58:31 +08:00
|
|
|
export function h(type: Constructor, children?: RawChildren): VNode
|
2019-09-05 23:11:33 +08:00
|
|
|
export function h<P>(
|
|
|
|
type: Constructor<P>,
|
2019-11-02 10:54:01 +08:00
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
2019-09-07 00:58:31 +08:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 23:11:33 +08:00
|
|
|
): VNode
|
|
|
|
|
|
|
|
// Actual implementation
|
2019-11-02 05:06:19 +08:00
|
|
|
export function h(type: any, propsOrChildren?: any, children?: any): VNode {
|
2019-08-24 03:27:17 +08:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
|
2019-10-10 22:17:16 +08:00
|
|
|
// single vnode without props
|
|
|
|
if (isVNode(propsOrChildren)) {
|
|
|
|
return createVNode(type, null, [propsOrChildren])
|
|
|
|
}
|
2019-08-24 03:27:17 +08:00
|
|
|
// props without children
|
|
|
|
return createVNode(type, propsOrChildren)
|
|
|
|
} else {
|
|
|
|
// omit props
|
|
|
|
return createVNode(type, null, propsOrChildren)
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-10 22:17:16 +08:00
|
|
|
if (isVNode(children)) {
|
|
|
|
children = [children]
|
|
|
|
}
|
2019-08-24 03:27:17 +08:00
|
|
|
return createVNode(type, propsOrChildren, children)
|
|
|
|
}
|
|
|
|
}
|