@@ -10,26 +10,26 @@ import {
1010 type UseFormReturn ,
1111 useFormContext ,
1212} from "react-hook-form" ;
13- import { cn } from "../lib/utils.js" ;
14- import { Button , type ButtonProps } from "./shadcn/button.js" ;
15- import { Checkbox as ShadCheckbox } from "./shadcn/checkbox.js" ;
13+ import { cn } from "../../ lib/utils.js" ;
14+ import { Button , type ButtonProps } from ".. /shadcn/button.js" ;
15+ import { Checkbox as ShadCheckbox } from ".. /shadcn/checkbox.js" ;
1616import {
1717 FormControl ,
1818 FormField ,
1919 FormItem ,
2020 FormLabel ,
2121 FormMessage ,
2222 Form as ShadForm ,
23- } from "./shadcn/form.js" ;
24- import { Input , type InputProps } from "./shadcn/input.js" ;
23+ } from ".. /shadcn/form.js" ;
24+ import { Input , type InputProps } from ".. /shadcn/input.js" ;
2525import {
2626 Select ,
2727 SelectContent ,
2828 SelectItem ,
2929 SelectTrigger ,
3030 SelectValue ,
31- } from "./shadcn/select.js" ;
32- import { Textarea as ShadTextarea } from "./shadcn/textarea.js" ;
31+ } from ".. /shadcn/select.js" ;
32+ import { Textarea as ShadTextarea } from ".. /shadcn/textarea.js" ;
3333
3434interface Props < T extends FieldValues >
3535 extends Omit < React . FormHTMLAttributes < HTMLFormElement > , "onSubmit" > {
@@ -86,17 +86,10 @@ function Text<T extends FieldValues>({
8686 control = { control }
8787 name = { name }
8888 render = { ( { field } ) => (
89- < FormItem className = { className } >
89+ < FormItem className = { cn ( "w-full" , className ) } >
9090 < FormLabel > { label } </ FormLabel >
9191 < FormControl >
92- < WithIcon icon = { icon } >
93- < Input
94- { ...rest }
95- { ...field }
96- type = { type }
97- className = { cn ( icon && "pr-10" ) }
98- />
99- </ WithIcon >
92+ < Input { ...rest } { ...field } type = { type } icon = { icon } />
10093 </ FormControl >
10194 < FormMessage > </ FormMessage >
10295 </ FormItem >
@@ -119,12 +112,10 @@ function Textarea<T extends FieldValues>({
119112 control = { control }
120113 name = { name }
121114 render = { ( { field } ) => (
122- < FormItem className = { className } >
115+ < FormItem className = { cn ( "w-full" , className ) } >
123116 < FormLabel > { label } </ FormLabel >
124117 < FormControl >
125- < WithIcon icon = { icon } >
126- < ShadTextarea { ...field } className = { cn ( icon && "pr-10" ) } />
127- </ WithIcon >
118+ < ShadTextarea { ...field } icon = { icon } />
128119 </ FormControl >
129120 < FormMessage > </ FormMessage >
130121 </ FormItem >
@@ -148,21 +139,19 @@ function NumberField<T extends FieldValues>({
148139 control = { control }
149140 name = { name }
150141 render = { ( ) => (
151- < FormItem className = { className } >
142+ < FormItem className = { cn ( "w-full" , className ) } >
152143 < FormLabel > { label } </ FormLabel >
153144 < FormControl >
154- < WithIcon icon = { icon } >
155- { /* TODO: maybe we should use zod's coerce instead? https://github.com/shadcn-ui/ui/issues/421 */ }
156- < Input
157- type = "number"
158- { ...rest }
159- { ...register ( name , {
160- setValueAs : ( value ) =>
161- value === "" ? undefined : Number . parseInt ( value ) ,
162- } ) }
163- className = { cn ( icon && "pr-10" ) }
164- />
165- </ WithIcon >
145+ { /* TODO: maybe we should use zod's coerce instead? https://github.com/shadcn-ui/ui/issues/421 */ }
146+ < Input
147+ type = "number"
148+ { ...rest }
149+ { ...register ( name , {
150+ setValueAs : ( value ) =>
151+ value === "" ? undefined : Number . parseInt ( value ) ,
152+ } ) }
153+ icon = { icon }
154+ />
166155 </ FormControl >
167156 < FormMessage > </ FormMessage >
168157 </ FormItem >
@@ -192,21 +181,20 @@ function BigIntField<T extends FieldValues>({
192181 control = { control }
193182 name = { name }
194183 render = { ( { field } ) => (
195- < FormItem className = { className } >
184+ < FormItem className = { cn ( "w-full" , className ) } >
196185 < FormLabel > { label } </ FormLabel >
197186 < FormControl >
198- < WithIcon icon = { icon } >
199- { /* TODO: maybe we should use zod's coerce instead? https://github.com/shadcn-ui/ui/issues/421 */ }
200- < Input
201- type = "number"
202- { ...rest }
203- { ...field }
204- onChange = { ( e ) =>
205- field . onChange ( BigInt ( e . target . value ) * multiplier )
206- }
207- value = { ( BigInt ( field . value ) / multiplier ) . toString ( ) }
208- />
209- </ WithIcon >
187+ { /* TODO: maybe we should use zod's coerce instead? https://github.com/shadcn-ui/ui/issues/421 */ }
188+ < Input
189+ type = "number"
190+ { ...rest }
191+ { ...field }
192+ onChange = { ( e ) =>
193+ field . onChange ( BigInt ( e . target . value ) * multiplier )
194+ }
195+ value = { ( BigInt ( field . value ) / multiplier ) . toString ( ) }
196+ icon = { icon }
197+ />
210198 </ FormControl >
211199 < FormMessage > </ FormMessage >
212200 </ FormItem >
@@ -216,15 +204,19 @@ function BigIntField<T extends FieldValues>({
216204}
217205Form . BigInt = BigIntField ;
218206
219- function Checkbox < T extends FieldValues > ( { name, label } : BaseInputProps < T > ) {
207+ function Checkbox < T extends FieldValues > ( {
208+ name,
209+ label,
210+ className = "" ,
211+ } : BaseInputProps < T > ) {
220212 const { control } = useFormContext ( ) ;
221213
222214 return (
223215 < FormField
224216 control = { control }
225217 name = { name }
226218 render = { ( { field } ) => (
227- < FormItem className = "flex w-full flex-col" >
219+ < FormItem className = { cn ( "flex w-full flex-col" , className ) } >
228220 < div className = "flex w-full flex-row items-center justify-between space-y-0" >
229221 < FormLabel className = "w-full grow cursor-pointer leading-none" >
230222 { label }
@@ -329,6 +321,7 @@ interface SelectProps<
329321 items : Item [ ] ;
330322 toValue ?: ( v : Item ) => string ;
331323 render ?: ( v : Item ) => React . ReactNode ;
324+ className ?: string ;
332325}
333326
334327function SelectInput <
@@ -342,6 +335,7 @@ function SelectInput<
342335 items,
343336 toValue = ( v ) => v . toString ( ) ,
344337 render = ( v ) => v . toString ( ) ,
338+ className = "" ,
345339} : SelectProps < T , TName , Item > ) {
346340 const { control } = useFormContext ( ) ;
347341
@@ -351,7 +345,9 @@ function SelectInput<
351345 name = { name }
352346 render = { ( { field } ) => {
353347 return (
354- < FormItem className = "flex items-baseline gap-2" >
348+ < FormItem
349+ className = { cn ( "flex w-full items-baseline gap-2" , className ) }
350+ >
355351 < FormLabel className = "shrink-0" > { label } </ FormLabel >
356352 < Select
357353 onValueChange = { field . onChange }
@@ -378,22 +374,3 @@ function SelectInput<
378374 ) ;
379375}
380376Form . Select = SelectInput ;
381-
382- function WithIcon ( {
383- icon,
384- children,
385- } : {
386- icon : React . ReactNode ;
387- children : React . ReactNode ;
388- } ) {
389- return (
390- < div className = "relative w-full" >
391- { children }
392- { icon && (
393- < div className = "pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2" >
394- { icon }
395- </ div >
396- ) }
397- </ div >
398- ) ;
399- }
0 commit comments