polish(teleport): do not warn missing target when teleport is disabled (#2021)

This commit is contained in:
HcySunYang 2020-09-02 09:05:51 +08:00 committed by GitHub
parent 0d0970f9cd
commit 93b8ff94a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View File

@ -179,6 +179,36 @@ describe('renderer: teleport', () => {
) )
}) })
test('should work when using template ref as target', async () => {
const root = nodeOps.createElement('div')
const target = ref(null)
const disabled = ref(true)
const App = {
setup() {
return () =>
h(Fragment, [
h('div', { ref: target }),
h(
Teleport,
{ to: target.value, disabled: disabled.value },
h('div', 'teleported')
)
])
}
}
render(h(App), root)
expect(serializeInner(root)).toMatchInlineSnapshot(
`"<div></div><!--teleport start--><div>teleported</div><!--teleport end-->"`
)
disabled.value = false
await nextTick()
expect(serializeInner(root)).toMatchInlineSnapshot(
`"<div><div>teleported</div></div><!--teleport start--><!--teleport end-->"`
)
})
test('disabled', () => { test('disabled', () => {
const target = nodeOps.createElement('div') const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div') const root = nodeOps.createElement('div')

View File

@ -14,7 +14,7 @@ import { warn } from '../warning'
export type TeleportVNode = VNode<RendererNode, RendererElement, TeleportProps> export type TeleportVNode = VNode<RendererNode, RendererElement, TeleportProps>
export interface TeleportProps { export interface TeleportProps {
to: string | RendererElement to: string | RendererElement | null | undefined
disabled?: boolean disabled?: boolean
} }
@ -50,7 +50,7 @@ const resolveTarget = <T = RendererElement>(
return target as any return target as any
} }
} else { } else {
if (__DEV__ && !targetSelector) { if (__DEV__ && !targetSelector && !isTeleportDisabled(props)) {
warn(`Invalid Teleport target: ${targetSelector}`) warn(`Invalid Teleport target: ${targetSelector}`)
} }
return targetSelector as any return targetSelector as any
@ -94,7 +94,7 @@ export const TeleportImpl = {
const targetAnchor = (n2.targetAnchor = createText('')) const targetAnchor = (n2.targetAnchor = createText(''))
if (target) { if (target) {
insert(targetAnchor, target) insert(targetAnchor, target)
} else if (__DEV__) { } else if (__DEV__ && !disabled) {
warn('Invalid Teleport target on mount:', target, `(${typeof target})`) warn('Invalid Teleport target on mount:', target, `(${typeof target})`)
} }