Skip to content

Commit e28b96b

Browse files
committed
chore: tweaks
1 parent 2ba4dc0 commit e28b96b

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,5 +561,64 @@ describe('component: slots', () => {
561561
await nextTick()
562562
expect(html()).toBe('fallback<!--if--><!--slot-->')
563563
})
564+
565+
test('render fallback with nested v-if ', async () => {
566+
const Child = {
567+
setup() {
568+
return createSlot('default', null, () =>
569+
document.createTextNode('fallback'),
570+
)
571+
},
572+
}
573+
574+
const outerShow = ref(false)
575+
const innerShow = ref(false)
576+
577+
const { html } = define({
578+
setup() {
579+
return createComponent(Child, null, {
580+
default: () => {
581+
return createIf(
582+
() => outerShow.value,
583+
() => {
584+
return createIf(
585+
() => innerShow.value,
586+
() => {
587+
return document.createTextNode('content')
588+
},
589+
)
590+
},
591+
)
592+
},
593+
})
594+
},
595+
}).render()
596+
597+
expect(html()).toBe('fallback<!--if--><!--slot-->')
598+
599+
outerShow.value = true
600+
await nextTick()
601+
expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
602+
603+
innerShow.value = true
604+
await nextTick()
605+
expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
606+
607+
innerShow.value = false
608+
await nextTick()
609+
expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
610+
611+
outerShow.value = false
612+
await nextTick()
613+
expect(html()).toBe('fallback<!--if--><!--slot-->')
614+
615+
outerShow.value = true
616+
await nextTick()
617+
expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
618+
619+
innerShow.value = true
620+
await nextTick()
621+
expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
622+
})
564623
})
565624
})

packages/runtime-vapor/src/block.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ export class DynamicFragment extends VaporFragment {
6767

6868
if (this.fallback && !isValidBlock(this.nodes)) {
6969
parent && remove(this.nodes, parent)
70-
// if current nodes is a DynamicFragment, call its update with the fallback
71-
// to handle nested dynamic fragment
72-
if (this.nodes instanceof DynamicFragment) {
73-
this.nodes.update(this.fallback)
70+
// handle nested dynamic fragment
71+
if (isFragment(this.nodes)) {
72+
renderFallback(this.nodes, this.fallback, key)
7473
} else {
7574
this.nodes =
7675
(this.scope || (this.scope = new EffectScope())).run(this.fallback) ||
@@ -83,6 +82,22 @@ export class DynamicFragment extends VaporFragment {
8382
}
8483
}
8584

85+
function renderFallback(
86+
fragment: VaporFragment,
87+
fallback: BlockFn,
88+
key: any,
89+
): void {
90+
if (fragment instanceof DynamicFragment) {
91+
const nodes = fragment.nodes
92+
if (isFragment(nodes)) {
93+
renderFallback(nodes, fallback, key)
94+
} else {
95+
if (!fragment.fallback) fragment.fallback = fallback
96+
fragment.update(fragment.fallback, key)
97+
}
98+
}
99+
}
100+
86101
export function isFragment(val: NonNullable<unknown>): val is VaporFragment {
87102
return val instanceof VaporFragment
88103
}

0 commit comments

Comments
 (0)