Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(volar/jsx-directive): support function component in v-slot #499

Merged
merged 6 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-impalas-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vue-macros/volar': patch
---

support function component in v-slot
9 changes: 8 additions & 1 deletion packages/volar/src/jsx-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ function transformVSlot({
}: TransformOptions & {
nodes: import('typescript/lib/tsserverlibrary').JsxElement[]
}) {
if (nodes.length === 0) return
codes.push(`type __VLS_getSlots<T> = T extends new (...args: any) => any
? InstanceType<T>['$slots']
: T extends (props: any, ctx: infer Ctx) => any
? Ctx['slots']
: any`)

nodes.forEach((node) => {
if (!ts.isIdentifier(node.openingElement.tagName)) return

Expand Down Expand Up @@ -296,7 +303,7 @@ function transformVSlot({
'</>,',
]
),
`} as InstanceType<typeof ${node.openingElement.tagName.escapedText}>['$slots'] }`,
`} as __VLS_getSlots<typeof ${node.openingElement.tagName.escapedText}> }`,
] as Segment<FileRangeCapabilities>[]

if (attribute) {
Expand Down
13 changes: 13 additions & 0 deletions playground/vue3/src/examples/jsx-directive/v-slot/index.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<script setup lang="tsx">
import { type FunctionalComponent } from 'vue'
import Child from './child.vue'

const Comp: FunctionalComponent<
{},
{},
{
default: (scope: { foo: string }) => any
}
> = (props, { slots }) => {
return <slots.default foo="foo"></slots.default>
}

defineRender(() => (
<fieldset>
<legend>v-slot</legend>

<Comp v-slot={{ foo }}>{''}</Comp>

<Child>
<div>default: begin</div>

Expand Down