{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "checkbox-form-multiple", "type": "registry:example", "author": "shadcn (https://ui.shadcn.com)", "registryDependencies": [ "checkbox", "form" ], "files": [ { "path": "examples/checkbox-form-multiple.tsx", "content": "\"use client\"\n\nimport { zodResolver } from \"@hookform/resolvers/zod\"\nimport { useForm } from \"react-hook-form\"\nimport { z } from \"zod\"\n\nimport { toast } from \"@/registry/default/hooks/use-toast\"\nimport { Button } from \"@/registry/default/ui/button\"\nimport { Checkbox } from \"@/registry/default/ui/checkbox\"\nimport {\n Form,\n FormControl,\n FormDescription,\n FormField,\n FormItem,\n FormLabel,\n FormMessage,\n} from \"@/registry/default/ui/form\"\n\nconst items = [\n {\n id: \"recents\",\n label: \"Recents\",\n },\n {\n id: \"home\",\n label: \"Home\",\n },\n {\n id: \"applications\",\n label: \"Applications\",\n },\n {\n id: \"desktop\",\n label: \"Desktop\",\n },\n {\n id: \"downloads\",\n label: \"Downloads\",\n },\n {\n id: \"documents\",\n label: \"Documents\",\n },\n] as const\n\nconst FormSchema = z.object({\n items: z.array(z.string()).refine((value) => value.some((item) => item), {\n message: \"You have to select at least one item.\",\n }),\n})\n\nexport default function CheckboxReactHookFormMultiple() {\n const form = useForm>({\n resolver: zodResolver(FormSchema),\n defaultValues: {\n items: [\"recents\", \"home\"],\n },\n })\n\n function onSubmit(data: z.infer) {\n toast({\n title: \"You submitted the following values:\",\n description: (\n
\n          {JSON.stringify(data, null, 2)}\n        
\n ),\n })\n }\n\n return (\n
\n \n (\n \n
\n Sidebar\n \n Select the items you want to display in the sidebar.\n \n
\n {items.map((item) => (\n {\n return (\n \n \n {\n return checked\n ? field.onChange([...field.value, item.id])\n : field.onChange(\n field.value?.filter(\n (value) => value !== item.id\n )\n )\n }}\n />\n \n \n {item.label}\n \n
\n )\n }}\n />\n ))}\n \n \n )}\n />\n \n \n \n )\n}\n", "type": "registry:example", "target": "" } ] }