2023-10-23 23:33:39 +02:00
|
|
|
import { useState } from "react"
|
|
|
|
import Link from "next/link"
|
|
|
|
import useTranslation from "next-translate/useTranslation"
|
|
|
|
import classNames from "clsx"
|
2022-01-07 21:21:38 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { FormState } from "../FormState"
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
export interface InputProps extends React.ComponentPropsWithRef<"input"> {
|
2021-10-24 06:09:43 +02:00
|
|
|
label: string
|
|
|
|
error?: string | null
|
|
|
|
showForgotPassword?: boolean
|
2022-01-14 23:15:51 +01:00
|
|
|
className?: string
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getInputType = (inputType: string): string => {
|
2023-10-23 23:33:39 +02:00
|
|
|
return inputType === "password" ? "text" : "password"
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Input: React.FC<InputProps> = (props) => {
|
|
|
|
const {
|
|
|
|
label,
|
|
|
|
name,
|
2022-01-14 23:15:51 +01:00
|
|
|
className,
|
2023-10-23 23:33:39 +02:00
|
|
|
type = "text",
|
2021-10-24 06:09:43 +02:00
|
|
|
showForgotPassword = false,
|
|
|
|
error,
|
|
|
|
...rest
|
|
|
|
} = props
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const [inputType, setInputType] = useState(type)
|
|
|
|
|
|
|
|
const handlePassword = (): void => {
|
|
|
|
const oppositeType = getInputType(inputType)
|
|
|
|
setInputType(oppositeType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex flex-col">
|
|
|
|
<div className={classNames("mb-2 mt-6 flex justify-between", className)}>
|
|
|
|
<label className="pl-1" htmlFor={name}>
|
2022-12-13 22:31:32 +01:00
|
|
|
{label}
|
|
|
|
</label>
|
2023-10-23 23:33:39 +02:00
|
|
|
{type === "password" && showForgotPassword ? (
|
2022-12-13 22:31:32 +01:00
|
|
|
<Link
|
2023-10-23 23:33:39 +02:00
|
|
|
href="/authentication/forgot-password"
|
|
|
|
className="text-center font-headline text-xs text-green-800 hover:underline dark:text-green-400 sm:text-sm"
|
|
|
|
data-cy="forgot-password-link"
|
2022-12-13 22:31:32 +01:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{t("authentication:forgot-password")}
|
2022-12-13 22:31:32 +01:00
|
|
|
</Link>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="relative mt-0">
|
2022-12-13 22:31:32 +01:00
|
|
|
<input
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy={`input-${name ?? "name"}`}
|
|
|
|
className="h-11 w-full rounded-lg border border-transparent bg-[#f1f1f1] px-3 font-paragraph leading-10 text-[#2a2a2a] caret-green-600 focus:border focus:shadow-green focus:outline-none"
|
2022-12-13 22:31:32 +01:00
|
|
|
{...rest}
|
|
|
|
id={name}
|
|
|
|
name={name}
|
|
|
|
type={inputType}
|
|
|
|
/>
|
2023-10-23 23:33:39 +02:00
|
|
|
{type === "password" && (
|
2022-12-13 22:31:32 +01:00
|
|
|
<div
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="password-eye"
|
2022-12-13 22:31:32 +01:00
|
|
|
onClick={handlePassword}
|
|
|
|
style={{
|
2023-10-23 23:33:39 +02:00
|
|
|
backgroundImage: `url('/images/svg/icons/input/${inputType}.svg')`,
|
2022-12-13 22:31:32 +01:00
|
|
|
}}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="absolute right-4 top-3 z-10 h-5 w-5 cursor-pointer bg-[#f1f1f1] bg-cover"
|
2021-10-24 06:09:43 +02:00
|
|
|
/>
|
2022-12-13 22:31:32 +01:00
|
|
|
)}
|
|
|
|
<FormState
|
2023-10-23 23:33:39 +02:00
|
|
|
id={`error-${name ?? "input"}`}
|
|
|
|
state={error == null ? "idle" : "error"}
|
2022-12-13 22:31:32 +01:00
|
|
|
message={error}
|
|
|
|
/>
|
2021-10-24 06:09:43 +02:00
|
|
|
</div>
|
2022-12-13 22:31:32 +01:00
|
|
|
</div>
|
2021-10-24 06:09:43 +02:00
|
|
|
)
|
|
|
|
}
|