mirror of https://github.com/vuejs/core.git
fix(runtime-vapor): dynamic component work with insertionState (#13142)
This commit is contained in:
parent
b96447dd41
commit
d65675d902
|
@ -3,6 +3,7 @@ import { nextTick, resolveDynamicComponent } from '@vue/runtime-dom'
|
||||||
import {
|
import {
|
||||||
createComponentWithFallback,
|
createComponentWithFallback,
|
||||||
createDynamicComponent,
|
createDynamicComponent,
|
||||||
|
defineVaporComponent,
|
||||||
renderEffect,
|
renderEffect,
|
||||||
setHtml,
|
setHtml,
|
||||||
setInsertionState,
|
setInsertionState,
|
||||||
|
@ -79,4 +80,34 @@ describe('api: createDynamicComponent', () => {
|
||||||
mount()
|
mount()
|
||||||
expect(html()).toBe('<div><button>hi</button></div>')
|
expect(html()).toBe('<div><button>hi</button></div>')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('switch dynamic component children', async () => {
|
||||||
|
const CompA = defineVaporComponent({
|
||||||
|
setup() {
|
||||||
|
return template('<div>A</div>')()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const CompB = defineVaporComponent({
|
||||||
|
setup() {
|
||||||
|
return template('<div>B</div>')()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const current = shallowRef(CompA)
|
||||||
|
const { html } = define({
|
||||||
|
setup() {
|
||||||
|
const t1 = template('<div></div>')
|
||||||
|
const n2 = t1() as any
|
||||||
|
setInsertionState(n2)
|
||||||
|
createDynamicComponent(() => current.value)
|
||||||
|
return n2
|
||||||
|
},
|
||||||
|
}).render()
|
||||||
|
|
||||||
|
expect(html()).toBe('<div><div>A</div><!--dynamic-component--></div>')
|
||||||
|
|
||||||
|
current.value = CompB
|
||||||
|
await nextTick()
|
||||||
|
expect(html()).toBe('<div><div>B</div><!--dynamic-component--></div>')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
import { resolveDynamicComponent } from '@vue/runtime-dom'
|
import { resolveDynamicComponent } from '@vue/runtime-dom'
|
||||||
import { DynamicFragment, type VaporFragment } from './block'
|
import { DynamicFragment, type VaporFragment, insert } from './block'
|
||||||
import { createComponentWithFallback } from './component'
|
import { createComponentWithFallback } from './component'
|
||||||
import { renderEffect } from './renderEffect'
|
import { renderEffect } from './renderEffect'
|
||||||
import type { RawProps } from './componentProps'
|
import type { RawProps } from './componentProps'
|
||||||
import type { RawSlots } from './componentSlots'
|
import type { RawSlots } from './componentSlots'
|
||||||
|
import {
|
||||||
|
insertionAnchor,
|
||||||
|
insertionParent,
|
||||||
|
resetInsertionState,
|
||||||
|
} from './insertionState'
|
||||||
|
import { isHydrating, locateHydrationNode } from './dom/hydration'
|
||||||
|
|
||||||
export function createDynamicComponent(
|
export function createDynamicComponent(
|
||||||
getter: () => any,
|
getter: () => any,
|
||||||
|
@ -11,9 +17,18 @@ export function createDynamicComponent(
|
||||||
rawSlots?: RawSlots | null,
|
rawSlots?: RawSlots | null,
|
||||||
isSingleRoot?: boolean,
|
isSingleRoot?: boolean,
|
||||||
): VaporFragment {
|
): VaporFragment {
|
||||||
|
const _insertionParent = insertionParent
|
||||||
|
const _insertionAnchor = insertionAnchor
|
||||||
|
if (isHydrating) {
|
||||||
|
locateHydrationNode()
|
||||||
|
} else {
|
||||||
|
resetInsertionState()
|
||||||
|
}
|
||||||
|
|
||||||
const frag = __DEV__
|
const frag = __DEV__
|
||||||
? new DynamicFragment('dynamic-component')
|
? new DynamicFragment('dynamic-component')
|
||||||
: new DynamicFragment()
|
: new DynamicFragment()
|
||||||
|
|
||||||
renderEffect(() => {
|
renderEffect(() => {
|
||||||
const value = getter()
|
const value = getter()
|
||||||
frag.update(
|
frag.update(
|
||||||
|
@ -27,5 +42,10 @@ export function createDynamicComponent(
|
||||||
value,
|
value,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!isHydrating && _insertionParent) {
|
||||||
|
insert(frag, _insertionParent, _insertionAnchor)
|
||||||
|
}
|
||||||
|
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue