Skip to content

Commit

Permalink
fix(runtime-vapor): properly handle dynamic slot work with v-if (#12660)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored Jan 29, 2025
1 parent b6d5399 commit fd2917c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
35 changes: 35 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../src'
import { currentInstance, nextTick, ref } from '@vue/runtime-dom'
import { makeRender } from './_utils'
import type { DynamicSlot } from '../src/componentSlots'

const define = makeRender<any>()

Expand Down Expand Up @@ -468,5 +469,39 @@ describe('component: slots', () => {
await nextTick()
expect(html()).toBe('content<!--if--><!--slot-->')
})

test('dynamic slot work with v-if', async () => {
const val = ref('header')
const toggle = ref(false)

const Comp = defineVaporComponent(() => {
const n0 = template('<div></div>')()
prepend(n0 as any as ParentNode, createSlot('header', null))
return n0
})

const { host } = define(() => {
// dynamic slot
return createComponent(Comp, null, {
$: [
() =>
(toggle.value
? {
name: val.value,
fn: () => {
return template('<h1></h1>')()
},
}
: void 0) as DynamicSlot,
],
})
}).render()

expect(host.innerHTML).toBe('<div><!--slot--></div>')

toggle.value = true
await nextTick()
expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
})
})
})
12 changes: 7 additions & 5 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ export function getSlot(
source = dynamicSources[i]
if (isFunction(source)) {
const slot = source()
if (isArray(slot)) {
for (const s of slot) {
if (s.name === key) return s.fn
if (slot) {
if (isArray(slot)) {
for (const s of slot) {
if (s.name === key) return s.fn
}
} else if (slot.name === key) {
return slot.fn
}
} else if (slot.name === key) {
return slot.fn
}
} else if (hasOwn(source, key)) {
return source[key]
Expand Down

0 comments on commit fd2917c

Please sign in to comment.