Skip to content

fix(runtime-core): fix rendering error when Element and Teleport children are function #9108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/dts-test/h.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('h inference w/ element', () => {
// slots
const slots = { default: () => {} } // RawSlots
h('div', {}, slots)
h('div', {}, () => 'hello')
// events
h('div', {
onClick: e => {
Expand All @@ -58,6 +59,7 @@ describe('h inference w/ Fragment', () => {
describe('h inference w/ Teleport', () => {
h(Teleport, { to: '#foo' }, 'hello')
h(Teleport, { to: '#foo' }, { default() {} })
h(Teleport, { to: '#foo' }, () => 'hello')
// @ts-expect-error
h(Teleport)
// @ts-expect-error
Expand Down
8 changes: 3 additions & 5 deletions packages/runtime-core/__tests__/vnode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ describe('vnode', () => {
})

describe('children normalization', () => {
const nop = vi.fn

test('null', () => {
const vnode = createVNode('p', null, null)
expect(vnode.children).toBe(null)
Expand All @@ -145,10 +143,10 @@ describe('vnode', () => {
})

test('function', () => {
const vnode = createVNode('p', null, nop)
expect(vnode.children).toMatchObject({ default: nop })
const vnode = createVNode('p', null, () => 'foo')
expect(vnode.children).toBe('foo')
expect(vnode.shapeFlag).toBe(
ShapeFlags.ELEMENT | ShapeFlags.SLOTS_CHILDREN,
ShapeFlags.ELEMENT | ShapeFlags.TEXT_CHILDREN,
)
})

Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
}
}
} else if (isFunction(children)) {
children = { default: children, _ctx: currentRenderingInstance }
type = ShapeFlags.SLOTS_CHILDREN
normalizeChildren(vnode, { default: children })
return
} else {
children = String(children)
// force teleport children to array so it can be moved around
Expand Down