1- "use client"
1+ "use client" ;
22
3- import * as React from "react"
4- import * as LabelPrimitive from "@radix-ui/react-label"
5- import { Slot } from "@radix-ui/react-slot"
3+ import * as React from "react" ;
4+ import * as LabelPrimitive from "@radix-ui/react-label" ;
5+ import { Slot } from "@radix-ui/react-slot" ;
66import {
77 Controller ,
88 FormProvider ,
@@ -11,23 +11,21 @@ import {
1111 type ControllerProps ,
1212 type FieldPath ,
1313 type FieldValues ,
14- } from "react-hook-form"
14+ } from "react-hook-form" ;
1515
16- import { cn } from "@/lib/utils"
17- import { Label } from "@/components/ui/label"
16+ import { cn } from "@/lib/utils" ;
17+ import { Label } from "@/components/ui/label" ;
1818
19- const Form = FormProvider
19+ const Form = FormProvider ;
2020
2121type FormFieldContextValue <
2222 TFieldValues extends FieldValues = FieldValues ,
2323 TName extends FieldPath < TFieldValues > = FieldPath < TFieldValues > ,
2424> = {
25- name : TName
26- }
25+ name : TName ;
26+ } ;
2727
28- const FormFieldContext = React . createContext < FormFieldContextValue > (
29- { } as FormFieldContextValue
30- )
28+ const FormFieldContext = React . createContext < FormFieldContextValue > ( { } as FormFieldContextValue ) ;
3129
3230const FormField = <
3331 TFieldValues extends FieldValues = FieldValues ,
@@ -39,21 +37,21 @@ const FormField = <
3937 < FormFieldContext . Provider value = { { name : props . name } } >
4038 < Controller { ...props } />
4139 </ FormFieldContext . Provider >
42- )
43- }
40+ ) ;
41+ } ;
4442
4543const useFormField = ( ) => {
46- const fieldContext = React . useContext ( FormFieldContext )
47- const itemContext = React . useContext ( FormItemContext )
48- const { getFieldState } = useFormContext ( )
49- const formState = useFormState ( { name : fieldContext . name } )
50- const fieldState = getFieldState ( fieldContext . name , formState )
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 ) ;
5149
5250 if ( ! fieldContext ) {
53- throw new Error ( "useFormField should be used within <FormField>" )
51+ throw new Error ( "useFormField should be used within <FormField>" ) ;
5452 }
5553
56- const { id } = itemContext
54+ const { id } = itemContext ;
5755
5856 return {
5957 id,
@@ -62,36 +60,27 @@ const useFormField = () => {
6260 formDescriptionId : `${ id } -form-item-description` ,
6361 formMessageId : `${ id } -form-item-message` ,
6462 ...fieldState ,
65- }
66- }
63+ } ;
64+ } ;
6765
6866type FormItemContextValue = {
69- id : string
70- }
67+ id : string ;
68+ } ;
7169
72- const FormItemContext = React . createContext < FormItemContextValue > (
73- { } as FormItemContextValue
74- )
70+ const FormItemContext = React . createContext < FormItemContextValue > ( { } as FormItemContextValue ) ;
7571
7672function FormItem ( { className, ...props } : React . ComponentProps < "div" > ) {
77- const id = React . useId ( )
73+ const id = React . useId ( ) ;
7874
7975 return (
8076 < FormItemContext . Provider value = { { id } } >
81- < div
82- data-slot = "form-item"
83- className = { cn ( "grid gap-2" , className ) }
84- { ...props }
85- />
77+ < div data-slot = "form-item" className = { cn ( "grid gap-2" , className ) } { ...props } />
8678 </ FormItemContext . Provider >
87- )
79+ ) ;
8880}
8981
90- function FormLabel ( {
91- className,
92- ...props
93- } : React . ComponentProps < typeof LabelPrimitive . Root > ) {
94- const { error, formItemId } = useFormField ( )
82+ function FormLabel ( { className, ...props } : React . ComponentProps < typeof LabelPrimitive . Root > ) {
83+ const { error, formItemId } = useFormField ( ) ;
9584
9685 return (
9786 < Label
@@ -101,29 +90,25 @@ function FormLabel({
10190 htmlFor = { formItemId }
10291 { ...props }
10392 />
104- )
93+ ) ;
10594}
10695
10796function FormControl ( { ...props } : React . ComponentProps < typeof Slot > ) {
108- const { error, formItemId, formDescriptionId, formMessageId } = useFormField ( )
97+ const { error, formItemId, formDescriptionId, formMessageId } = useFormField ( ) ;
10998
11099 return (
111100 < Slot
112101 data-slot = "form-control"
113102 id = { formItemId }
114- aria-describedby = {
115- ! error
116- ? `${ formDescriptionId } `
117- : `${ formDescriptionId } ${ formMessageId } `
118- }
103+ aria-describedby = { ! error ? `${ formDescriptionId } ` : `${ formDescriptionId } ${ formMessageId } ` }
119104 aria-invalid = { ! ! error }
120105 { ...props }
121106 />
122- )
107+ ) ;
123108}
124109
125110function FormDescription ( { className, ...props } : React . ComponentProps < "p" > ) {
126- const { formDescriptionId } = useFormField ( )
111+ const { formDescriptionId } = useFormField ( ) ;
127112
128113 return (
129114 < p
@@ -132,36 +117,22 @@ function FormDescription({ className, ...props }: React.ComponentProps<"p">) {
132117 className = { cn ( "text-muted-foreground text-sm" , className ) }
133118 { ...props }
134119 />
135- )
120+ ) ;
136121}
137122
138123function FormMessage ( { className, ...props } : React . ComponentProps < "p" > ) {
139- const { error, formMessageId } = useFormField ( )
140- const body = error ? String ( error ?. message ?? "" ) : props . children
124+ const { error, formMessageId } = useFormField ( ) ;
125+ const body = error ? String ( error ?. message ?? "" ) : props . children ;
141126
142127 if ( ! body ) {
143- return null
128+ return null ;
144129 }
145130
146131 return (
147- < p
148- data-slot = "form-message"
149- id = { formMessageId }
150- className = { cn ( "text-destructive text-sm" , className ) }
151- { ...props }
152- >
132+ < p data-slot = "form-message" id = { formMessageId } className = { cn ( "text-destructive text-sm" , className ) } { ...props } >
153133 { body }
154134 </ p >
155- )
135+ ) ;
156136}
157137
158- export {
159- useFormField ,
160- Form ,
161- FormItem ,
162- FormLabel ,
163- FormControl ,
164- FormDescription ,
165- FormMessage ,
166- FormField ,
167- }
138+ export { useFormField , Form , FormItem , FormLabel , FormControl , FormDescription , FormMessage , FormField } ;
0 commit comments