Skip to content

Commit

Permalink
feat(runtime-core): support variadic children in h for simple JSX c…
Browse files Browse the repository at this point in the history
…ompat

ref: #1917
  • Loading branch information
yyx990803 committed Aug 22, 2020
1 parent 6602d6d commit 54d06ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/runtime-core/__tests__/h.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ describe('renderer: h', () => {
})
)
})

// for simple JSX compat
// note this signature is not supported in types; it's purely for usage with
// compiled code.
test('support variadic children', () => {
// @ts-ignore
const vnode = h('div', null, h('span'), h('span'))
expect(vnode.children).toMatchObject([
{
type: 'span'
},
{
type: 'span'
}
])
})
})
7 changes: 5 additions & 2 deletions packages/runtime-core/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ export function h<P>(

// Actual implementation
export function h(type: any, propsOrChildren?: any, children?: any): VNode {
if (arguments.length === 2) {
const l = arguments.length
if (l === 2) {
if (isObject(propsOrChildren) && !isArray(propsOrChildren)) {
// single vnode without props
if (isVNode(propsOrChildren)) {
Expand All @@ -142,7 +143,9 @@ export function h(type: any, propsOrChildren?: any, children?: any): VNode {
return createVNode(type, null, propsOrChildren)
}
} else {
if (isVNode(children)) {
if (l > 3) {
children = Array.prototype.slice.call(arguments, 2)
} else if (l === 3 && isVNode(children)) {
children = [children]
}
return createVNode(type, propsOrChildren, children)
Expand Down

0 comments on commit 54d06ec

Please sign in to comment.