/* eslint-disable react/no-children-prop */ "use client" import * as React from "react" 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, CardFooter } from "@/registry/new-york-v4/ui/card" import { Checkbox } from "@/registry/new-york-v4/ui/checkbox" import { Field, FieldContent, FieldDescription, FieldError, FieldGroup, FieldLabel, FieldLegend, FieldSeparator, FieldSet, FieldTitle, } from "@/registry/new-york-v4/ui/field" import { RadioGroup, RadioGroupItem, } from "@/registry/new-york-v4/ui/radio-group" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/registry/new-york-v4/ui/select" import { Switch } from "@/registry/new-york-v4/ui/switch" const addons = [ { id: "analytics", title: "Analytics", description: "Advanced analytics and reporting", }, { id: "backup", title: "Backup", description: "Automated daily backups", }, { id: "support", title: "Priority Support", description: "24/7 premium customer support", }, ] as const const formSchema = z.object({ plan: z .string({ required_error: "Please select a subscription plan", }) .min(1, "Please select a subscription plan") .refine((value) => value === "basic" || value === "pro", { message: "Invalid plan selection. Please choose Basic or Pro", }), billingPeriod: z .string({ required_error: "Please select a billing period", }) .min(1, "Please select a billing period"), addons: z .array(z.string()) .min(1, "Please select at least one add-on") .max(3, "You can select up to 3 add-ons") .refine( (value) => value.every((addon) => addons.some((a) => a.id === addon)), { message: "You selected an invalid add-on", } ), emailNotifications: z.boolean(), }) export default function FormTanstackComplex() { const form = useForm({ defaultValues: { plan: "basic", billingPeriod: "monthly", addons: [] as string[], emailNotifications: false, }, 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 (
{ e.preventDefault() form.handleSubmit() }} > { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return (
Subscription Plan Choose your subscription plan. Basic For individuals and small teams Pro For businesses with higher demands {isInvalid && ( )}
) }} /> { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return ( Billing Period Choose how often you want to be billed. {isInvalid && ( )} ) }} /> { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return (
Add-ons Select additional features you'd like to include. {addons.map((addon) => ( { if (checked) { field.pushValue(addon.id) } else { const index = field.state.value.indexOf( addon.id ) if (index > -1) { field.removeValue(index) } } }} /> {addon.title} {addon.description} ))} {isInvalid && ( )}
) }} /> { const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid return ( Email Notifications Receive email updates about your subscription {isInvalid && ( )} ) }} />
) }