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