|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import * as LabelPrimitive from "@radix-ui/react-label"; |
| 5 | +import { Slot } from "@radix-ui/react-slot"; |
| 6 | +import { Controller, ControllerProps, FieldPath, FieldValues, FormProvider, useFormContext } from "react-hook-form"; |
| 7 | + |
| 8 | +import { cn } from "@/lib/utils"; |
| 9 | +import { Label } from "@/components/ui/Label"; |
| 10 | + |
| 11 | +const Form = FormProvider; |
| 12 | + |
| 13 | +type FormFieldContextValue< |
| 14 | + TFieldValues extends FieldValues = FieldValues, |
| 15 | + TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, |
| 16 | +> = { |
| 17 | + name: TName; |
| 18 | +}; |
| 19 | + |
| 20 | +const FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue); |
| 21 | + |
| 22 | +const FormField = < |
| 23 | + TFieldValues extends FieldValues = FieldValues, |
| 24 | + TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, |
| 25 | +>({ |
| 26 | + ...props |
| 27 | +}: ControllerProps<TFieldValues, TName>) => { |
| 28 | + return ( |
| 29 | + <FormFieldContext.Provider value={{ name: props.name }}> |
| 30 | + <Controller {...props} /> |
| 31 | + </FormFieldContext.Provider> |
| 32 | + ); |
| 33 | +}; |
| 34 | + |
| 35 | +const useFormField = () => { |
| 36 | + const fieldContext = React.useContext(FormFieldContext); |
| 37 | + const itemContext = React.useContext(FormItemContext); |
| 38 | + const { getFieldState, formState } = useFormContext(); |
| 39 | + |
| 40 | + const fieldState = getFieldState(fieldContext.name, formState); |
| 41 | + |
| 42 | + if (!fieldContext) { |
| 43 | + throw new Error("useFormField should be used within <FormField>"); |
| 44 | + } |
| 45 | + |
| 46 | + const { id } = itemContext; |
| 47 | + |
| 48 | + return { |
| 49 | + id, |
| 50 | + name: fieldContext.name, |
| 51 | + formItemId: `${id}-form-item`, |
| 52 | + formDescriptionId: `${id}-form-item-description`, |
| 53 | + formMessageId: `${id}-form-item-message`, |
| 54 | + ...fieldState, |
| 55 | + }; |
| 56 | +}; |
| 57 | + |
| 58 | +type FormItemContextValue = { |
| 59 | + id: string; |
| 60 | +}; |
| 61 | + |
| 62 | +const FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue); |
| 63 | + |
| 64 | +const FormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( |
| 65 | + ({ className, ...props }, ref) => { |
| 66 | + const id = React.useId(); |
| 67 | + |
| 68 | + return ( |
| 69 | + <FormItemContext.Provider value={{ id }}> |
| 70 | + <div ref={ref} className={cn("space-y-2", className)} {...props} /> |
| 71 | + </FormItemContext.Provider> |
| 72 | + ); |
| 73 | + } |
| 74 | +); |
| 75 | +FormItem.displayName = "FormItem"; |
| 76 | + |
| 77 | +const FormLabel = React.forwardRef< |
| 78 | + React.ElementRef<typeof LabelPrimitive.Root>, |
| 79 | + React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> |
| 80 | +>(({ className, ...props }, ref) => { |
| 81 | + const { error, formItemId } = useFormField(); |
| 82 | + |
| 83 | + return <Label ref={ref} className={cn(error && "text-destructive", className)} htmlFor={formItemId} {...props} />; |
| 84 | +}); |
| 85 | +FormLabel.displayName = "FormLabel"; |
| 86 | + |
| 87 | +const FormControl = React.forwardRef<React.ElementRef<typeof Slot>, React.ComponentPropsWithoutRef<typeof Slot>>( |
| 88 | + ({ ...props }, ref) => { |
| 89 | + const { error, formItemId, formDescriptionId, formMessageId } = useFormField(); |
| 90 | + |
| 91 | + return ( |
| 92 | + <Slot |
| 93 | + ref={ref} |
| 94 | + id={formItemId} |
| 95 | + aria-describedby={!error ? `${formDescriptionId}` : `${formDescriptionId} ${formMessageId}`} |
| 96 | + aria-invalid={!!error} |
| 97 | + {...props} |
| 98 | + /> |
| 99 | + ); |
| 100 | + } |
| 101 | +); |
| 102 | +FormControl.displayName = "FormControl"; |
| 103 | + |
| 104 | +const FormDescription = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>( |
| 105 | + ({ className, ...props }, ref) => { |
| 106 | + const { formDescriptionId } = useFormField(); |
| 107 | + |
| 108 | + return <p ref={ref} id={formDescriptionId} className={cn("text-sm text-muted-foreground", className)} {...props} />; |
| 109 | + } |
| 110 | +); |
| 111 | +FormDescription.displayName = "FormDescription"; |
| 112 | + |
| 113 | +const FormMessage = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>( |
| 114 | + ({ className, children, ...props }, ref) => { |
| 115 | + const { error, formMessageId } = useFormField(); |
| 116 | + const body = error ? String(error?.message) : children; |
| 117 | + |
| 118 | + if (!body) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + return ( |
| 123 | + <p ref={ref} id={formMessageId} className={cn("text-sm font-medium text-destructive", className)} {...props}> |
| 124 | + {body} |
| 125 | + </p> |
| 126 | + ); |
| 127 | + } |
| 128 | +); |
| 129 | +FormMessage.displayName = "FormMessage"; |
| 130 | + |
| 131 | +export { useFormField, Form, FormItem, FormLabel, FormControl, FormDescription, FormMessage, FormField }; |
0 commit comments