From 810c5da6e26fdfc4f29df75dca13c0fdb787f34a Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 18 Sep 2024 09:29:41 +0700 Subject: update auto scrool to when error --- src-migrate/modules/register/components/Form.tsx | 32 +++++++++++++----------- 1 file changed, 18 insertions(+), 14 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 b04e763c..55d542f2 100644 --- a/src-migrate/modules/register/components/Form.tsx +++ b/src-migrate/modules/register/components/Form.tsx @@ -26,7 +26,7 @@ const Form: React.FC = ({ }) => { const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } = useRegisterStore(); - + console.log('buttonSubmitClick', buttonSubmitClick); const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); const router = useRouter(); @@ -35,7 +35,7 @@ const Form: React.FC = ({ const emailRef = useRef(null); const nameRef = useRef(null); const passwordRef = useRef(null); - const teleponRef = useRef(null); + const phoneRef = useRef(null); const handleInputChange = (event: ChangeEvent) => { const { name, value } = event.target; @@ -51,29 +51,33 @@ const Form: React.FC = ({ const loadIndustries = async () => { const response = await mutation.mutateAsync(form); if (!response?.register) { - // Logic to focus on first invalid input + const options: ScrollIntoViewOptions = { + behavior: 'smooth', + block: 'center', + }; + if (errors.email_partner && emailRef.current) { - emailRef.current.focus(); + emailRef.current.scrollIntoView(options); return; } if (errors.name && nameRef.current) { - nameRef.current.focus(); + nameRef.current.scrollIntoView(options); return; } if (errors.password && passwordRef.current) { - passwordRef.current.focus(); + passwordRef.current.scrollIntoView(options); return; } - if (errors.phone && teleponRef.current) { - teleponRef.current.scrollIntoView(); + if (errors.phone && phoneRef.current) { + phoneRef.current.scrollIntoView(options); return; } } }; loadIndustries(); - }, [buttonSubmitClick]); + }, [buttonSubmitClick, chekValid]); const handleSubmit = async (e: ChangeEvent) => { e.preventDefault(); @@ -133,7 +137,7 @@ const Form: React.FC = ({ type='text' id='name' name='name' - className='form-input mt-3' + className='form-input mt-3 transition-all duration-700' placeholder='Masukan nama lengkap anda' value={form.name} ref={nameRef} @@ -155,7 +159,7 @@ const Form: React.FC = ({ type='text' id='email' name='email' - className='form-input mt-3' + className='form-input mt-3 transition-all duration-500' placeholder='Masukan alamat email anda' value={form.email} ref={emailRef} @@ -177,7 +181,7 @@ const Form: React.FC = ({ type='password' name='password' id='password' - className='form-input mt-3' + className='form-input mt-3 transition-all duration-500' placeholder='••••••••••••' value={form.password} ref={passwordRef} @@ -200,10 +204,10 @@ const Form: React.FC = ({ type='tel' id='phone' name='phone' - className='form-input mt-3' - ref={passwordRef} + className='form-input mt-3 transition-all duration-500' placeholder='08xxxxxxxx' value={form.phone} + ref={phoneRef} onChange={handleInputChange} aria-invalid={chekValid && !!errors.phone} /> -- cgit v1.2.3