Skip to content

Commit 140a89b

Browse files
authored
fix(teleport): handle target change while disabled (#7837)
close #7835
1 parent ceb0732 commit 140a89b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/runtime-core/__tests__/components/Teleport.spec.ts

+53
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,57 @@ describe('renderer: teleport', () => {
475475
expect(dir.mounted).toHaveBeenCalledTimes(1)
476476
expect(dir.unmounted).toHaveBeenCalledTimes(1)
477477
})
478+
479+
// #7835
480+
test(`ensure that target changes when disabled are updated correctly when enabled`, async () => {
481+
const root = nodeOps.createElement('div')
482+
const target1 = nodeOps.createElement('div')
483+
const target2 = nodeOps.createElement('div')
484+
const target3 = nodeOps.createElement('div')
485+
const target = ref(target1)
486+
const disabled = ref(true)
487+
488+
const App = {
489+
setup() {
490+
return () =>
491+
h(Fragment, [
492+
h(
493+
Teleport,
494+
{ to: target.value, disabled: disabled.value },
495+
h('div', 'teleported')
496+
)
497+
])
498+
}
499+
}
500+
render(h(App), root)
501+
disabled.value = false
502+
await nextTick()
503+
expect(serializeInner(target1)).toMatchInlineSnapshot(
504+
`"<div>teleported</div>"`
505+
)
506+
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
507+
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)
508+
509+
disabled.value = true
510+
await nextTick()
511+
target.value = target2
512+
await nextTick()
513+
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
514+
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
515+
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)
516+
517+
target.value = target3
518+
await nextTick()
519+
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
520+
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
521+
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)
522+
523+
disabled.value = false
524+
await nextTick()
525+
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
526+
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
527+
expect(serializeInner(target3)).toMatchInlineSnapshot(
528+
`"<div>teleported</div>"`
529+
)
530+
})
478531
})

packages/runtime-core/src/components/Teleport.ts

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ export const TeleportImpl = {
186186
internals,
187187
TeleportMoveTypes.TOGGLE
188188
)
189+
} else {
190+
// #7835
191+
// When `teleport` is disabled, `to` may change, making it always old,
192+
// to ensure the correct `to` when enabled
193+
if (n2.props && n1.props && n2.props.to !== n1.props.to) {
194+
n2.props.to = n1.props.to
195+
}
189196
}
190197
} else {
191198
// target changed

0 commit comments

Comments
 (0)