1+ // biome-ignore lint/style/noNamespaceImport: prevents React undefined errors when exporting as a component library
12import * as React from 'react' ;
23import type { Control , FieldPath , FieldValues } from 'react-hook-form' ;
34import {
@@ -10,10 +11,23 @@ import {
1011 FormMessage ,
1112} from './form' ;
1213import { TextInput } from './text-input' ;
14+ import { cn } from './utils' ;
1315
14- export interface TextFieldComponents extends FieldComponents {
15- Input ?: React . ComponentType < React . InputHTMLAttributes < HTMLInputElement > > ;
16- }
16+ export const FieldPrefix = ( { children, className } : { children : React . ReactNode ; className ?: string } ) => {
17+ return (
18+ < span className = { cn ( "whitespace-nowrap shadow-sm font-bold rounded-md text-base flex items-center px-2.5 pr-5 -mr-2.5 bg-gray-50 text-gray-500" , className ) } >
19+ { children }
20+ </ span >
21+ ) ;
22+ } ;
23+
24+ export const FieldSuffix = ( { children, className } : { children : React . ReactNode ; className ?: string } ) => {
25+ return (
26+ < span className = { cn ( "whitespace-nowrap shadow-sm font-bold rounded-md text-base flex items-center px-2.5 pl-5 -ml-2.5 bg-gray-50 text-gray-500" , className ) } >
27+ { children }
28+ </ span >
29+ ) ;
30+ } ;
1731
1832export interface TextFieldProps <
1933 TFieldValues extends FieldValues = FieldValues ,
@@ -23,13 +37,13 @@ export interface TextFieldProps<
2337 name : TName ;
2438 label ?: string ;
2539 description ?: string ;
26- components ?: Partial < TextFieldComponents > ;
40+ components ?: Partial < FieldComponents > ;
41+ prefix ?: React . ReactNode ;
42+ suffix ?: React . ReactNode ;
2743}
2844
2945export const TextField = React . forwardRef < HTMLDivElement , TextFieldProps > (
30- ( { control, name, label, description, className, components, ...props } , ref ) => {
31- const InputComponent = components ?. Input || TextInput ;
32-
46+ ( { control, name, label, description, className, components, prefix, suffix, ...props } , ref ) => {
3347 return (
3448 < FormField
3549 control = { control }
@@ -38,7 +52,23 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
3852 < FormItem className = { className } ref = { ref } >
3953 { label && < FormLabel Component = { components ?. FormLabel } > { label } </ FormLabel > }
4054 < FormControl Component = { components ?. FormControl } >
41- < InputComponent { ...field } { ...props } ref = { field . ref } />
55+ < div className = { cn ( "flex items-stretch relative" , {
56+ "field__input--with-prefix" : prefix ,
57+ "field__input--with-suffix" : suffix ,
58+ } ) } >
59+ { prefix && < FieldPrefix > { prefix } </ FieldPrefix > }
60+ < TextInput
61+ { ...field }
62+ { ...props }
63+ ref = { field . ref }
64+ className = { cn ( props . className , {
65+ "z-10" : prefix || suffix ,
66+ "rounded-l-none" : prefix ,
67+ "rounded-r-none" : suffix ,
68+ } ) }
69+ />
70+ { suffix && < FieldSuffix > { suffix } </ FieldSuffix > }
71+ </ div >
4272 </ FormControl >
4373 { description && < FormDescription Component = { components ?. FormDescription } > { description } </ FormDescription > }
4474 { fieldState . error && (
0 commit comments