From aaf907c834343970e1d30b3ef49c13ed5f9d34ed Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 17 Sep 2024 09:01:04 +0700 Subject: add focus error when button submit click --- src-migrate/modules/register/components/Form.tsx | 43 ++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'src-migrate/modules/register/components/Form.tsx') diff --git a/src-migrate/modules/register/components/Form.tsx b/src-migrate/modules/register/components/Form.tsx index 118d9d69..b04e763c 100644 --- a/src-migrate/modules/register/components/Form.tsx +++ b/src-migrate/modules/register/components/Form.tsx @@ -1,4 +1,4 @@ -import { ChangeEvent, useMemo } from 'react'; +import { ChangeEvent, useMemo, useEffect, useRef, useState } from 'react'; import { useMutation } from 'react-query'; import { useRegisterStore } from '../stores/useRegisterStore'; import { RegisterProps } from '~/types/auth'; @@ -14,13 +14,15 @@ interface FormProps { required: boolean; isBisnisRegist: boolean; chekValid: boolean; + buttonSubmitClick: boolean; } const Form: React.FC = ({ type, required, isBisnisRegist = false, - chekValid = false, + chekValid, + buttonSubmitClick, }) => { const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } = useRegisterStore(); @@ -30,6 +32,11 @@ const Form: React.FC = ({ const router = useRouter(); const toast = useToast(); + const emailRef = useRef(null); + const nameRef = useRef(null); + const passwordRef = useRef(null); + const teleponRef = useRef(null); + const handleInputChange = (event: ChangeEvent) => { const { name, value } = event.target; updateForm(name, value); @@ -40,6 +47,34 @@ const Form: React.FC = ({ mutationFn: (data: RegisterProps) => registerUser(data), }); + useEffect(() => { + const loadIndustries = async () => { + const response = await mutation.mutateAsync(form); + if (!response?.register) { + // Logic to focus on first invalid input + if (errors.email_partner && emailRef.current) { + emailRef.current.focus(); + return; + } + if (errors.name && nameRef.current) { + nameRef.current.focus(); + return; + } + + if (errors.password && passwordRef.current) { + passwordRef.current.focus(); + return; + } + + if (errors.phone && teleponRef.current) { + teleponRef.current.scrollIntoView(); + return; + } + } + }; + loadIndustries(); + }, [buttonSubmitClick]); + const handleSubmit = async (e: ChangeEvent) => { e.preventDefault(); @@ -101,6 +136,7 @@ const Form: React.FC = ({ className='form-input mt-3' placeholder='Masukan nama lengkap anda' value={form.name} + ref={nameRef} onChange={handleInputChange} aria-invalid={chekValid && !!errors.name} /> @@ -122,6 +158,7 @@ const Form: React.FC = ({ className='form-input mt-3' placeholder='Masukan alamat email anda' value={form.email} + ref={emailRef} onChange={handleInputChange} autoComplete='username' aria-invalid={chekValid && !!errors.email} @@ -143,6 +180,7 @@ const Form: React.FC = ({ className='form-input mt-3' placeholder='••••••••••••' value={form.password} + ref={passwordRef} onChange={handleInputChange} autoComplete='current-password' aria-invalid={chekValid && !!errors.password} @@ -163,6 +201,7 @@ const Form: React.FC = ({ id='phone' name='phone' className='form-input mt-3' + ref={passwordRef} placeholder='08xxxxxxxx' value={form.phone} onChange={handleInputChange} -- cgit v1.2.3