"use client" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { z } from "zod" import { Button } from "@/components/ui/button" import { Checkbox } from "@/components/ui/checkbox" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { toast } from "@/components/ui/use-toast" const items = [ { id: "recents", label: "Recents", }, { id: "home", label: "Home", }, { id: "applications", label: "Applications", }, { id: "desktop", label: "Desktop", }, { id: "downloads", label: "Downloads", }, { id: "documents", label: "Documents", }, ] as const const FormSchema = z.object({ items: z.array(z.string()).refine((value) => value.some((item) => item), { message: "You have to select at least one item.", }), }) export function CheckboxReactHookFormMultiple() { const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { items: ["recents", "home"], }, }) function onSubmit(data: z.infer) { toast({ title: "You submitted the following values:", description: (
					{JSON.stringify(data, null, 2)}
				
), }) } return (
(
Sidebar Select the items you want to display in the sidebar.
{items.map((item) => ( { return ( { return checked ? field.onChange([...field.value, item.id]) : field.onChange( field.value?.filter( (value) => value !== item.id ) ) }} /> {item.label} ) }} /> ))}
)} /> ) }