Skip to content
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

fix(Teleport): component with multi roots should be removed when unmounted #3157

Merged
merged 2 commits into from
Mar 25, 2021
Merged
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
20 changes: 20 additions & 0 deletions packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ describe('renderer: teleport', () => {
testUnmount({ to: null, disabled: true })
})

test('component with multi roots should be removed when unmounted', () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')

const Comp = {
render() {
return [h('p'), h('p')]
}
}

render(
h(() => [h(Teleport, { to: target }, h(Comp)), h('div', 'root')]),
root
)
expect(serializeInner(target)).toMatchInlineSnapshot(`"<p></p><p></p>"`)

render(null, root)
expect(serializeInner(target)).toBe('')
})

test('multiple teleport with same target', () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
Expand Down
15 changes: 12 additions & 3 deletions packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface TeleportProps {

export const isTeleport = (type: any): boolean => type.__isTeleport

export const isTeleportDisabled = (props: VNode['props']): boolean =>
const isTeleportDisabled = (props: VNode['props']): boolean =>
props && (props.disabled || props.disabled === '')

const isTargetSVG = (target: RendererElement): boolean =>
Expand Down Expand Up @@ -218,7 +218,10 @@ export const TeleportImpl = {

remove(
vnode: VNode,
{ r: remove, o: { remove: hostRemove } }: RendererInternals,
parentComponent: ComponentInternalInstance | null,
parentSuspense: SuspenseBoundary | null,
optimized: boolean,
{ um: unmount, o: { remove: hostRemove } }: RendererInternals,
doRemove: Boolean
) {
const { shapeFlag, children, anchor, targetAnchor, target, props } = vnode
Expand All @@ -232,7 +235,13 @@ export const TeleportImpl = {
hostRemove(anchor!)
if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
for (let i = 0; i < (children as VNode[]).length; i++) {
remove((children as VNode[])[i])
unmount(
(children as VNode[])[i],
parentComponent,
parentSuspense,
true,
optimized
)
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,14 @@ function baseCreateRenderer(
}

if (shapeFlag & ShapeFlags.TELEPORT) {
;(vnode.type as typeof TeleportImpl).remove(vnode, internals, doRemove)
;(vnode.type as typeof TeleportImpl).remove(
vnode,
parentComponent,
parentSuspense,
optimized,
internals,
doRemove
)
}

if (doRemove) {
Expand Down