vue3-yuanma/packages/runtime-core/src/h.ts

162 lines
4.2 KiB
TypeScript
Raw Normal View History

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,
Portal,
isVNode
2019-09-05 23:11:33 +08:00
} from './vnode'
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'
import { FunctionalComponent } from './component'
2019-09-05 23:11:33 +08:00
import {
ComponentOptionsWithoutProps,
ComponentOptionsWithArrayProps,
2019-10-08 21:26:09 +08:00
ComponentOptionsWithObjectProps,
ComponentOptions
} 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
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
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
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
| 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> {
__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
// fragment
export function h(type: typeof Fragment, children?: VNodeChildren): VNode
2019-09-05 23:11:33 +08:00
export function h(
type: typeof Fragment,
props?: RawProps | null,
children?: VNodeChildren
2019-09-05 23:11:33 +08:00
): VNode
// portal (target prop is required)
2019-09-05 23:11:33 +08:00
export function h(
type: typeof Portal,
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,
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>,
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
export function h(
type: ComponentOptionsWithoutProps | ComponentOptionsWithArrayProps,
props?: RawProps | null,
2019-09-07 00:58:31 +08:00
children?: RawChildren | RawSlots
2019-09-05 23:11:33 +08:00
): VNode
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>,
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
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)) {
// 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 {
if (isVNode(children)) {
children = [children]
}
2019-08-24 03:27:17 +08:00
return createVNode(type, propsOrChildren, children)
}
}