Form
A form that validates itself: fields report to it, it focuses the first that fails and hands you the values. No form library.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Form, FormDescription, FormField, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
export default function FormDemo() {
const [sent, setSent] = useState<Record<string, unknown> | null>(null);
return (
<div className="flex w-full max-w-sm flex-col gap-5">
<Form onFormSubmit={(values) => setSent(values)}>
<FormField name="username">
<FormLabel>Username</FormLabel>
<Input required minLength={3} placeholder="ballpoint" seed="form-username" />
<FormDescription>This is how you appear on the page.</FormDescription>
<FormMessage match="valueMissing">Choose a username.</FormMessage>
<FormMessage match="tooShort">At least three letters, please.</FormMessage>
</FormField>
<FormField name="email">
<FormLabel>Email</FormLabel>
<Input type="email" required placeholder="you@example.com" seed="form-email" />
<FormMessage match="valueMissing">We need somewhere to write to.</FormMessage>
<FormMessage match="typeMismatch">That email is missing its domain.</FormMessage>
</FormField>
<Button type="submit" className="self-start" seed="form-submit">
Sign up
</Button>
</Form>
{sent && (
<pre className="text-sm text-ink-2" aria-live="polite">
{JSON.stringify(sent, null, 2)}
</pre>
)}
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ form"use client";
import { Field as FieldPrimitive } from "@base-ui/react/field";
import { Form as FormPrimitive } from "@base-ui/react/form";
import { inkClassName } from "@/lib/ink";
/**
* A form that validates itself. Fields inside it (FormField) report their
* validity to it; on submit it checks every field, focuses the first that
* fails and hands `onFormSubmit` the values by name. Server-side errors go in
* `errors`, keyed by field name. No extra library: Input, Checkbox, Select,
* Switch, Slider and the rest already connect to the FormField around them.
*/
function Form({ className, ...props }: FormPrimitive.Props) {
return <FormPrimitive data-slot="form" className={inkClassName("flex w-full flex-col gap-6", className)} {...props} />;
}
/** One named field of the form: give it `name` and, if it needs more than `required`, a `validate`. */
function FormField({ className, ...props }: FieldPrimitive.Root.Props) {
return <FieldPrimitive.Root data-slot="form-field" className={inkClassName("group/form-field flex w-full flex-col gap-2 data-invalid:text-destructive", className)} {...props} />;
}
function FormLabel({ className, ...props }: FieldPrimitive.Label.Props) {
return (
<FieldPrimitive.Label
data-slot="form-label"
className={inkClassName("flex items-center gap-2 text-base leading-tight select-none data-disabled:opacity-50 data-invalid:text-destructive", className)}
{...props}
/>
);
}
/** Wraps a native control, for when you are not using one of Ballpoint's own. */
function FormControl({ ...props }: FieldPrimitive.Control.Props) {
return <FieldPrimitive.Control data-slot="form-control" {...props} />;
}
function FormDescription({ className, ...props }: FieldPrimitive.Description.Props) {
return <FieldPrimitive.Description data-slot="form-description" className={inkClassName("text-sm leading-normal text-ink-3", className)} {...props} />;
}
/** The error, in red pen. Shown while the field is invalid; `match` narrows it to one kind of failure, such as "valueMissing". */
function FormMessage({ className, ...props }: FieldPrimitive.Error.Props) {
return <FieldPrimitive.Error data-slot="form-message" className={inkClassName("text-sm text-destructive", className)} {...props} />;
}
export { Form, FormField, FormLabel, FormControl, FormDescription, FormMessage };Usage
import { Form, FormDescription, FormField, FormLabel, FormMessage } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
<Form onFormSubmit={(values) => save(values)}>
<FormField name="email">
<FormLabel>Email</FormLabel>
<Input type="email" required />
<FormMessage match="valueMissing">We need somewhere to write to.</FormMessage>
</FormField>
</Form>Examples
Errors from the server
Pass errors keyed by field name; they clear when the field is edited.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Form, FormField, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
export default function FormServerErrors() {
const [errors, setErrors] = useState<Record<string, string>>({});
return (
<Form
className="max-w-sm"
errors={errors}
onFormSubmit={(values) => {
// Pretend the server already has this name. The error goes when the field is edited.
setErrors(values.username === "ada" ? { username: "Someone already writes under that name." } : {});
}}
>
<FormField name="username">
<FormLabel>Username</FormLabel>
<Input defaultValue="ada" seed="fse-username" />
<FormMessage />
</FormField>
<Button type="submit" className="self-start" seed="fse-submit">
Claim it
</Button>
</Form>
);
}API reference
- Form onFormSubmit
(values, details) => voidCalled with every field's value, keyed by name, once they all validate.
- Form errors
Record<string, string | string[]>Errors from outside (a server), keyed by the field's name.
- Form validationModedefault
"onSubmit" "onSubmit" | "onBlur" | "onChange"When fields are first checked.
- FormField name / validate
string / (value) => string | nullThe field's key in the values, and a check of your own.
- FormMessage match
string | booleanShow only for one kind of failure, such as "valueMissing" or "typeMismatch".
Every other prop goes to Base UI's Form.