diff --git a/client/src/components/ui/TextInput/index.tsx b/client/src/components/ui/TextInput/index.tsx index 0fef646ae291f711ff9824df115ae2facfab3ea5..af6519929dc7379094fa87c421361ffbe51f7053 100644 --- a/client/src/components/ui/TextInput/index.tsx +++ b/client/src/components/ui/TextInput/index.tsx @@ -1,4 +1,4 @@ -import React, { ChangeEvent, Dispatch } from "react"; +import React, { ChangeEvent, Dispatch, FocusEvent, useState } from "react"; import './text-input.scss'; @@ -7,17 +7,28 @@ interface Props { name: string, color?: 'dark' type?: 'password' | 'textarea' | 'text', - valueSetter?: Dispatch<string>, - error?: string + onChange: Dispatch<string>, + validateFn: (arg0: string) => string | null; } -export default function TextInput({ label, name, type, color, valueSetter, error }: Props) { +export default function TextInput({ label, name, type, color, onChange, validateFn }: Props) { + const [error, setError] = useState(''); type = type ?? 'text'; const setValue = (e: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>): void => { - if (valueSetter) - valueSetter(e.target.value); + onChange(e.target.value); + } + + const validateField = (e: FocusEvent<HTMLTextAreaElement | HTMLInputElement>): void => { + if (validateFn) { + let error = validateFn(e.target.value); + + if (error) + setError(error); + else + setError(''); + } } const errorTag = error ? (<div className="error">{error}</div>) : null; @@ -28,8 +39,8 @@ export default function TextInput({ label, name, type, color, valueSetter, error <label htmlFor={name}>{label}</label> { type === 'textarea' ? - (<textarea onChange={(e) => setValue(e)} name={name} id={name}></textarea>) - : (<input onChange={(e) => setValue(e)} type={type} name={name} id={name} />) + (<textarea onChange={setValue} name={name} id={name} onBlur={validateField}></textarea>) + : (<input onChange={setValue} type={type} name={name} id={name} onBlur={validateField} />) } </div > {errorTag} diff --git a/client/src/components/ui/TextInput/text-input.scss b/client/src/components/ui/TextInput/text-input.scss index bdfb8fd46a135c85c78247409501d75e6b8ba154..772b9c27aaa97bd58018eb353fa422ccd145c8c5 100644 --- a/client/src/components/ui/TextInput/text-input.scss +++ b/client/src/components/ui/TextInput/text-input.scss @@ -6,7 +6,8 @@ .error { color: s.$error-color; - margin-top: 5px; + margin-top: 10px; + padding-left: 15px; } .input-field { diff --git a/client/src/pages/Home/index.tsx b/client/src/pages/Home/index.tsx index 7067e9b756b43c3335793498a404ae17f3484923..24b23ef782914caca37a7ac0f3ece51adf4b5c00 100644 --- a/client/src/pages/Home/index.tsx +++ b/client/src/pages/Home/index.tsx @@ -1,7 +1,7 @@ import Project from 'components/ui/Project'; import ButtonLink from 'components/ui/ButtonLink'; import Button from 'components/ui/Button'; -import TextInput from 'components/ui/TextInput'; +//import TextInput from 'components/ui/TextInput'; import Page from 'components/ui/Page'; import './home.scss'; @@ -150,11 +150,11 @@ export default function Home() { to help you resolve the issue. </p> <form className="contact-form" action="mailto:dplanoetscher@unibz.it" method="GET"> - <TextInput label="Fistname" name="firstname" /> + {/* <TextInput label="Fistname" name="firstname" /> <TextInput label="Lastname" name="lastname" /> <TextInput label="Email" name="email" /> <TextInput label="Subject" name="subject" /> - <TextInput label="Message" name="message" type="textarea" /> + <TextInput label="Message" name="message" type="textarea" />*/} <div className="button-container"> <Button type="submit">Send</Button> </div> diff --git a/client/src/pages/Register/index.tsx b/client/src/pages/Register/index.tsx index 5f38639252dfe4641d15741773d0e42140b122f2..08781570f5653144d676dc76b89da477c085fddf 100644 --- a/client/src/pages/Register/index.tsx +++ b/client/src/pages/Register/index.tsx @@ -7,20 +7,17 @@ import { registerUser } from 'adapters/api'; import './register.scss'; function usernameIsValid(username: string) { - return (username && username.length > 3); + return (username && username.length > 3) ? null : 'Username has to be at least 4 characters long.'; } function passwordIsValid(password: string) { - return (password && password.length > 5); + return (password && password.length > 5) ? null : 'Password has to be at least 6 characters long'; } export default function Register() { const [username, setUsername] = useState<string>(''); - const [usernameError, setUsernameError] = useState<string>(''); - const [password, setPassword] = useState<string>(''); - const [passwordError, setPasswordError] = useState<string>(''); - + const history = useHistory(); @@ -28,28 +25,11 @@ export default function Register() { e.preventDefault(); if (usernameIsValid(username) && passwordIsValid(password)) { - setPasswordError(''); - setUsernameError(''); - - await registerUser(username, password).then((data) => { - console.log(data); - history.push('/tasks'); }).catch(() => { - setUsernameError('This username is already used.'); }); - } else { - if (!usernameIsValid(username)) - setUsernameError('Your username has to be at least 4 characters long'); - else - setUsernameError(''); - - if (!passwordIsValid(password)) - setPasswordError('Your password has to be at least 6 characters long'); - else - setPasswordError(''); } } @@ -62,16 +42,16 @@ export default function Register() { label="Username" name="username" color="dark" - valueSetter={setUsername} - error={usernameError} + onChange={setUsername} + validateFn={usernameIsValid} /> <TextInput label="Password" name="password" color="dark" type="password" - valueSetter={setPassword} - error={passwordError} + onChange={setPassword} + validateFn={passwordIsValid} /> <Button type="submit"> Register now