Allow to pass label and description via props

This commit is contained in:
thewebartisan7 2024-12-17 11:42:37 +01:00
parent 3eaef4a748
commit caa1010bc7
2 changed files with 42 additions and 16 deletions

View File

@ -1,19 +1,32 @@
<script lang="ts" setup>
import { cn } from '@/lib/utils'
import { useId } from 'radix-vue'
import { type HTMLAttributes, provide } from 'vue'
import { FORM_ITEM_INJECTION_KEY } from './injectionKeys'
import { cn } from "@/lib/utils";
import { useId } from "radix-vue";
import { type HTMLAttributes, provide } from "vue";
import { FORM_ITEM_INJECTION_KEY } from "./injectionKeys";
import { useFormField } from "./useFormField";
const props = defineProps<{
class?: HTMLAttributes['class']
}>()
class?: HTMLAttributes["class"];
label?: string;
description?: string;
}>();
const id = useId()
provide(FORM_ITEM_INJECTION_KEY, id)
const { error } = useFormField();
const id = useId();
provide(FORM_ITEM_INJECTION_KEY, id);
</script>
<template>
<div :class="cn('space-y-2', props.class)">
<FormLabel v-if="props.label">{{ props.label }}</FormLabel>
<slot />
<FormDescription v-if="props.description && !error">
{{ props.description }}
</FormDescription>
<FormMessage v-if="props.description" />
</div>
</template>

View File

@ -1,19 +1,32 @@
<script lang="ts" setup>
import { cn } from '@/lib/utils'
import { useId } from 'radix-vue'
import { type HTMLAttributes, provide } from 'vue'
import { FORM_ITEM_INJECTION_KEY } from './injectionKeys'
import { cn } from "@/lib/utils";
import { useId } from "radix-vue";
import { type HTMLAttributes, provide } from "vue";
import { FORM_ITEM_INJECTION_KEY } from "./injectionKeys";
import { useFormField } from "./useFormField";
const props = defineProps<{
class?: HTMLAttributes['class']
}>()
class?: HTMLAttributes["class"];
label?: string;
description?: string;
}>();
const id = useId()
provide(FORM_ITEM_INJECTION_KEY, id)
const { error } = useFormField();
const id = useId();
provide(FORM_ITEM_INJECTION_KEY, id);
</script>
<template>
<div :class="cn('space-y-2', props.class)">
<FormLabel v-if="props.label">{{ props.label }}</FormLabel>
<slot />
<FormDescription v-if="props.description && !error">
{{ props.description }}
</FormDescription>
<FormMessage v-if="props.description" />
</div>
</template>