Components
- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Dialog
- EmptyNew
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Input GroupNew
- KbdNew
- Label
- Menu
- Meter
- Number Field
- Pagination
- Popover
- Preview Card
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- SkeletonNew
- Slider
- SpinnerNew
- 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, FieldError, FieldLabel } from "@/components/ui/field";
import { Form } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
export default function Particle() {
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 className="max-w-64" onSubmit={onSubmit}>
<Field>
<FieldLabel>Email</FieldLabel>
<Input
disabled={loading}
name="email"
placeholder="[email protected]"
required
type="email"
/>
<FieldError>Please enter a valid email.</FieldError>
</Field>
<Button disabled={loading} type="submit">
Submit
</Button>
</Form>
);
}
Installation
pnpm dlx shadcn@latest add @coss/form
Usage
import {
Field,
FieldError,
FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
import { Input } from "@/components/ui/input"<Form
onSubmit={(e) => {
/* handle submit */
}}
>
<Field>
<FieldLabel>Email</FieldLabel>
<Input 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, FieldError, FieldLabel } from "@/components/ui/field";
import { Form } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
const schema = z.object({
age: z.coerce
.number({ message: "Please enter a number." })
.positive({ message: "Number must be positive." }),
name: z.string().min(1, { message: "Please enter a name." }),
});
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));
if (!result.success) {
const { fieldErrors } = z.flattenError(result.error);
return { errors: fieldErrors as Errors };
}
return {
errors: {} as Errors,
};
}
export default function Particle() {
const [loading, setLoading] = React.useState(false);
const [errors, setErrors] = React.useState<Errors>({});
const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
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} onSubmit={onSubmit}>
<Field name="name">
<FieldLabel>Name</FieldLabel>
<Input disabled={loading} placeholder="Enter name" />
<FieldError />
</Field>
<Field name="age">
<FieldLabel>Age</FieldLabel>
<Input disabled={loading} placeholder="Enter age" />
<FieldError />
</Field>
<Button disabled={loading} type="submit">
Submit
</Button>
</Form>
);
}
On This Page