Select focus does not work after focusing an input field in a form. #7770
Unanswered
saimarshadsaim31
asked this question in
Q&A
Replies: 1 comment
-
Potential Issues Identified:Focus-Related Issues:
Other Issues:
Here's the corrected code addressing all issues:"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { toast } from "sonner"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
const formSchema = z.object({
name_6891997886: z.string().min(1),
name_9709881618: z.string().min(1),
name_0224756532: z.string().optional(),
name_1375947398: z.string().optional(),
})
export default function MyForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
name_6891997886: "",
name_9709881618: "",
name_0224756532: "",
name_1375947398: "",
},
})
function onSubmit(values: z.infer<typeof formSchema>) {
try {
console.log(values)
toast(
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(values, null, 2)}</code>
</pre>,
)
} catch (error) {
console.error("Form submission error", error)
toast.error("Failed to submit the form. Please try again.")
}
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="mx-auto max-w-3xl space-y-8 py-10">
<div className="grid grid-cols-12 gap-4">
<div className="col-span-6">
<FormField
control={form.control}
name="name_6891997886"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>This is your public display name.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="col-span-6">
<FormField
control={form.control}
name="name_9709881618"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" {...field} />
</FormControl>
<FormDescription>This is your public display name.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
<div className="grid grid-cols-12 gap-4">
<div className="col-span-6">
<FormField
control={form.control}
name="name_0224756532"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="[email protected]">m@example.com</SelectItem>
<SelectItem value="[email protected]">m@google.com</SelectItem>
<SelectItem value="[email protected]">m@support.com</SelectItem>
</SelectContent>
</Select>
<FormDescription>You can manage email addresses in your email settings.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="col-span-6">
<FormField
control={form.control}
name="name_1375947398"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="[email protected]">m@example.com</SelectItem>
<SelectItem value="[email protected]">m@google.com</SelectItem>
<SelectItem value="[email protected]">m@support.com</SelectItem>
</SelectContent>
</Select>
<FormDescription>You can manage email addresses in your email settings.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
<Button type="submit">Submit</Button>
</form>
</Form>
)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using select component along with input components. I am facing a weired behaviour, When i focus/click inputs first and then click select its focus does now work as expected but if i reload the page and click select first the focus works and after that when i click the input again and then click select the focus again breaks. I know it sounds confusing here are the visuals:
In this when i click the select after the inputs its focus does not work:
https://github.com/user-attachments/assets/5e47b9a4-6df0-4d50-9ace-7261fee4ff7f
And when i reload the page and this time use select first the focus works but again if focus inputs and select again it breaks:
https://github.com/user-attachments/assets/261bece9-1b9f-4826-a2cc-4585fdf7f369
This is a clean next js project and has only this component nothing else here is the code:
Beta Was this translation helpful? Give feedback.
All reactions