webapp/src/components/ui/form/useFormField.ts
padreug b0a2d1a6df Add Shadcn Form components with vee-validate and zod integration
- Install Shadcn Form components (FormControl, FormDescription, FormItem, FormLabel, FormMessage)
- Add vee-validate 4.15.1 for form validation
- Add zod 3.25.76 for schema validation
- Add @vee-validate/zod 4.15.1 for integration
- Update reka-ui to 2.5.0
- Prepare foundation for proper form handling with type-safe validation
- Enables proper checkbox array handling and form accessibility

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-08 16:28:26 +02:00

30 lines
857 B
TypeScript

import { FieldContextKey, useFieldError, useIsFieldDirty, useIsFieldTouched, useIsFieldValid } from "vee-validate"
import { inject } from "vue"
import { FORM_ITEM_INJECTION_KEY } from "./injectionKeys"
export function useFormField() {
const fieldContext = inject(FieldContextKey)
const fieldItemContext = inject(FORM_ITEM_INJECTION_KEY)
if (!fieldContext)
throw new Error("useFormField should be used within <FormField>")
const { name } = fieldContext
const id = fieldItemContext
const fieldState = {
valid: useIsFieldValid(name),
isDirty: useIsFieldDirty(name),
isTouched: useIsFieldTouched(name),
error: useFieldError(name),
}
return {
id,
name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState,
}
}