chore: add visibility toggle to reset password

This commit is contained in:
Mythie
2023-09-20 02:32:06 +00:00
parent ecc8e59c8c
commit 71c7a6ee8c
4 changed files with 61 additions and 22 deletions

View File

@ -37,6 +37,7 @@ 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 [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false);
@ -112,7 +113,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
type="button" type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3" className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showPassword ? 'Mask password' : 'Reveal password'} aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowPassword((showPassword) => !showPassword)} onClick={() => setShowPassword((show) => !show)}
> >
{showPassword ? ( {showPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" /> <EyeOff className="text-muted-foreground h-5 w-5" />
@ -146,7 +147,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
type="button" type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3" className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showConfirmPassword ? 'Mask password' : 'Reveal password'} aria-label={showConfirmPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowConfirmPassword((showConfirmPassword) => !showConfirmPassword)} onClick={() => setShowConfirmPassword((show) => !show)}
> >
{showConfirmPassword ? ( {showConfirmPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" /> <EyeOff className="text-muted-foreground h-5 w-5" />

View File

@ -1,8 +1,11 @@
'use client'; 'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Eye, EyeOff } from 'lucide-react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { z } from 'zod'; import { z } from 'zod';
@ -37,6 +40,9 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
const { toast } = useToast(); const { toast } = useToast();
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const { const {
register, register,
reset, reset,
@ -96,15 +102,31 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
<span>Password</span> <span>Password</span>
</Label> </Label>
<Input <div className="relative">
id="password" <Input
type="password" id="password"
minLength={6} type={showPassword ? 'text' : 'password'}
maxLength={72} minLength={6}
autoComplete="current-password" maxLength={72}
className="bg-background mt-2" autoComplete="new-password"
{...register('password')} className="bg-background mt-2 pr-10"
/> {...register('password')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowPassword((show) => !show)}
>
{showPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" />
) : (
<Eye className="text-muted-foreground h-5 w-5" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.password} /> <FormErrorMessage className="mt-1.5" error={errors.password} />
</div> </div>
@ -114,15 +136,31 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
<span>Repeat Password</span> <span>Repeat Password</span>
</Label> </Label>
<Input <div className="relative">
id="repeatedPassword" <Input
type="password" id="repeated-password"
minLength={6} type={showConfirmPassword ? 'text' : 'password'}
maxLength={72} minLength={6}
autoComplete="current-password" maxLength={72}
className="bg-background mt-2" autoComplete="new-password"
{...register('repeatedPassword')} className="bg-background mt-2 pr-10"
/> {...register('repeatedPassword')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showConfirmPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowConfirmPassword((show) => !show)}
>
{showConfirmPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" />
) : (
<Eye className="text-muted-foreground h-5 w-5" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.repeatedPassword} /> <FormErrorMessage className="mt-1.5" error={errors.repeatedPassword} />
</div> </div>

View File

@ -145,7 +145,7 @@ export const SignInForm = ({ className }: SignInFormProps) => {
type="button" type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3" className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showPassword ? 'Mask password' : 'Reveal password'} aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowPassword((showPassword) => !showPassword)} onClick={() => setShowPassword((show) => !show)}
> >
{showPassword ? ( {showPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" /> <EyeOff className="text-muted-foreground h-5 w-5" />

View File

@ -125,7 +125,7 @@ export const SignUpForm = ({ className }: SignUpFormProps) => {
type="button" type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3" className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showPassword ? 'Mask password' : 'Reveal password'} aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowPassword((showPassword) => !showPassword)} onClick={() => setShowPassword((show) => !show)}
> >
{showPassword ? ( {showPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" /> <EyeOff className="text-muted-foreground h-5 w-5" />