1
- 'use client' ;
2
- import { useState } from 'react' ;
1
+ import { CSSProperties , ChangeEvent , useState } from 'react' ;
3
2
import { isRequiredValidator } from '@/helpers/validators' ;
4
3
5
4
type InputProps = {
6
- type : string ;
5
+ type : 'text' | 'number' | 'email' | 'password' ;
7
6
label ?: string ;
8
- id ? : string ;
7
+ id : string ;
9
8
required ?: boolean ;
10
9
placeholder ?: string ;
11
10
value : string ;
12
11
name : string ;
13
- onChange : ( value : string ) => void ;
12
+ onChange : ( e : ChangeEvent < HTMLInputElement > ) => void ;
14
13
} ;
15
14
16
15
export default function Input ( { type, label, id, required, placeholder, value, name, onChange } : InputProps ) {
17
16
const [ errorMessage , setErrorMessage ] = useState < string | undefined > ( undefined ) ;
18
- const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
19
- const { value } = e . target ;
20
- onChange ( value ) ;
21
- } ;
22
17
23
18
const handleBlur = ( ) => {
24
19
if ( required ) {
@@ -28,19 +23,25 @@ export default function Input({ type, label, id, required, placeholder, value, n
28
23
} ;
29
24
30
25
return (
31
- < div >
32
- { label && < label htmlFor = { id } > { label } </ label > }
26
+ < div className = "relative flex flex-col" >
27
+ { label && (
28
+ < label className = { `mb-1 ${ errorMessage && 'text-red-800' } ` } htmlFor = { id } >
29
+ { `${ label } *` }
30
+ </ label >
31
+ ) }
33
32
< input
34
33
className = "border-[3px] border-solid rounded-lg border-gray-1 py-2 px-5 text-white bg-gray-2 outline-none focus:border-secondary focus-visible:border-secondary active:border-secondary"
34
+ style = { errorMessage ? ( { borderColor : 'red' } as CSSProperties ) : undefined }
35
35
type = { type }
36
36
value = { value }
37
37
name = { name }
38
+ id = { id }
38
39
placeholder = { placeholder }
39
- onChange = { handleChange }
40
+ onChange = { onChange }
40
41
onBlur = { handleBlur }
41
42
required = { required }
42
43
/>
43
- { errorMessage && < span > { errorMessage } </ span > }
44
+ < span className = "block min-h-[25px] text-red-800" > { errorMessage } </ span >
44
45
</ div >
45
46
) ;
46
47
}
0 commit comments