Skip to content

Commit

Permalink
fix(jsx-directive/v-slot): default slot and v-if slot co-usage (#590
Browse files Browse the repository at this point in the history
)

* fix(v-slot): default slot and v-if slot co-usage

* chore: add changeset
  • Loading branch information
zhiyuanzmj committed Dec 21, 2023
1 parent 16beecf commit d1a3b4d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/early-zoos-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@vue-macros/jsx-directive": patch
"@vue-macros/volar": patch
---

default slot and v-if slot co-usage
10 changes: 6 additions & 4 deletions packages/jsx-directive/src/core/v-slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ export function transformVSlot(

if (vIfAttribute) {
if (['v-if', 'v-else-if'].includes(`${vIfAttribute.name.name}`)) {
const nextIndex = index + (attributes[index + 1][0] ? 1 : 2)
result.push(
'}',
`${attributes[index + 1]?.[1].vIfAttribute?.name
.name}`.startsWith('v-else')
? ` : `
: ` : null,`,
`${attributes[nextIndex]?.[1].vIfAttribute?.name.name}`.startsWith(
'v-else',
)
? ' : '
: ' : null,',
)
} else if ('v-else' === vIfAttribute.name.name) {
result.push('},')
Expand Down
18 changes: 10 additions & 8 deletions packages/jsx-directive/tests/__snapshots__/v-slot.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ defineRender(() => (
<Comp scopedSlots={{'default': () => <span> </span>,}} />
<Child scopedSlots={{'bottom': ({ foo }) => <span> </span>,}} />
<Child scopedSlots={{'default': () => <span>default</span>,...(show) ? {'bottom': ({ foo }) => <span>
<Child scopedSlots={{...(show) ? {'title': ({ foo }) => <span>
{show}
</span>,} : (show === false) ? {'bot-tom': ({ foo }) => <span>
</span>,} : (show === false) ? {'center': ({ foo }) => <span>
{foo}
</span>,} : {'title': () => <span> </span>,},}}
</span>,} : {'bottom': () => <span> </span>,},'bot-tom': () => <span>bottom</span>,default: () => <span>
default
></Child>
</span>}}></Child>
</div>
))
</script>
Expand Down Expand Up @@ -74,16 +75,17 @@ defineRender(() => (
<Comp v-slots={{'default': () => <> </>,}} />
<Child v-slots={{'bottom': ({ foo }) => <> </>,}} />
<Child v-slots={{'default': () => <>default</>,...(show) ? {'bottom': ({ foo }) => <>
<Child v-slots={{...(show) ? {'title': ({ foo }) => <>
{show}
</>,} : (show === false) ? {'bot-tom': ({ foo }) => <>
</>,} : (show === false) ? {'center': ({ foo }) => <>
{foo}
</>,} : {'title': () => <> </>,},}}
</>,} : {'bottom': () => <> </>,},'bot-tom': () => <>bottom</>,default: () => <>
default
></Child>
</>}}></Child>
</div>
))
</script>
Expand Down
1 change: 1 addition & 0 deletions packages/jsx-directive/tests/fixtures/v-slot/child.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defineSlots<{
default: () => any
title: (props: { foo: 1 }) => any
bottom: (props: { foo: 1 }) => any
center: (props: { foo: 1 }) => any
'bot-tom': (props: { foo: 1 }) => any
}>()
</script>
Expand Down
9 changes: 5 additions & 4 deletions packages/jsx-directive/tests/fixtures/v-slot/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ defineRender(() => (
<Child v-slot:bottom={{ foo }} />
<Child>
<template v-slot>default</template>
<template v-if={show} v-slot:bottom={{ foo }}>
default
<template v-if={show} v-slot:title={{ foo }}>
{show}
</template>
<template v-else-if={show === false} v-slot:bot-tom={{ foo }}>
<template v-else-if={show === false} v-slot:center={{ foo }}>
{foo}
</template>
<template v-else v-slot:title />
<template v-else v-slot:bottom />
<template v-slot:bot-tom>bottom</template>
</Child>
</div>
))
Expand Down
7 changes: 4 additions & 3 deletions packages/volar/src/jsx-directive/v-slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ export function transformVSlot({

if (vIfAttribute && vIfAttributeName) {
if (['v-if', 'v-else-if'].includes(vIfAttributeName)) {
const nextIndex = index + (attributes[index + 1][0] ? 1 : 2)
result.push(
'}',
`${attributes[index + 1]?.[1].vIfAttribute?.name.getText(
`${attributes[nextIndex]?.[1].vIfAttribute?.name.getText(
sfc[source]?.ast,
)}`.startsWith('v-else')
? ` : `
: ` : null,`,
? ' : '
: ' : null,',
)
} else if ('v-else' === vIfAttributeName) {
result.push('},')
Expand Down

0 comments on commit d1a3b4d

Please sign in to comment.