From f7ef6f800cf662dfee757b8201926808e50fe29b Mon Sep 17 00:00:00 2001 From: Anish Date: Mon, 19 Feb 2024 10:39:10 +0500 Subject: [PATCH] [Feature]: 2216 add reset button in code enterence on auth pages (#2218) * feat(Auth): added the reset button and fix the padding of inputs * feat(Auth): added the reset button and fix the padding of inputs * feat(Auth): added the reset button and fix the padding of inputs * feat(Auth): added the reset button and fix the padding of inputs * feat(Auth): added the reset button and fix the padding of inputs * feat(Auth): added the reset button and fix the padding of inputs --- apps/web/app/[locale]/auth/passcode/page.tsx | 27 ++++++++++++++++--- .../lib/components/inputs/auth-code-input.tsx | 10 ++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/web/app/[locale]/auth/passcode/page.tsx b/apps/web/app/[locale]/auth/passcode/page.tsx index 5e59954e2..43b673bc2 100644 --- a/apps/web/app/[locale]/auth/passcode/page.tsx +++ b/apps/web/app/[locale]/auth/passcode/page.tsx @@ -10,7 +10,7 @@ import { AuthLayout } from 'lib/layout'; import { useTranslations } from 'next-intl'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; -import { FormEvent, useCallback, useEffect, useState } from 'react'; +import { FormEvent, useCallback, useEffect, useRef, useState } from 'react'; import stc from 'string-to-color'; @@ -122,7 +122,15 @@ function EmailScreen({ form, className }: { form: TAuthenticationPasscode } & IC function PasscodeScreen({ form, className }: { form: TAuthenticationPasscode } & IClassName) { const t = useTranslations(); - + const inputsRef = useRef>([]); + const resetForm = () => { + if (inputsRef.current) { + for (let i = 0; i < inputsRef.current.length; i++) { + inputsRef.current[i].value = ''; + } + inputsRef.current[0].focus(); + } + }; return (
@@ -133,15 +141,26 @@ function PasscodeScreen({ form, className }: { form: TAuthenticationPasscode } & {/* Auth code input */}
- {t('pages.auth.INPUT_INVITE_CODE')} +
+ + {t('pages.auth.INPUT_INVITE_CODE')} + + resetForm()} + className="text-xs font-normal cursor-pointer hover:underline text-gray-400" + > + {t('common.RESET')} + +
{ form.setFormValues((v) => ({ ...v, code })); diff --git a/apps/web/lib/components/inputs/auth-code-input.tsx b/apps/web/lib/components/inputs/auth-code-input.tsx index 741ca9f64..14265ec99 100644 --- a/apps/web/lib/components/inputs/auth-code-input.tsx +++ b/apps/web/lib/components/inputs/auth-code-input.tsx @@ -1,13 +1,14 @@ 'use client'; import { clsxm } from '@app/utils'; -import React, { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; +import React, { MutableRefObject, forwardRef, useEffect, useImperativeHandle, useRef } from 'react'; import { InputField } from './input'; import { useTranslations } from 'next-intl'; const allowedCharactersValues = ['alpha', 'numeric', 'alphanumeric'] as const; export type AuthCodeProps = { + inputReference?: MutableRefObject; allowedCharacters?: (typeof allowedCharactersValues)[number]; ariaLabel?: string; autoFocus?: boolean; @@ -64,6 +65,7 @@ const propsMap: { [key: string]: InputProps } = { export const AuthCodeInputField = forwardRef( ( { + inputReference = null, allowedCharacters = 'alphanumeric', ariaLabel, autoFocus = true, @@ -87,8 +89,8 @@ export const AuthCodeInputField = forwardRef( if (!allowedCharactersValues.some((value) => value === allowedCharacters)) { throw new Error(t('errors.INVALID_ALLOWED_CHARACTER')); } - - const inputsRef = useRef>([]); + const reference = useRef([]); + const inputsRef = inputReference || reference; const inputProps = propsMap[allowedCharacters]; const validDefaultValue = defaultValue && defaultValue.length === length && defaultValue.match(inputProps.pattern) ? true : false; @@ -122,7 +124,6 @@ export const AuthCodeInputField = forwardRef( const res = inputsRef.current.map((input) => input.value).join(''); onChange && onChange(res); }; - const handleOnChange = (e: React.ChangeEvent) => { const { target: { value, nextElementSibling } @@ -132,6 +133,7 @@ export const AuthCodeInputField = forwardRef( if (value.length > 1) { e.target.value = value.charAt(0); + if (nextElementSibling !== null) { (nextElementSibling as HTMLInputElement).focus(); }