Skip to content

Commit fd2917c

Browse files
authored
fix(runtime-vapor): properly handle dynamic slot work with v-if (#12660)
1 parent b6d5399 commit fd2917c

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

packages/runtime-vapor/__tests__/componentSlots.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '../src'
1616
import { currentInstance, nextTick, ref } from '@vue/runtime-dom'
1717
import { makeRender } from './_utils'
18+
import type { DynamicSlot } from '../src/componentSlots'
1819

1920
const define = makeRender<any>()
2021

@@ -468,5 +469,39 @@ describe('component: slots', () => {
468469
await nextTick()
469470
expect(html()).toBe('content<!--if--><!--slot-->')
470471
})
472+
473+
test('dynamic slot work with v-if', async () => {
474+
const val = ref('header')
475+
const toggle = ref(false)
476+
477+
const Comp = defineVaporComponent(() => {
478+
const n0 = template('<div></div>')()
479+
prepend(n0 as any as ParentNode, createSlot('header', null))
480+
return n0
481+
})
482+
483+
const { host } = define(() => {
484+
// dynamic slot
485+
return createComponent(Comp, null, {
486+
$: [
487+
() =>
488+
(toggle.value
489+
? {
490+
name: val.value,
491+
fn: () => {
492+
return template('<h1></h1>')()
493+
},
494+
}
495+
: void 0) as DynamicSlot,
496+
],
497+
})
498+
}).render()
499+
500+
expect(host.innerHTML).toBe('<div><!--slot--></div>')
501+
502+
toggle.value = true
503+
await nextTick()
504+
expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
505+
})
471506
})
472507
})

packages/runtime-vapor/src/componentSlots.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ export function getSlot(
7070
source = dynamicSources[i]
7171
if (isFunction(source)) {
7272
const slot = source()
73-
if (isArray(slot)) {
74-
for (const s of slot) {
75-
if (s.name === key) return s.fn
73+
if (slot) {
74+
if (isArray(slot)) {
75+
for (const s of slot) {
76+
if (s.name === key) return s.fn
77+
}
78+
} else if (slot.name === key) {
79+
return slot.fn
7680
}
77-
} else if (slot.name === key) {
78-
return slot.fn
7981
}
8082
} else if (hasOwn(source, key)) {
8183
return source[key]

0 commit comments

Comments
 (0)