mirror of
https://github.com/hanzoai/shadcn.git
synced 2026-11-03 03:18:52 +00:00
Upstream sync from shadcn-ui/ui (1686 commits since last merge base). Key changes: - packages/shadcn upgraded to 4.1.0 (CLI font transformers, scaffold from github, chart colors) - apps/v4 replaces apps/www as the primary docs/registry app - Font system: 25+ heading font registries, font markers utility, @supports override - Chart improvements: recharts v3 compat, radial chart fixes - templates/ updated with monorepo variants (astro, vite, react-router, start) - skills/shadcn AI skill definitions added - deprecated/ removed (auth, auth-firebase, cli, www) Preserved Hanzo-only paths: app/, pkg/, demo/, docs/, template/next/
180 lines
6.1 KiB
TypeScript
180 lines
6.1 KiB
TypeScript
/* eslint-disable react/no-children-prop */
|
|
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useForm } from "@tanstack/react-form"
|
|
import { XIcon } from "lucide-react"
|
|
import { toast } from "sonner"
|
|
import { 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 {
|
|
Field,
|
|
FieldContent,
|
|
FieldDescription,
|
|
FieldError,
|
|
FieldGroup,
|
|
FieldLegend,
|
|
FieldSet,
|
|
} from "@/registry/new-york-v4/ui/field"
|
|
import {
|
|
InputGroup,
|
|
InputGroupAddon,
|
|
InputGroupButton,
|
|
InputGroupInput,
|
|
} from "@/registry/new-york-v4/ui/input-group"
|
|
|
|
const formSchema = z.object({
|
|
emails: z
|
|
.array(
|
|
z.object({
|
|
address: z.string().email("Enter a valid email address."),
|
|
})
|
|
)
|
|
.min(1, "Add at least one email address.")
|
|
.max(5, "You can add up to 5 email addresses."),
|
|
})
|
|
|
|
export default function FormTanstackArray() {
|
|
const form = useForm({
|
|
defaultValues: {
|
|
emails: [{ address: "" }],
|
|
},
|
|
validators: {
|
|
onBlur: formSchema,
|
|
},
|
|
onSubmit: async ({ value }) => {
|
|
toast("You submitted the following values:", {
|
|
description: (
|
|
<pre className="mt-2 w-[320px] overflow-x-auto rounded-md bg-code p-4 text-code-foreground">
|
|
<code>{JSON.stringify(value, null, 2)}</code>
|
|
</pre>
|
|
),
|
|
position: "bottom-right",
|
|
classNames: {
|
|
content: "flex flex-col gap-2",
|
|
},
|
|
style: {
|
|
"--border-radius": "calc(var(--radius) + 4px)",
|
|
} as React.CSSProperties,
|
|
})
|
|
},
|
|
})
|
|
|
|
return (
|
|
<Card className="w-full sm:max-w-md">
|
|
<CardHeader className="border-b">
|
|
<CardTitle>Contact Emails</CardTitle>
|
|
<CardDescription>Manage your contact email addresses.</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form
|
|
id="form-tanstack-array"
|
|
onSubmit={(e) => {
|
|
e.preventDefault()
|
|
form.handleSubmit()
|
|
}}
|
|
>
|
|
<form.Field name="emails" mode="array">
|
|
{(field) => {
|
|
const isInvalid =
|
|
field.state.meta.isTouched && !field.state.meta.isValid
|
|
return (
|
|
<FieldSet className="gap-4">
|
|
<FieldLegend variant="label">Email Addresses</FieldLegend>
|
|
<FieldDescription>
|
|
Add up to 5 email addresses where we can contact you.
|
|
</FieldDescription>
|
|
<FieldGroup className="gap-4">
|
|
{field.state.value.map((_, index) => (
|
|
<form.Field
|
|
key={index}
|
|
name={`emails[${index}].address`}
|
|
children={(subField) => {
|
|
const isSubFieldInvalid =
|
|
subField.state.meta.isTouched &&
|
|
!subField.state.meta.isValid
|
|
return (
|
|
<Field
|
|
orientation="horizontal"
|
|
data-invalid={isSubFieldInvalid}
|
|
>
|
|
<FieldContent>
|
|
<InputGroup>
|
|
<InputGroupInput
|
|
id={`form-tanstack-array-email-${index}`}
|
|
name={subField.name}
|
|
value={subField.state.value}
|
|
onBlur={subField.handleBlur}
|
|
onChange={(e) =>
|
|
subField.handleChange(e.target.value)
|
|
}
|
|
aria-invalid={isSubFieldInvalid}
|
|
placeholder="[email protected]"
|
|
type="email"
|
|
autoComplete="email"
|
|
/>
|
|
{field.state.value.length > 1 && (
|
|
<InputGroupAddon align="inline-end">
|
|
<InputGroupButton
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon-xs"
|
|
onClick={() => field.removeValue(index)}
|
|
aria-label={`Remove email ${index + 1}`}
|
|
>
|
|
<XIcon />
|
|
</InputGroupButton>
|
|
</InputGroupAddon>
|
|
)}
|
|
</InputGroup>
|
|
{isSubFieldInvalid && (
|
|
<FieldError
|
|
errors={subField.state.meta.errors}
|
|
/>
|
|
)}
|
|
</FieldContent>
|
|
</Field>
|
|
)
|
|
}}
|
|
/>
|
|
))}
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => field.pushValue({ address: "" })}
|
|
disabled={field.state.value.length >= 5}
|
|
>
|
|
Add Email Address
|
|
</Button>
|
|
</FieldGroup>
|
|
{isInvalid && <FieldError errors={field.state.meta.errors} />}
|
|
</FieldSet>
|
|
)
|
|
}}
|
|
</form.Field>
|
|
</form>
|
|
</CardContent>
|
|
<CardFooter className="border-t">
|
|
<Field orientation="horizontal">
|
|
<Button type="button" variant="outline" onClick={() => form.reset()}>
|
|
Reset
|
|
</Button>
|
|
<Button type="submit" form="form-tanstack-array">
|
|
Save
|
|
</Button>
|
|
</Field>
|
|
</CardFooter>
|
|
</Card>
|
|
)
|
|
}
|