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

Commit e269b0a

Browse files
author
Manuel Proß
committed
wip(FE): add errorMessage state, add required validaton on blur
1 parent 962ef15 commit e269b0a

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
1+
'use client';
2+
import { useState } from 'react';
3+
import { isRequiredValidator } from '@/helpers/validators';
4+
15
type InputProps = {
26
type: string;
37
label?: string;
48
id?: string;
59
required?: boolean;
6-
hasError?: boolean;
710
placeholder?: string;
811
value: string;
912
name: string;
1013
onChange: (value: string) => void;
1114
};
1215

13-
export default function Input({ type, label, id, required, hasError, placeholder, value, name, onChange }: InputProps) {
16+
export default function Input({ type, label, id, required, placeholder, value, name, onChange }: InputProps) {
17+
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
1418
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
1519
const { value } = e.target;
1620
onChange(value);
1721
};
22+
23+
const handleBlur = () => {
24+
if (required) {
25+
const error = isRequiredValidator(value);
26+
setErrorMessage(error);
27+
}
28+
};
29+
1830
return (
1931
<div>
2032
{label && <label htmlFor={id}>{label}</label>}
2133
<input
22-
className="border-[1px] border-solid border-gray-1"
34+
className="border-[1px] border-solid rounded-lg border-gray-1 py-2 px-4 text-white bg-gray-2"
2335
type={type}
2436
value={value}
2537
name={name}
2638
placeholder={placeholder}
2739
onChange={handleChange}
40+
onBlur={handleBlur}
2841
required={required}
2942
/>
30-
{hasError}
43+
{errorMessage && <span>{errorMessage}</span>}
3144
</div>
3245
);
3346
}

0 commit comments

Comments
 (0)