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

Commit 87bd4b1

Browse files
author
Manuel Proß
committed
feat(FE): add validation prop to provide custom error messages based on validation
1 parent 60090a0 commit 87bd4b1

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

web/src/components/Inputs/TextInput.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { CSSProperties, ChangeEvent, useState } from 'react';
2-
import { isRequiredValidator } from '@/helpers/validators';
32

43
type InputProps = {
54
type: 'text' | 'number' | 'email' | 'password';
65
label?: string;
76
id: string;
87
required?: boolean;
8+
validate?: (value: string) => string | undefined;
99
placeholder?: string;
1010
value: string;
1111
name: string;
1212
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
1313
};
1414

15-
export default function Input({ type, label, id, required, placeholder, value, name, onChange }: InputProps) {
15+
export default function Input({ type, label, id, required, validate, placeholder, value, name, onChange }: InputProps) {
1616
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
1717

1818
const handleBlur = () => {
1919
if (required) {
20-
const error = isRequiredValidator(value);
20+
const error = value ? undefined : 'Required';
2121
setErrorMessage(error);
22+
if (error) return;
23+
}
24+
25+
if (validate) {
26+
setErrorMessage(validate(value));
2227
}
2328
};
2429

0 commit comments

Comments
 (0)