Form
A form component that provides validation and error handling for form inputs.
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button/button";
import { Field, FieldError, FieldLabel } from "@/components/ui/field/field";
import { Form } from "@/components/ui/form/form";
import { Input } from "@/components/ui/input/input";
export default 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 className="max-w-64" onSubmit={onSubmit}>
<Field>
<FieldLabel>Email</FieldLabel>
<Input disabled={loading} name="email" placeholder="you@example.com" required type="email" />
<FieldError>Please enter a valid email.</FieldError>
</Field>
<Button className="mt-4 w-full" disabled={loading} type="submit">
Submit
</Button>
</Form>
);
}
npx shadcn@latest add @roiui/formnpx shadcn@latest add @roiui/form-tailwindanatomy
<Form>
<FormGroup>
<FormField>
<FormLabel />
<FormDescription />
<FormControl />
<FormError />
</FormField>
</FormGroup>
<FormActions></FormActions>
</Form>