1- import React , { useRef } from "react" ;
1+ import React from "react" ;
22import Label from "@/common/Label" ;
33import type { LabelProps } from "@/common/Label" ;
44import Input from "@/common/Input" ;
@@ -17,70 +17,75 @@ export interface FormFieldProps {
1717 errorTextClassName ?: string ;
1818}
1919
20- const FormField : React . FC < FormFieldProps > = ( {
21- labelProps ,
22- inputProps ,
23- error ,
24- className ,
25- inputIcon ,
26- isParentFocused ,
27- inputWrapperClassName ,
28- errorTextClassName ,
29- } ) => {
30- const { id : inputId , className : inputSpecificClassName } = inputProps ;
31- const internalInputRef = useRef < HTMLInputElement > ( null ) ;
20+ const FormField = React . forwardRef < HTMLInputElement , FormFieldProps > (
21+ ( {
22+ labelProps ,
23+ inputProps ,
24+ error ,
25+ className ,
26+ inputIcon ,
27+ isParentFocused ,
28+ inputWrapperClassName ,
29+ errorTextClassName ,
30+ } ) => {
31+ const { id : inputId , className : inputSpecificClassName } = inputProps ;
3232
33- let inputCombinedClassName = inputSpecificClassName || "" ;
34- const errorRingStyles = "border-error focus:ring-error focus:border-error" ;
33+ const { ref : refForInputElement , ...remainingInputProps } = inputProps ;
3534
36- let finalInputProps = { ... inputProps } ;
37- delete finalInputProps . className ;
35+ let inputCombinedClassName = inputSpecificClassName || "" ;
36+ const errorRingStyles = "border-error focus:ring-error focus:border-error" ;
3837
39- if ( inputIcon ) {
40- inputCombinedClassName += " pr-16" ;
41- }
38+ let finalInputProps = { ...remainingInputProps } ;
39+ delete finalInputProps . className ;
4240
43- if ( error ) {
44- inputCombinedClassName += ` ${ errorRingStyles } ` ;
45- } else if ( isParentFocused ) {
46- finalInputProps . forceFocusStyle = true ;
47- }
41+ if ( inputIcon ) {
42+ inputCombinedClassName += " pr-16" ;
43+ }
4844
49- return (
50- < div className = { `${ className || "" } ` } >
51- < div
52- className = { `relative mt-1 rounded-md shadow-sm ${ inputWrapperClassName || "" } ` }
53- >
54- < div className = "relative w-full" >
55- < Input
56- { ...finalInputProps }
57- ref = { internalInputRef }
58- className = { inputCombinedClassName }
59- aria-invalid = { ! ! error }
60- aria-describedby = { error ? `${ inputId } -error` : undefined }
61- />
62- < Label { ...labelProps } htmlFor = { inputId } isError = { ! ! error } />
63- { inputIcon && (
64- < div
65- className = { `absolute inset-y-0 right-0 flex items-center rounded-r-md mt-px mt-3 ${ isParentFocused ? "bg-primary/15" : "" } ` }
66- >
67- { inputIcon }
68- </ div >
69- ) }
70- </ div >
71- </ div >
72- { error && (
45+ if ( error ) {
46+ inputCombinedClassName += ` ${ errorRingStyles } ` ;
47+ } else if ( isParentFocused ) {
48+ finalInputProps . forceFocusStyle = true ;
49+ }
50+
51+ return (
52+ < div className = { `${ className || "" } ` } >
7353 < div
74- id = { `${ inputId } -error` }
75- className = { `flex items-center mt-2 text-sm text-error ${ errorTextClassName || "" } ` }
76- role = "alert"
54+ className = { `relative mt-1 rounded-md shadow-sm ${ inputWrapperClassName || "" } ` }
7755 >
78- < Icon As = { ExclamationCircleIcon } className = "h-4 w-4 mr-1" />
79- { error }
56+ < div className = "relative w-full" >
57+ < Input
58+ { ...finalInputProps }
59+ ref = { refForInputElement }
60+ className = { inputCombinedClassName }
61+ aria-invalid = { ! ! error }
62+ aria-describedby = { error ? `${ inputId } -error` : undefined }
63+ />
64+ < Label { ...labelProps } htmlFor = { inputId } isError = { ! ! error } />
65+ { inputIcon && (
66+ < div
67+ className = { `absolute inset-y-0 right-0 flex items-center rounded-r-md mt-px mt-3 ${ isParentFocused ? "bg-primary/15" : "" } ` }
68+ >
69+ { inputIcon }
70+ </ div >
71+ ) }
72+ </ div >
8073 </ div >
81- ) }
82- </ div >
83- ) ;
84- } ;
74+ { error && (
75+ < div
76+ id = { `${ inputId } -error` }
77+ className = { `flex items-center mt-2 text-sm text-error ${ errorTextClassName || "" } ` }
78+ role = "alert"
79+ >
80+ < Icon As = { ExclamationCircleIcon } className = "h-4 w-4 mr-1" />
81+ { error }
82+ </ div >
83+ ) }
84+ </ div >
85+ ) ;
86+ } ,
87+ ) ;
88+
89+ FormField . displayName = "FormField" ;
8590
8691export default FormField ;
0 commit comments