Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 51c6520

Browse files
author
Manuel Proß
committed
feat(FE): make input type union, make id required prop, add ChangeEvent, add styling for error state of input
1 parent 46d74e3 commit 51c6520

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
'use client';
2-
import { useState } from 'react';
1+
import { CSSProperties, ChangeEvent, useState } from 'react';
32
import { isRequiredValidator } from '@/helpers/validators';
43

54
type InputProps = {
6-
type: string;
5+
type: 'text' | 'number' | 'email' | 'password';
76
label?: string;
8-
id?: string;
7+
id: string;
98
required?: boolean;
109
placeholder?: string;
1110
value: string;
1211
name: string;
13-
onChange: (value: string) => void;
12+
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
1413
};
1514

1615
export default function Input({ type, label, id, required, placeholder, value, name, onChange }: InputProps) {
1716
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
18-
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
19-
const { value } = e.target;
20-
onChange(value);
21-
};
2217

2318
const handleBlur = () => {
2419
if (required) {
@@ -28,19 +23,25 @@ export default function Input({ type, label, id, required, placeholder, value, n
2823
};
2924

3025
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+
)}
3332
<input
3433
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}
3535
type={type}
3636
value={value}
3737
name={name}
38+
id={id}
3839
placeholder={placeholder}
39-
onChange={handleChange}
40+
onChange={onChange}
4041
onBlur={handleBlur}
4142
required={required}
4243
/>
43-
{errorMessage && <span>{errorMessage}</span>}
44+
<span className="block min-h-[25px] text-red-800">{errorMessage}</span>
4445
</div>
4546
);
4647
}

0 commit comments

Comments
 (0)