Shadcn input focus shift issue
Answered
Allegheny mound ant posted this in #help-forum
Allegheny mound antOP
Hey Guys Im using shadcn in nextjs14 app router. I'm just facing literally wierd input focus bug in shadcn form . Im using Shadcn form, React hook forms and zod validation.
34 Replies
Allegheny mound antOP
Check this video
Can somebody explain how do I stop this weird behavior ??
my form code:
function LoginPage() {
const form = useForm<TLoginSchema>({
resolver: zodResolver(LoginSchema),
defaultValues: {
email: "",
password: "",
},
});
const loginSubmit = (data: TLoginSchema) => {};
return (
<AuthWrapper
headerLabel="Welcome to Login form"
additionalBtnHref="/auth/register"
additionalBtnLabel="Dont have account? "
>
<Form {...form}>
<form onClick={form.handleSubmit(loginSubmit)} className="space-y-8">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
placeholder="Enter your email"
type="email"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
placeholder="Enter your password"
type="email"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</AuthWrapper>
);
}schema.ts:
import { z } from "zod";
export const LoginSchema = z.object({
email: z.string().email({
message: "Email is required!!",
}),
password: z.string().min(6, {
message: "Password must be greater than 6 letters",
}),
});
export type TLoginSchema = z.infer<typeof LoginSchema>;Can you tell how to reproduce it
Allegheny mound antOP
Yeah, you can directly use the shadcn form with react hook forms and zod validation
Allegheny mound antOP
Yeah, Im simple used the shadcn form :
1. Define the schema ex: LoginSchema
2. Configure for react form hook
3. Use the form elements of shadcn to create the form.
1. Define the schema ex: LoginSchema
2. Configure for react form hook
3. Use the form elements of shadcn to create the form.
@Allegheny mound ant schema.ts:
ts
import { z } from "zod";
export const LoginSchema = z.object({
email: z.string().email({
message: "Email is required!!",
}),
password: z.string().min(6, {
message: "Password must be greater than 6 letters",
}),
});
export type TLoginSchema = z.infer<typeof LoginSchema>;
Allegheny mound antOP
this is the schema
"use client";
import AuthWrapper from "@/components/auth/AuthWrapper";
import { LoginSchema, TLoginSchema } from "@/lib/Types/schemas";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
function LoginPage() {
const form = useForm<TLoginSchema>({
resolver: zodResolver(LoginSchema),
defaultValues: {
email: "",
password: "",
},
});
const loginSubmit = (data: TLoginSchema) => {};
return (
<Form {...form}>
<form onClick={form.handleSubmit(loginSubmit)} className="space-y-8">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
placeholder="Enter your email"
type="email"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input
placeholder="Enter your password"
type="email"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
);
}
export default LoginPage;and this is the main code ,
you can use these files to reproduce this issue
Allegheny mound antOP
@Anay-208 found any sol.
?
@Anay-208 Can you tell how to reproduce it
I'm still waiting for a reply to this question
Allegheny mound antOP
I already gave you a detailed explaination about this question🤨, want me to create a minimal reporduction repo ?
I need to know like what keys did you press like tab or something similar. A repo will be useful
Allegheny mound antOP
@Anay-208 check this minimal reproduction repo of this issue.
Steps to run:
- git clone https://github.com/shoaibkh4n/minimal-repo.git.
-npm install
-npm run dev.
go to
- git clone https://github.com/shoaibkh4n/minimal-repo.git.
-npm install
-npm run dev.
go to
localhost:3000/auth/login to see this in action@Anay-208 I need to know like what keys did you press like tab or something similar. A repo will be useful
Allegheny mound antOP
navigation through tabs working fine but causing issue when switching by click on either input field
Allegheny mound antOP
hey @Anay-208 got the issue ?
I'll check when I'm free
@Allegheny mound ant Can you give further details on how to reproduce it
oh got it
@Anay-208 oh got it
Allegheny mound antOP
ðŸ‘
@Allegheny mound ant this is the schema
@Allegheny mound ant I've found the source of the issue. instead of
onClick, it should be onSubmitonClick is submitting the form, and it sees invalid fields, and its directing the user to that
@Allegheny mound ant Mark my message as a solution
Allegheny mound antOP
aaahhhhhhh I am stupid asf bro
This is so silly issue. 😣
Thanks to identify this man, 🎉
@Allegheny mound ant Thanks to identify this man, 🎉
Mark message as a solution
Answer
Right click apps mark solution