"use client" import * as React from "react" import { Form, Field as FormischField, reset, useForm } from "@formisch/react" import type { SubmitHandler } from "@formisch/react" import { toast } from "sonner" import * as v from "valibot" 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, 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 = v.object({ plan: v.pipe( v.string(), v.minLength(1, "Please select a subscription plan"), v.check( (value) => value === "basic" || value === "pro", "Invalid plan selection. Please choose Basic or Pro" ) ), billingPeriod: v.pipe( v.string(), v.minLength(1, "Please select a billing period") ), addons: v.pipe( v.array(v.string()), v.minLength(1, "Please select at least one add-on"), v.maxLength(3, "You can select up to 3 add-ons"), v.check( (value) => value.every((addon) => addons.some((a) => a.id === addon)), "You selected an invalid add-on" ) ), emailNotifications: v.boolean(), }) export default function FormFormischComplex() { const form = useForm({ schema: FormSchema, initialInput: { plan: "basic", billingPeriod: "", addons: [], emailNotifications: false, }, }) const handleSubmit: SubmitHandler = (output) => { toast("You submitted the following values:", { description: (
          {JSON.stringify(output, null, 2)}
        
), position: "bottom-right", classNames: { content: "flex flex-col gap-2", }, style: { "--border-radius": "calc(var(--radius) + 4px)", } as React.CSSProperties, }) } return ( You're almost there! Choose your subscription plan and billing period.
{(field) => (
Subscription Plan Choose your subscription plan. field.onChange(value)} aria-invalid={field.errors !== null} > Basic For individuals and small teams Pro For businesses with higher demands {field.errors && ( ({ message }))} /> )}
)}
{(field) => ( Billing Period Choose how often you want to be billed. {field.errors && ( ({ message }))} /> )} )} {(field) => { const current = field.input ?? [] return (
Add-ons Select additional features you'd like to include. {addons.map((addon) => ( { field.onChange( checked === true ? [...current, addon.id] : current.filter( (value) => value !== addon.id ) ) }} /> {addon.title} {addon.description} ))} {field.errors && ( ({ message }))} /> )}
) }}
{(field) => ( Email Notifications Receive email updates about your subscription field.onChange(checked)} aria-invalid={field.errors !== null} /> {field.errors && ( ({ message }))} /> )} )}
) }