/* eslint-disable react/no-children-prop */ "use client" import { useForm } from "@tanstack/react-form" import { toast } from "sonner" import * as z from "zod" import { Button } from "@/registry/new-york-v4/ui/button" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/registry/new-york-v4/ui/card" import { Checkbox } from "@/registry/new-york-v4/ui/checkbox" import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSeparator, FieldSet, } from "@/registry/new-york-v4/ui/field" const tasks = [ { id: "push", label: "Push notifications", }, { id: "email", label: "Email notifications", }, ] as const const formSchema = z.object({ responses: z.boolean(), tasks: z .array(z.string()) .min(1, "Please select at least one notification type.") .refine( (value) => value.every((task) => tasks.some((t) => t.id === task)), { message: "Invalid notification type selected.", } ), }) export default function FormTanstackCheckbox() { const form = useForm({ defaultValues: { responses: true, tasks: [] as string[], }, validators: { onSubmit: formSchema, }, onSubmit: async ({ value }) => { toast("You submitted the following values:", { description: (
            {JSON.stringify(value, null, 2)}
          
), position: "bottom-right", classNames: { content: "flex flex-col gap-2", }, style: { "--border-radius": "calc(var(--radius) + 4px)", } as React.CSSProperties, }) }, }) return ( Notifications Manage your notification preferences.
{ e.preventDefault() form.handleSubmit() }} > { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return (
Responses Get notified for requests that take time, like research or image generation. field.handleChange(checked === true) } disabled /> Push notifications
{isInvalid && ( )}
) }} /> { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return (
Tasks Get notified when tasks you've created have updates. {tasks.map((task) => ( { if (checked) { field.pushValue(task.id) } else { const index = field.state.value.indexOf( task.id ) if (index > -1) { field.removeValue(index) } } }} /> {task.label} ))}
{isInvalid && ( )}
) }} />
) }