Manually controlling Select component without register and with setValue and watch #13232
-
|
My select component comes from shadcn and it does not have an <Select
value={watch("category")}
onValueChange={(val) =>
setValue("category", val, { shouldTouch: true, shouldDirty: true, shouldValidate: true })
}
{...register("category")} // won't work
>
<SelectTrigger>...</SelectTrigger>
<SelectContent>...</SelectContent>
</Select>This works well for now but I'm inclined to ask if there are any obvious problems with this method. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
|
because |
Beta Was this translation helpful? Give feedback.
-
|
How about using <FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a verified email to display" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="m@example.com">m@example.com</SelectItem>
<SelectItem value="m@google.com">m@google.com</SelectItem>
<SelectItem value="m@support.com">m@support.com</SelectItem>
</SelectContent>
</Select>
<FormDescription>
You can manage email addresses in your{" "}
<Link href="/examples/forms">email settings</Link>.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>The new Shadcn form docs don't provide an example with <Controller
name="title"
control={form.control}
render={({ field, fieldState }) => (
<Field data-invalid="{fieldState.invalid}">
<FieldLabel htmlFor="form-rhf-demo-title"> Bug Title </FieldLabel>
<input
{...field}
id="form-rhf-demo-title"
aria-invalid="{fieldState.invalid}"
placeholder="Login button not working on mobile"
autocomplete="off"
/>
{fieldState.invalid && <FieldError errors="{[fieldState.error]}" />}
</Field>
)}
/>; |
Beta Was this translation helpful? Give feedback.
How about using
<Controller />instead? It looks like the new Select docs don't have an example of a select with a form, but you can get an idea from the legacy docs. Here,<FormField />uses aControllerinternally.