2021-10-24 06:09:43 +02:00
|
|
|
import { useState } from 'react'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
2022-01-14 23:15:51 +01:00
|
|
|
import classNames from 'classnames'
|
2022-01-07 21:21:38 +01:00
|
|
|
|
2021-10-24 06:09:43 +02:00
|
|
|
import { FormState } from '../FormState'
|
|
|
|
|
|
|
|
export interface InputProps extends React.ComponentPropsWithRef<'input'> {
|
|
|
|
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 => {
|
|
|
|
return inputType === 'password' ? 'text' : 'password'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Input: React.FC<InputProps> = (props) => {
|
|
|
|
const {
|
|
|
|
label,
|
|
|
|
name,
|
2022-01-14 23:15:51 +01:00
|
|
|
className,
|
2021-10-24 06:09:43 +02:00
|
|
|
type = 'text',
|
|
|
|
showForgotPassword = false,
|
|
|
|
error,
|
|
|
|
...rest
|
|
|
|
} = props
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const [inputType, setInputType] = useState(type)
|
|
|
|
|
|
|
|
const handlePassword = (): void => {
|
|
|
|
const oppositeType = getInputType(inputType)
|
|
|
|
setInputType(oppositeType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className='flex flex-col'>
|
2022-01-14 23:15:51 +01:00
|
|
|
<div
|
2022-02-19 23:20:33 +01:00
|
|
|
className={classNames('mt-6 mb-2 flex justify-between', className)}
|
2022-01-14 23:15:51 +01:00
|
|
|
>
|
2021-10-24 06:09:43 +02:00
|
|
|
<label className='pl-1' htmlFor={name}>
|
|
|
|
{label}
|
|
|
|
</label>
|
|
|
|
{type === 'password' && showForgotPassword ? (
|
|
|
|
<Link href='/authentication/forgot-password'>
|
|
|
|
<a
|
2022-02-19 23:20:33 +01:00
|
|
|
className='text-center font-headline text-xs text-green-800 hover:underline dark:text-green-400 sm:text-sm'
|
2022-08-23 21:51:20 +02:00
|
|
|
data-cy='forgot-password-link'
|
2021-10-24 06:09:43 +02:00
|
|
|
>
|
|
|
|
{t('authentication:forgot-password')}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2022-02-19 23:20:33 +01:00
|
|
|
<div className='relative mt-0'>
|
2021-10-24 06:09:43 +02:00
|
|
|
<input
|
|
|
|
data-cy={`input-${name ?? 'name'}`}
|
2022-02-19 23:20:33 +01:00
|
|
|
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'
|
2021-10-24 06:09:43 +02:00
|
|
|
{...rest}
|
|
|
|
id={name}
|
|
|
|
name={name}
|
|
|
|
type={inputType}
|
|
|
|
/>
|
|
|
|
{type === 'password' && (
|
|
|
|
<div
|
2022-08-23 21:51:20 +02:00
|
|
|
data-cy='password-eye'
|
2021-10-24 06:09:43 +02:00
|
|
|
onClick={handlePassword}
|
2022-02-19 23:20:33 +01:00
|
|
|
className='password-eye absolute cursor-pointer bg-[#f1f1f1] bg-cover'
|
2021-10-24 06:09:43 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<FormState
|
|
|
|
id={`error-${name ?? 'input'}`}
|
|
|
|
state={error == null ? 'idle' : 'error'}
|
|
|
|
message={error}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style jsx>
|
|
|
|
{`
|
|
|
|
.password-eye {
|
|
|
|
top: 12px;
|
|
|
|
right: 16px;
|
|
|
|
z-index: 1;
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
background-image: url('/images/svg/icons/input/${inputType}.svg');
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
</style>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|