summaryrefslogtreecommitdiff
path: root/src-migrate/modules/register/components/Form.tsx
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2024-09-17 09:01:04 +0700
committerit-fixcomart <it@fixcomart.co.id>2024-09-17 09:01:04 +0700
commitaaf907c834343970e1d30b3ef49c13ed5f9d34ed (patch)
treee22a80c3b9bfc2835ad78c13fc51fcafbc6598a0 /src-migrate/modules/register/components/Form.tsx
parent499954a8be814850103eece2dbd0306d6246989d (diff)
<iman> add focus error when button submit click
Diffstat (limited to 'src-migrate/modules/register/components/Form.tsx')
-rw-r--r--src-migrate/modules/register/components/Form.tsx43
1 files changed, 41 insertions, 2 deletions
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<FormProps> = ({
type,
required,
isBisnisRegist = false,
- chekValid = false,
+ chekValid,
+ buttonSubmitClick,
}) => {
const { form, isCheckedTNC, isValidCaptcha, errors, updateForm, validate } =
useRegisterStore();
@@ -30,6 +32,11 @@ const Form: React.FC<FormProps> = ({
const router = useRouter();
const toast = useToast();
+ const emailRef = useRef<HTMLInputElement>(null);
+ const nameRef = useRef<HTMLInputElement>(null);
+ const passwordRef = useRef<HTMLInputElement>(null);
+ const teleponRef = useRef<HTMLDivElement>(null);
+
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
updateForm(name, value);
@@ -40,6 +47,34 @@ const Form: React.FC<FormProps> = ({
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<HTMLFormElement>) => {
e.preventDefault();
@@ -101,6 +136,7 @@ const Form: React.FC<FormProps> = ({
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<FormProps> = ({
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<FormProps> = ({
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<FormProps> = ({
id='phone'
name='phone'
className='form-input mt-3'
+ ref={passwordRef}
placeholder='08xxxxxxxx'
value={form.phone}
onChange={handleInputChange}