1
- "use client"
1
+ "use client" ;
2
2
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" ;
6
6
import {
7
7
Controller ,
8
8
ControllerProps ,
9
9
FieldPath ,
10
10
FieldValues ,
11
11
FormProvider ,
12
12
useFormContext ,
13
- } from "react-hook-form"
13
+ } from "react-hook-form" ;
14
14
15
- import { cn } from "@/lib/utils"
16
- import { Label } from "@/components/ui/label"
15
+ import { cn } from "@/lib/utils" ;
16
+ import { Label } from "@/components/ui/label" ;
17
17
18
- const Form = FormProvider
18
+ const Form = FormProvider ;
19
19
20
20
type FormFieldContextValue <
21
21
TFieldValues extends FieldValues = FieldValues ,
22
- TName extends FieldPath < TFieldValues > = FieldPath < TFieldValues >
22
+ TName extends FieldPath < TFieldValues > = FieldPath < TFieldValues > ,
23
23
> = {
24
- name : TName
25
- }
24
+ name : TName ;
25
+ } ;
26
26
27
27
const FormFieldContext = React . createContext < FormFieldContextValue > (
28
- { } as FormFieldContextValue
29
- )
28
+ { } as FormFieldContextValue ,
29
+ ) ;
30
30
31
31
const FormField = <
32
32
TFieldValues extends FieldValues = FieldValues ,
33
- TName extends FieldPath < TFieldValues > = FieldPath < TFieldValues >
33
+ TName extends FieldPath < TFieldValues > = FieldPath < TFieldValues > ,
34
34
> ( {
35
35
...props
36
36
} : ControllerProps < TFieldValues , TName > ) => {
37
37
return (
38
38
< FormFieldContext . Provider value = { { name : props . name } } >
39
39
< Controller { ...props } />
40
40
</ FormFieldContext . Provider >
41
- )
42
- }
41
+ ) ;
42
+ } ;
43
43
44
44
const useFormField = ( ) => {
45
- const fieldContext = React . useContext ( FormFieldContext )
46
- const itemContext = React . useContext ( FormItemContext )
47
- const { getFieldState, formState } = useFormContext ( )
45
+ const fieldContext = React . useContext ( FormFieldContext ) ;
46
+ const itemContext = React . useContext ( FormItemContext ) ;
47
+ const { getFieldState, formState } = useFormContext ( ) ;
48
48
49
- const fieldState = getFieldState ( fieldContext . name , formState )
49
+ const fieldState = getFieldState ( fieldContext . name , formState ) ;
50
50
51
51
if ( ! fieldContext ) {
52
- throw new Error ( "useFormField should be used within <FormField>" )
52
+ throw new Error ( "useFormField should be used within <FormField>" ) ;
53
53
}
54
54
55
- const { id } = itemContext
55
+ const { id } = itemContext ;
56
56
57
57
return {
58
58
id,
@@ -61,53 +61,54 @@ const useFormField = () => {
61
61
formDescriptionId : `${ id } -form-item-description` ,
62
62
formMessageId : `${ id } -form-item-message` ,
63
63
...fieldState ,
64
- }
65
- }
64
+ } ;
65
+ } ;
66
66
67
67
type FormItemContextValue = {
68
- id : string
69
- }
68
+ id : string ;
69
+ } ;
70
70
71
71
const FormItemContext = React . createContext < FormItemContextValue > (
72
- { } as FormItemContextValue
73
- )
72
+ { } as FormItemContextValue ,
73
+ ) ;
74
74
75
75
const FormItem = React . forwardRef <
76
76
HTMLDivElement ,
77
77
React . HTMLAttributes < HTMLDivElement >
78
78
> ( ( { className, ...props } , ref ) => {
79
- const id = React . useId ( )
79
+ const id = React . useId ( ) ;
80
80
81
81
return (
82
82
< FormItemContext . Provider value = { { id } } >
83
83
< div ref = { ref } className = { cn ( "space-y-2" , className ) } { ...props } />
84
84
</ FormItemContext . Provider >
85
- )
86
- } )
87
- FormItem . displayName = "FormItem"
85
+ ) ;
86
+ } ) ;
87
+ FormItem . displayName = "FormItem" ;
88
88
89
89
const FormLabel = React . forwardRef <
90
90
React . ElementRef < typeof LabelPrimitive . Root > ,
91
91
React . ComponentPropsWithoutRef < typeof LabelPrimitive . Root >
92
92
> ( ( { className, ...props } , ref ) => {
93
- const { error, formItemId } = useFormField ( )
93
+ const { error, formItemId } = useFormField ( ) ;
94
94
95
95
return (
96
96
< Label
97
97
ref = { ref }
98
- className = { cn ( error && "text-destructive" , className ) }
98
+ className = { cn ( error && "text-destructive" , className , "text-md" ) }
99
99
htmlFor = { formItemId }
100
100
{ ...props }
101
101
/>
102
- )
103
- } )
104
- FormLabel . displayName = "FormLabel"
102
+ ) ;
103
+ } ) ;
104
+ FormLabel . displayName = "FormLabel" ;
105
105
106
106
const FormControl = React . forwardRef <
107
107
React . ElementRef < typeof Slot > ,
108
108
React . ComponentPropsWithoutRef < typeof Slot >
109
109
> ( ( { ...props } , ref ) => {
110
- const { error, formItemId, formDescriptionId, formMessageId } = useFormField ( )
110
+ const { error, formItemId, formDescriptionId, formMessageId } =
111
+ useFormField ( ) ;
111
112
112
113
return (
113
114
< Slot
@@ -121,15 +122,15 @@ const FormControl = React.forwardRef<
121
122
aria-invalid = { ! ! error }
122
123
{ ...props }
123
124
/>
124
- )
125
- } )
126
- FormControl . displayName = "FormControl"
125
+ ) ;
126
+ } ) ;
127
+ FormControl . displayName = "FormControl" ;
127
128
128
129
const FormDescription = React . forwardRef <
129
130
HTMLParagraphElement ,
130
131
React . HTMLAttributes < HTMLParagraphElement >
131
132
> ( ( { className, ...props } , ref ) => {
132
- const { formDescriptionId } = useFormField ( )
133
+ const { formDescriptionId } = useFormField ( ) ;
133
134
134
135
return (
135
136
< p
@@ -138,19 +139,19 @@ const FormDescription = React.forwardRef<
138
139
className = { cn ( "text-[0.8rem] text-muted-foreground" , className ) }
139
140
{ ...props }
140
141
/>
141
- )
142
- } )
143
- FormDescription . displayName = "FormDescription"
142
+ ) ;
143
+ } ) ;
144
+ FormDescription . displayName = "FormDescription" ;
144
145
145
146
const FormMessage = React . forwardRef <
146
147
HTMLParagraphElement ,
147
148
React . HTMLAttributes < HTMLParagraphElement >
148
149
> ( ( { className, children, ...props } , ref ) => {
149
- const { error, formMessageId } = useFormField ( )
150
- const body = error ? String ( error ?. message ) : children
150
+ const { error, formMessageId } = useFormField ( ) ;
151
+ const body = error ? String ( error ?. message ) : children ;
151
152
152
153
if ( ! body ) {
153
- return null
154
+ return null ;
154
155
}
155
156
156
157
return (
@@ -162,9 +163,9 @@ const FormMessage = React.forwardRef<
162
163
>
163
164
{ body }
164
165
</ p >
165
- )
166
- } )
167
- FormMessage . displayName = "FormMessage"
166
+ ) ;
167
+ } ) ;
168
+ FormMessage . displayName = "FormMessage" ;
168
169
169
170
export {
170
171
useFormField ,
@@ -175,4 +176,4 @@ export {
175
176
FormDescription ,
176
177
FormMessage ,
177
178
FormField ,
178
- }
179
+ } ;
0 commit comments