Skip to content

Commit

Permalink
fix(teleport): handle target change while disabled (#7837)
Browse files Browse the repository at this point in the history
close #7835
  • Loading branch information
baiwusanyu-c authored Jul 11, 2023
1 parent ceb0732 commit 140a89b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/runtime-core/__tests__/components/Teleport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,57 @@ describe('renderer: teleport', () => {
expect(dir.mounted).toHaveBeenCalledTimes(1)
expect(dir.unmounted).toHaveBeenCalledTimes(1)
})

// #7835
test(`ensure that target changes when disabled are updated correctly when enabled`, async () => {
const root = nodeOps.createElement('div')
const target1 = nodeOps.createElement('div')
const target2 = nodeOps.createElement('div')
const target3 = nodeOps.createElement('div')
const target = ref(target1)
const disabled = ref(true)

const App = {
setup() {
return () =>
h(Fragment, [
h(
Teleport,
{ to: target.value, disabled: disabled.value },
h('div', 'teleported')
)
])
}
}
render(h(App), root)
disabled.value = false
await nextTick()
expect(serializeInner(target1)).toMatchInlineSnapshot(
`"<div>teleported</div>"`
)
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)

disabled.value = true
await nextTick()
target.value = target2
await nextTick()
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)

target.value = target3
await nextTick()
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target3)).toMatchInlineSnapshot(`""`)

disabled.value = false
await nextTick()
expect(serializeInner(target1)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target2)).toMatchInlineSnapshot(`""`)
expect(serializeInner(target3)).toMatchInlineSnapshot(
`"<div>teleported</div>"`
)
})
})
7 changes: 7 additions & 0 deletions packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ export const TeleportImpl = {
internals,
TeleportMoveTypes.TOGGLE
)
} else {
// #7835
// When `teleport` is disabled, `to` may change, making it always old,
// to ensure the correct `to` when enabled
if (n2.props && n1.props && n2.props.to !== n1.props.to) {
n2.props.to = n1.props.to
}
}
} else {
// target changed
Expand Down

0 comments on commit 140a89b

Please sign in to comment.