-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPasswordInput.jsx
More file actions
73 lines (68 loc) · 2.14 KB
/
PasswordInput.jsx
File metadata and controls
73 lines (68 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { HiEye, HiEyeOff } from 'react-icons/hi';
import { classNames } from '@/lib/helper';
export default function PasswordInput({
label,
placeholder = '',
helperText = '',
id,
readOnly = false,
validation,
...rest
}) {
const {
register,
formState: { errors },
} = useFormContext();
const [showPassword, setShowPassword] = useState(false);
const togglePassword = () => setShowPassword((prev) => !prev);
return (
<div>
<label htmlFor={id} className='block text-sm font-normal text-gray-700'>
{label}
</label>
<div className='relative mt-1'>
<input
{...register(id, validation)}
{...rest}
type={showPassword ? 'text' : 'password'}
name={id}
id={id}
readOnly={readOnly}
className={classNames(
readOnly
? 'bg-gray-100 focus:ring-0 cursor-not-allowed border-gray-300 focus:border-gray-300'
: errors[id]
? 'focus:ring-red-500 border-red-500 focus:border-red-500'
: 'focus:ring-primary-500 border-gray-300 focus:border-primary-500',
'block w-full rounded-md shadow-sm'
)}
placeholder={placeholder}
aria-describedby={id}
/>
<button
onClick={(e) => {
e.preventDefault();
togglePassword();
}}
className='absolute inset-y-0 right-0 flex items-center p-1 mr-3 rounded-lg focus:outline-none focus:ring focus:ring-primary-500'
>
{showPassword ? (
<HiEyeOff className='text-xl text-gray-500 cursor-pointer hover:text-gray-600' />
) : (
<HiEye className='text-xl text-gray-500 cursor-pointer hover:text-gray-600' />
)}
</button>
</div>
<div className='mt-1'>
{helperText !== '' && (
<p className='text-xs text-gray-500'>{helperText}</p>
)}
{errors[id] && (
<span className='text-sm text-red-500'>{errors[id].message}</span>
)}
</div>
</div>
);
}