feat: added show password feature

Signed-off-by: Adithya Krishna <adikrish@redhat.com>
This commit is contained in:
Adithya Krishna
2023-08-26 12:31:40 +05:30
parent 10cd8144eb
commit 9c45ce61b8
3 changed files with 117 additions and 27 deletions

View File

@ -1,7 +1,9 @@
'use client'; 'use client';
import { useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Loader } from 'lucide-react'; import { Eye, EyeOff, Loader } from 'lucide-react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { z } from 'zod'; import { z } from 'zod';
@ -35,6 +37,8 @@ export type PasswordFormProps = {
export const PasswordForm = ({ className }: PasswordFormProps) => { export const PasswordForm = ({ className }: PasswordFormProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const { const {
register, register,
@ -79,6 +83,14 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
} }
}; };
const onShowPassword = () => {
setShowPassword(!showPassword);
};
const onShowConfirmPassword = () => {
setShowConfirmPassword(!showConfirmPassword);
};
return ( return (
<form <form
className={cn('flex w-full flex-col gap-y-4', className)} className={cn('flex w-full flex-col gap-y-4', className)}
@ -89,12 +101,28 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
Password Password
</Label> </Label>
<Input <div className="relative">
id="password" <Input
type="password" id="password"
className="bg-background mt-2" type={showPassword ? 'text' : 'password'}
{...register('password')} className="bg-background mt-2"
/> {...register('password')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center"
aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={onShowPassword}
>
{showPassword ? (
<EyeOff className="text-slate-500" />
) : (
<Eye className="text-slate-500" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.password} /> <FormErrorMessage className="mt-1.5" error={errors.password} />
</div> </div>
@ -104,12 +132,28 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
Repeat Password Repeat Password
</Label> </Label>
<Input <div className="relative">
id="repeated-password" <Input
type="password" id="repeated-password"
className="bg-background mt-2" type={showConfirmPassword ? 'text' : 'password'}
{...register('repeatedPassword')} className="bg-background mt-2"
/> {...register('repeatedPassword')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center"
aria-label={showConfirmPassword ? 'Mask password' : 'Reveal password'}
onClick={onShowConfirmPassword}
>
{showConfirmPassword ? (
<EyeOff className="text-slate-500" />
) : (
<Eye className="text-slate-500" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.repeatedPassword} /> <FormErrorMessage className="mt-1.5" error={errors.repeatedPassword} />
</div> </div>

View File

@ -1,7 +1,9 @@
'use client'; 'use client';
import { useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Loader } from 'lucide-react'; import { Eye, EyeOff, Loader } from 'lucide-react';
import { signIn } from 'next-auth/react'; import { signIn } from 'next-auth/react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { FcGoogle } from 'react-icons/fc'; import { FcGoogle } from 'react-icons/fc';
@ -26,6 +28,7 @@ export type SignInFormProps = {
export const SignInForm = ({ className }: SignInFormProps) => { export const SignInForm = ({ className }: SignInFormProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [showPassword, setShowPassword] = useState(false);
const { const {
register, register,
@ -73,6 +76,10 @@ export const SignInForm = ({ className }: SignInFormProps) => {
} }
}; };
const onShowPassword = () => {
setShowPassword(!showPassword);
};
return ( return (
<form <form
className={cn('flex w-full flex-col gap-y-4', className)} className={cn('flex w-full flex-col gap-y-4', className)}
@ -96,12 +103,28 @@ export const SignInForm = ({ className }: SignInFormProps) => {
Password Password
</Label> </Label>
<Input <div className="relative">
id="password" <Input
type="password" id="password"
className="bg-background mt-2" type={showPassword ? 'text' : 'password'}
{...register('password')} className="bg-background mt-2"
/> {...register('password')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center"
aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={onShowPassword}
>
{showPassword ? (
<EyeOff className="text-slate-500" />
) : (
<Eye className="text-slate-500" />
)}
</Button>
</div>
{errors.password && ( {errors.password && (
<span className="mt-1 text-xs text-red-500">{errors.password.message}</span> <span className="mt-1 text-xs text-red-500">{errors.password.message}</span>

View File

@ -1,7 +1,9 @@
'use client'; 'use client';
import { useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Loader } from 'lucide-react'; import { Eye, EyeOff, Loader } from 'lucide-react';
import { signIn } from 'next-auth/react'; import { signIn } from 'next-auth/react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { z } from 'zod'; import { z } from 'zod';
@ -29,6 +31,7 @@ export type SignUpFormProps = {
export const SignUpForm = ({ className }: SignUpFormProps) => { export const SignUpForm = ({ className }: SignUpFormProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [showPassword, setShowPassword] = useState(false);
const { const {
register, register,
@ -72,6 +75,10 @@ export const SignUpForm = ({ className }: SignUpFormProps) => {
} }
}; };
const onShowPassword = () => {
setShowPassword(!showPassword);
};
return ( return (
<form <form
className={cn('flex w-full flex-col gap-y-4', className)} className={cn('flex w-full flex-col gap-y-4', className)}
@ -102,12 +109,28 @@ export const SignUpForm = ({ className }: SignUpFormProps) => {
Password Password
</Label> </Label>
<Input <div className="relative">
id="password" <Input
type="password" id="password"
className="bg-background mt-2" type={showPassword ? 'text' : 'password'}
{...register('password')} className="bg-background mt-2"
/> {...register('password')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center"
aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={onShowPassword}
>
{showPassword ? (
<EyeOff className="text-slate-500" />
) : (
<Eye className="text-slate-500" />
)}
</Button>
</div>
</div> </div>
<div> <div>