Components
- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Dialog
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Label
- Menu
- Meter
- Number Field
- Pagination
- Popover
- Preview Card
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Slider
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Resources
Form
A form wrapper component that simplifies validation and submission.
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import {
Field,
FieldControl,
FieldError,
FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
export function FormDemo() {
const [loading, setLoading] = React.useState(false)
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
setLoading(true)
await new Promise((r) => setTimeout(r, 800))
setLoading(false)
alert(`Email: ${formData.get("email") || ""}`)
}
return (
<Form onSubmit={onSubmit} className="max-w-64">
<Field>
<FieldLabel>Email</FieldLabel>
<FieldControl
name="email"
type="email"
placeholder="[email protected]"
disabled={loading}
required
/>
<FieldError>Please enter a valid email.</FieldError>
</Field>
<Button type="submit" disabled={loading}>
Submit
</Button>
</Form>
)
}
Installation
pnpm dlx shadcn@latest add https://coss.com/ui/r/form.json
Usage
import {
Field,
FieldControl,
FieldError,
FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
<Form
onSubmit={(e) => {
/* handle submit */
}}
>
<Field>
<FieldLabel>Email</FieldLabel>
<FieldControl name="email" type="email" required />
<FieldError>Please enter a valid email.</FieldError>
</Field>
</Form>
Examples
Using with Zod
"use client"
import * as React from "react"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import {
Field,
FieldControl,
FieldError,
FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
const schema = z.object({
name: z.string().min(1, { message: "Please enter a name." }),
age: z.coerce
.number({ message: "Please enter a number." })
.positive({ message: "Number must be positive." }),
})
type Errors = Record<string, string | string[]>
async function submitForm(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
const formData = new FormData(event.currentTarget)
const result = schema.safeParse(Object.fromEntries(formData as any))
if (!result.success) {
const { fieldErrors } = z.flattenError(result.error)
return { errors: fieldErrors as Errors }
}
return {
errors: {} as Errors,
}
}
export function FormZodDemo() {
const [loading, setLoading] = React.useState(false)
const [errors, setErrors] = React.useState<Errors>({})
const handleClearErrors = (next: Errors) => setErrors(next)
const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
const formEl = event.currentTarget
setLoading(true)
const response = await submitForm(event)
await new Promise((r) => setTimeout(r, 800))
setErrors(response.errors)
setLoading(false)
if (Object.keys(response.errors).length === 0) {
const formData = new FormData(formEl)
alert(
`Name: ${String(formData.get("name") || "")}\nAge: ${String(
formData.get("age") || ""
)}`
)
}
}
return (
<Form
className="max-w-64"
errors={errors}
onClearErrors={handleClearErrors}
onSubmit={onSubmit}
>
<Field name="name">
<FieldLabel>Name</FieldLabel>
<FieldControl placeholder="Enter name" disabled={loading} />
<FieldError />
</Field>
<Field name="age">
<FieldLabel>Age</FieldLabel>
<FieldControl placeholder="Enter age" disabled={loading} />
<FieldError />
</Field>
<Button type="submit" disabled={loading}>
Submit
</Button>
</Form>
)
}
On This Page