2019-09-05 15:11:33 +00:00
|
|
|
import {
|
|
|
|
VNode,
|
2019-11-01 13:58:27 +00:00
|
|
|
VNodeProps,
|
2019-09-05 15:11:33 +00:00
|
|
|
createVNode,
|
2020-01-29 03:58:02 +00:00
|
|
|
VNodeArrayChildren,
|
2019-09-05 15:11:33 +00:00
|
|
|
Fragment,
|
2021-03-29 21:38:25 +00:00
|
|
|
Text,
|
|
|
|
Comment,
|
2019-11-04 23:38:55 +00:00
|
|
|
isVNode
|
2019-09-05 15:11:33 +00:00
|
|
|
} from './vnode'
|
2020-03-31 14:52:42 +00:00
|
|
|
import { Teleport, TeleportProps } from './components/Teleport'
|
2019-11-04 23:38:55 +00:00
|
|
|
import { Suspense, SuspenseProps } from './components/Suspense'
|
2019-08-23 19:27:17 +00:00
|
|
|
import { isObject, isArray } from '@vue/shared'
|
2019-09-05 15:11:33 +00:00
|
|
|
import { RawSlots } from './componentSlots'
|
2020-10-20 19:56:29 +00:00
|
|
|
import {
|
|
|
|
FunctionalComponent,
|
|
|
|
Component,
|
|
|
|
ComponentOptions,
|
|
|
|
ConcreteComponent
|
|
|
|
} from './component'
|
2020-07-08 15:51:03 +00:00
|
|
|
import { EmitsOptions } from './componentEmits'
|
2020-09-15 15:46:11 +00:00
|
|
|
import { DefineComponent } from './apiDefineComponent'
|
2019-08-23 19:27:17 +00: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 14:17:16 +00:00
|
|
|
h('div', h('br')) // vnode
|
|
|
|
h(Component, () => {}) // default slot
|
2019-08-23 19:27:17 +00:00
|
|
|
|
|
|
|
// type + props + children
|
|
|
|
h('div', {}, []) // array
|
|
|
|
h('div', {}, 'foo') // text
|
2019-10-10 14:17:16 +00:00
|
|
|
h('div', {}, h('br')) // vnode
|
|
|
|
h(Component, {}, () => {}) // default slot
|
|
|
|
h(Component, {}, {}) // named slots
|
2019-08-23 19:27:17 +00:00
|
|
|
|
|
|
|
// named slots without props requires explicit `null` to avoid ambiguity
|
2019-10-10 14:17:16 +00:00
|
|
|
h(Component, null, {})
|
2019-08-23 19:27:17 +00:00
|
|
|
**/
|
|
|
|
|
2019-11-01 13:58:27 +00:00
|
|
|
type RawProps = VNodeProps & {
|
2019-09-05 15:11:33 +00:00
|
|
|
// used to differ from a single VNode object as children
|
2020-05-02 20:16:51 +00:00
|
|
|
__v_isVNode?: never
|
2019-09-05 15:11:33 +00:00
|
|
|
// used to differ from Array children
|
|
|
|
[Symbol.iterator]?: never
|
2020-09-15 15:46:11 +00:00
|
|
|
} & Record<string, any>
|
2019-09-05 15:11:33 +00:00
|
|
|
|
2019-11-01 13:58:27 +00:00
|
|
|
type RawChildren =
|
2019-09-06 16:58:31 +00:00
|
|
|
| string
|
|
|
|
| number
|
|
|
|
| boolean
|
2019-10-10 14:17:16 +00:00
|
|
|
| VNode
|
2020-01-29 03:58:02 +00:00
|
|
|
| VNodeArrayChildren
|
2019-09-06 16:58:31 +00:00
|
|
|
| (() => any)
|
|
|
|
|
2019-12-22 15:58:12 +00:00
|
|
|
// fake constructor type returned from `defineComponent`
|
2019-09-05 15:11:33 +00:00
|
|
|
interface Constructor<P = any> {
|
2019-11-04 23:38:55 +00:00
|
|
|
__isFragment?: never
|
2020-03-31 14:52:42 +00:00
|
|
|
__isTeleport?: never
|
2019-11-04 23:38:55 +00:00
|
|
|
__isSuspense?: never
|
2020-08-25 01:53:30 +00:00
|
|
|
new (...args: any[]): { $props: P }
|
2019-09-05 15:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following is a series of overloads for providing props validation of
|
|
|
|
// manually written render functions.
|
|
|
|
|
|
|
|
// element
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(type: string, children?: RawChildren): VNode
|
|
|
|
export function h(
|
2019-09-05 15:11:33 +00:00
|
|
|
type: string,
|
2019-09-06 16:58:31 +00:00
|
|
|
props?: RawProps | null,
|
2020-06-12 14:38:56 +00:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2021-03-29 21:38:25 +00:00
|
|
|
// text/comment
|
|
|
|
export function h(
|
|
|
|
type: typeof Text | typeof Comment,
|
|
|
|
children?: string | number | boolean
|
|
|
|
): VNode
|
|
|
|
export function h(
|
|
|
|
type: typeof Text | typeof Comment,
|
|
|
|
props?: null,
|
|
|
|
children?: string | number | boolean
|
|
|
|
): VNode
|
2019-11-02 02:54:01 +00:00
|
|
|
// fragment
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(type: typeof Fragment, children?: VNodeArrayChildren): VNode
|
|
|
|
export function h(
|
2019-09-05 15:11:33 +00:00
|
|
|
type: typeof Fragment,
|
2019-11-02 02:54:01 +00:00
|
|
|
props?: RawProps | null,
|
2020-01-29 03:58:02 +00:00
|
|
|
children?: VNodeArrayChildren
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2020-03-31 14:52:42 +00:00
|
|
|
// teleport (target prop is required)
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(
|
2020-03-31 14:52:42 +00:00
|
|
|
type: typeof Teleport,
|
|
|
|
props: RawProps & TeleportProps,
|
2019-11-02 02:54:01 +00:00
|
|
|
children: RawChildren
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2019-10-29 18:04:44 +00:00
|
|
|
// suspense
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(type: typeof Suspense, children?: RawChildren): VNode
|
|
|
|
export function h(
|
2019-10-29 18:04:44 +00:00
|
|
|
type: typeof Suspense,
|
2019-11-04 23:38:55 +00:00
|
|
|
props?: (RawProps & SuspenseProps) | null,
|
2019-10-29 18:04:44 +00:00
|
|
|
children?: RawChildren | RawSlots
|
|
|
|
): VNode
|
|
|
|
|
2019-09-05 15:11:33 +00:00
|
|
|
// functional component
|
2020-07-08 15:51:03 +00:00
|
|
|
export function h<P, E extends EmitsOptions = {}>(
|
|
|
|
type: FunctionalComponent<P, E>,
|
2019-11-02 02:54:01 +00:00
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
2019-09-06 16:58:31 +00:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2020-04-04 17:29:29 +00:00
|
|
|
// catch-all for generic component types
|
|
|
|
export function h(type: Component, children?: RawChildren): VNode
|
2020-04-24 17:22:58 +00:00
|
|
|
|
2020-10-20 19:56:29 +00:00
|
|
|
// concrete component
|
|
|
|
export function h<P>(
|
|
|
|
type: ConcreteComponent | string,
|
|
|
|
children?: RawChildren
|
|
|
|
): VNode
|
|
|
|
export function h<P>(
|
|
|
|
type: ConcreteComponent<P> | string,
|
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
|
|
|
children?: RawChildren
|
|
|
|
): VNode
|
|
|
|
|
2020-09-15 15:46:11 +00:00
|
|
|
// component without props
|
|
|
|
export function h(
|
|
|
|
type: Component,
|
|
|
|
props: null,
|
|
|
|
children?: RawChildren | RawSlots
|
|
|
|
): VNode
|
|
|
|
|
2020-08-19 20:11:29 +00:00
|
|
|
// exclude `defineComponent` constructors
|
2020-09-15 15:46:11 +00:00
|
|
|
export function h<P>(
|
|
|
|
type: ComponentOptions<P>,
|
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
2019-09-06 16:58:31 +00:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2020-03-12 15:46:32 +00:00
|
|
|
// fake constructor type returned by `defineComponent` or class component
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(type: Constructor, children?: RawChildren): VNode
|
|
|
|
export function h<P>(
|
2019-09-05 15:11:33 +00:00
|
|
|
type: Constructor<P>,
|
2019-11-02 02:54:01 +00:00
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
2019-09-06 16:58:31 +00:00
|
|
|
children?: RawChildren | RawSlots
|
2019-09-05 15:11:33 +00:00
|
|
|
): VNode
|
|
|
|
|
2020-09-15 15:46:11 +00:00
|
|
|
// fake constructor type returned by `defineComponent`
|
|
|
|
export function h(type: DefineComponent, children?: RawChildren): VNode
|
|
|
|
export function h<P>(
|
|
|
|
type: DefineComponent<P>,
|
|
|
|
props?: (RawProps & P) | ({} extends P ? null : never),
|
|
|
|
children?: RawChildren | RawSlots
|
|
|
|
): VNode
|
|
|
|
|
2019-09-05 15:11:33 +00:00
|
|
|
// Actual implementation
|
2020-03-23 20:54:28 +00:00
|
|
|
export function h(type: any, propsOrChildren?: any, children?: any): VNode {
|
2020-08-22 01:54:33 +00:00
|
|
|
const l = arguments.length
|
|
|
|
if (l === 2) {
|
2019-08-23 19:27:17 +00:00
|
|
|
if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
|
2019-10-10 14:17:16 +00:00
|
|
|
// single vnode without props
|
|
|
|
if (isVNode(propsOrChildren)) {
|
|
|
|
return createVNode(type, null, [propsOrChildren])
|
|
|
|
}
|
2019-08-23 19:27:17 +00:00
|
|
|
// props without children
|
|
|
|
return createVNode(type, propsOrChildren)
|
|
|
|
} else {
|
|
|
|
// omit props
|
|
|
|
return createVNode(type, null, propsOrChildren)
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-22 01:54:33 +00:00
|
|
|
if (l > 3) {
|
|
|
|
children = Array.prototype.slice.call(arguments, 2)
|
|
|
|
} else if (l === 3 && isVNode(children)) {
|
2019-10-10 14:17:16 +00:00
|
|
|
children = [children]
|
|
|
|
}
|
2019-08-23 19:27:17 +00:00
|
|
|
return createVNode(type, propsOrChildren, children)
|
|
|
|
}
|
|
|
|
}
|