← full-stack-fastapi-template / frontend/src/components/ui/form.tsx
| 1 | import * as React from "react" |
| 2 | import * as LabelPrimitive from "@radix-ui/react-label" |
| 3 | import { Slot } from "@radix-ui/react-slot" |
| 4 | import { |
| 5 | Controller, |
| 6 | FormProvider, |
| 7 | useFormContext, |
| 8 | useFormState, |
| 9 | type ControllerProps, |
| 10 | type FieldPath, |
| 11 | type FieldValues, |
| 12 | } from "react-hook-form" |
| 13 | |
| 14 | import { cn } from "@/lib/utils" |
| 15 | import { Label } from "@/components/ui/label" |
| 16 | |
| 17 | const Form = FormProvider |
| 18 | |
| 19 | type FormFieldContextValue< |
| 20 | TFieldValues extends FieldValues = FieldValues, |
| 21 | TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, |
| 22 | > = { |
| 23 | name: TName |
| 24 | } |
| 25 | |
| 26 | const FormFieldContext = React.createContext<FormFieldContextValue>( |
| 27 | {} as FormFieldContextValue |
| 28 | ) |
| 29 | |
| 30 | const FormField = < |
| 31 | TFieldValues extends FieldValues = FieldValues, |
| 32 | TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, |
| 33 | >({ |
| 34 | ...props |
| 35 | }: ControllerProps<TFieldValues, TName>) => { |
| 36 | return ( |
| 37 | <FormFieldContext.Provider value={{ name: props.name }}> |
| 38 | <Controller {...props} /> |
| 39 | </FormFieldContext.Provider> |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | const useFormField = () => { |
| 44 | const fieldContext = React.useContext(FormFieldContext) |
| 45 | const itemContext = React.useContext(FormItemContext) |
| 46 | const { getFieldState } = useFormContext() |
| 47 | const formState = useFormState({ name: fieldContext.name }) |
| 48 | const fieldState = getFieldState(fieldContext.name, formState) |
| 49 | |
| 50 | if (!fieldContext) { |
| 51 | throw new Error("useFormField should be used within <FormField>") |
| 52 | } |
| 53 | |
| 54 | const { id } = itemContext |
| 55 | |
| 56 | return { |
| 57 | id, |
| 58 | name: fieldContext.name, |
| 59 | formItemId: `${id}-form-item`, |
| 60 | formDescriptionId: `${id}-form-item-description`, |
| 61 | formMessageId: `${id}-form-item-message`, |
| 62 | ...fieldState, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | type FormItemContextValue = { |
| 67 | id: string |
| 68 | } |
| 69 | |
| 70 | const FormItemContext = React.createContext<FormItemContextValue>( |
| 71 | {} as FormItemContextValue |
| 72 | ) |
| 73 | |
| 74 | function FormItem({ className, ...props }: React.ComponentProps<"div">) { |
| 75 | const id = React.useId() |
| 76 | |
| 77 | return ( |
| 78 | <FormItemContext.Provider value={{ id }}> |
| 79 | <div |
| 80 | data-slot="form-item" |
| 81 | className={cn("grid gap-2", className)} |
| 82 | {...props} |
| 83 | /> |
| 84 | </FormItemContext.Provider> |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | function FormLabel({ |
| 89 | className, |
| 90 | ...props |
| 91 | }: React.ComponentProps<typeof LabelPrimitive.Root>) { |
| 92 | const { error, formItemId } = useFormField() |
| 93 | |
| 94 | return ( |
| 95 | <Label |
| 96 | data-slot="form-label" |
| 97 | data-error={!!error} |
| 98 | className={cn("data-[error=true]:text-destructive", className)} |
| 99 | htmlFor={formItemId} |
| 100 | {...props} |
| 101 | /> |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | function FormControl({ ...props }: React.ComponentProps<typeof Slot>) { |
| 106 | const { error, formItemId, formDescriptionId, formMessageId } = useFormField() |
| 107 | |
| 108 | return ( |
| 109 | <Slot |
| 110 | data-slot="form-control" |
| 111 | id={formItemId} |
| 112 | aria-describedby={ |
| 113 | !error |
| 114 | ? `${formDescriptionId}` |
| 115 | : `${formDescriptionId} ${formMessageId}` |
| 116 | } |
| 117 | aria-invalid={!!error} |
| 118 | {...props} |
| 119 | /> |
| 120 | ) |
| 121 | } |
| 122 | |
| 123 | function FormDescription({ className, ...props }: React.ComponentProps<"p">) { |
| 124 | const { formDescriptionId } = useFormField() |
| 125 | |
| 126 | return ( |
| 127 | <p |
| 128 | data-slot="form-description" |
| 129 | id={formDescriptionId} |
| 130 | className={cn("text-muted-foreground text-sm", className)} |
| 131 | {...props} |
| 132 | /> |
| 133 | ) |
| 134 | } |
| 135 | |
| 136 | function FormMessage({ className, ...props }: React.ComponentProps<"p">) { |
| 137 | const { error, formMessageId } = useFormField() |
| 138 | const body = error ? String(error?.message ?? "") : props.children |
| 139 | |
| 140 | if (!body) { |
| 141 | return null |
| 142 | } |
| 143 | |
| 144 | return ( |
| 145 | <p |
| 146 | data-slot="form-message" |
| 147 | id={formMessageId} |
| 148 | className={cn("text-destructive text-sm", className)} |
| 149 | {...props} |
| 150 | > |
| 151 | {body} |
| 152 | </p> |
| 153 | ) |
| 154 | } |
| 155 | |
| 156 | export { |
| 157 | useFormField, |
| 158 | Form, |
| 159 | FormItem, |
| 160 | FormLabel, |
| 161 | FormControl, |
| 162 | FormDescription, |
| 163 | FormMessage, |
| 164 | FormField, |
| 165 | } |
| 166 |