-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example with react-hook-form #7
Comments
Hey @cattanr! I'm happy you enjoy the component. |
Hey @mxkaske, I'm also looking into this. |
Hey guys! So I just created a branch It's more of a guideline and provides a first functional draft.
preview link (open the console to see on submit trigger) I hope this helps a bit more! // @/app/examples/multi-select-form/page.tsx
"use client";
import * as z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { FancyMultiSelect } from "@/components/craft/fancy-multi-select";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
const formSchema = z.object({
frameworks: z
.array(z.string())
.refine((value) => value.some((item) => item), {
message: "You have to select at least one item.",
}),
});
export default function ExampleForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
frameworks: [],
},
});
function onSubmit(values: z.infer<typeof formSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
console.log(values);
}
return (
<main className="container mx-auto flex min-h-screen items-center justify-center">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="frameworks"
render={({ field }) => (
<FormItem>
<FormLabel>Frameworks</FormLabel>
<FormControl>
<FancyMultiSelect
onChange={(values) => {
field.onChange(values.map(({ value }) => value));
}}
/>
</FormControl>
<FormDescription>All the frameworks you like.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
</main>
);
} |
Hello @mxkaske, thank you for taking the time to create a first draft with react hook form! I'm currently also trying to use multiselect via shadcn on a multi page form. Current:
I changed it to:
Sandbox: https://codesandbox.io/s/javascript-forked-gldwdt?file=/index.js:854-1075 Thank you so much for this component! |
anything about default values? |
If you're encountering the error To fix it you have 2 options:1- Either Downgrade 2- Or include Command.List
Edit: i prefer the first option (downgrade cmdk) since the 2nd one caused the same error to show up when selecting all the commands and trying to type in the select input (it may be solvable, but I don't have time to investigate right now) |
@Chr1s0Blood I solved the problem with defaultValues something like this. Not ideal (more than sure), but it works in my case
By default field.value = [] but if default value field.value = [....] we are gonna set this data to setSelected |
Hi,
nice work, thank you for the component ! I wanted to use the multiselect inside a form using react-hook-form. I tried to do it with the example from the checkbox component here https://ui.shadcn.com/docs/components/checkbox#:~:text=Submit-,Checkboxes,-Preview but I can't see my value on submit.
Do you have any example of multiselect inside a form ?
Thank you for your help
The text was updated successfully, but these errors were encountered: