vue3-core/packages/runtime-core/src/compat/renderFn.ts

104 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-04-08 22:06:12 +08:00
import { isArray, isObject } from '@vue/shared'
import { Component, Data } from '../component'
import {
createVNode,
isVNode,
VNode,
VNodeArrayChildren,
VNodeProps
} from '../vnode'
interface LegacyVNodeProps {
key?: string | number
ref?: string
refInFor?: boolean
staticClass?: string
class?: unknown
staticStyle?: Record<string, unknown>
style?: Record<string, unknown>
attrs?: Record<string, unknown>
domProps?: Record<string, unknown>
on?: Record<string, Function | Function[]>
nativeOn?: Record<string, Function | Function[]>
directives?: LegacyVNodeDirective[]
slot?: string
scopedSlots?: Record<string, Function>
}
interface LegacyVNodeDirective {
name: string
value: unknown
arg?: string
modifiers?: Record<string, boolean>
}
type LegacyVNodeChildren =
| string
| number
| boolean
| VNode
| VNodeArrayChildren
export function compatH(
2021-04-08 22:06:12 +08:00
type: string | Component,
children?: LegacyVNodeChildren
): VNode
export function compatH(
2021-04-08 22:06:12 +08:00
type: string | Component,
props?: LegacyVNodeProps,
children?: LegacyVNodeChildren
): VNode
export function compatH(
type: any,
propsOrChildren?: any,
children?: any
): VNode {
2021-04-08 22:06:12 +08:00
const l = arguments.length
if (l === 2) {
if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
// single vnode without props
if (isVNode(propsOrChildren)) {
return convertLegacySlots(createVNode(type, null, [propsOrChildren]))
}
// props without children
return convertLegacyDirectives(
createVNode(type, convertLegacyProps(propsOrChildren)),
propsOrChildren
)
} else {
// omit props
return convertLegacySlots(createVNode(type, null, propsOrChildren))
}
} else {
if (l > 3) {
children = Array.prototype.slice.call(arguments, 2)
} else if (l === 3 && isVNode(children)) {
children = [children]
}
return convertLegacySlots(
convertLegacyDirectives(
createVNode(type, convertLegacyProps(propsOrChildren), children),
propsOrChildren
)
)
}
}
function convertLegacyProps(props: LegacyVNodeProps): Data & VNodeProps {
// TODO
return props as any
2021-04-08 22:06:12 +08:00
}
function convertLegacyDirectives(vnode: VNode, props: LegacyVNodeProps): VNode {
// TODO
return vnode
}
function convertLegacySlots(vnode: VNode): VNode {
// TODO
return vnode
}