React Form Field and Label
A composable React form field that wires a label, a description and an error message to any control, ready for react-hook-form.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/field.jsonWe only use it to send receipts.
FieldFieldErrorFieldLabelFieldControlFieldDescription- +9 more
- +6 more
Using it in a form? See the react-hook-form and zod recipe.
Label, description and error
The error shows after the field has been visited and is invalid. Type a bad address and tab away.
We only use it to send receipts.
import { useState } from "react";
import { Field, FieldControl, FieldDescription, FieldError, FieldLabel } from "@/components/sheenui/field/field";
import { Input } from "@/components/sheenui/input/input";
// Package build (after the first npm release, or a packed tarball): import { Field, FieldControl, FieldDescription, FieldError, FieldLabel, Input } from "@sheenui/theme";
// This example also uses input: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/input.json
export function Example() {
const [email, setEmail] = useState("");
const [touched, setTouched] = useState(false);
const invalid = touched && !/^\S+@\S+\.\S+$/.test(email);
return (
<Field required invalid={invalid} className="max-w-sm">
<FieldLabel>Email</FieldLabel>
<FieldControl>
<Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} onBlur={() => setTouched(true)} />
</FieldControl>
<FieldDescription>We only use it to send receipts.</FieldDescription>
<FieldError>Enter a valid email address.</FieldError>
</Field>
);
}Files to copy
4 folders · 3 files · 4 packages
- From
theme/src/ components/field/hooks/use-latest.tslib/{cn.ts, form.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/field.jsonRegistry item: field. The styles need the Tailwind v4 glass UI setup.- The examples also use
components/button/components/calendar/components/checkbox/components/color-picker/components/combobox/components/date-picker/components/drawer/parts/sheet.tsxcomponents/file-upload/components/input/components/number-field/components/otp-input/components/radio-group/components/rating/components/segmented/components/select/components/slider/components/spinner/components/status-icons/components/switch/components/tags-input/hooks/{use-controllable.ts, use-direction.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts, use-virtual-window.ts}motion/{gate.ts, tokens.ts}lib/{field-control-shell.tsx, field-size.ts, glide-highlight.tsx, group-segments.ts, ime.ts, math.ts, motion-props.ts, page-size.ts, popup-a11y.ts, popup-motion.ts, state-light.tsx, warn-once.ts}
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/calendar.json https://lokendra-builds.github.io/sheenui/r/checkbox.json https://lokendra-builds.github.io/sheenui/r/color-picker.json https://lokendra-builds.github.io/sheenui/r/combobox.json https://lokendra-builds.github.io/sheenui/r/date-picker.json https://lokendra-builds.github.io/sheenui/r/file-upload.json https://lokendra-builds.github.io/sheenui/r/input.json https://lokendra-builds.github.io/sheenui/r/number-field.json https://lokendra-builds.github.io/sheenui/r/otp-input.json https://lokendra-builds.github.io/sheenui/r/radio-group.json https://lokendra-builds.github.io/sheenui/r/rating.json https://lokendra-builds.github.io/sheenui/r/segmented.json https://lokendra-builds.github.io/sheenui/r/select.json https://lokendra-builds.github.io/sheenui/r/slider.json https://lokendra-builds.github.io/sheenui/r/switch.json https://lokendra-builds.github.io/sheenui/r/tags-input.json- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Any control
Base UI controls such as Switch join the field on their own. FieldControl wires a native element or your own component and merges with its props (its own disabled, required, aria-invalid and id win). Pass a function to place the props yourself.
Email me about new features.
import { Field, FieldControl, FieldDescription, FieldLabel } from "@/components/sheenui/field/field";
import { Switch } from "@/components/sheenui/switch/switch";
// Package build (after the first npm release, or a packed tarball): import { Field, FieldControl, FieldDescription, FieldLabel, Switch } from "@sheenui/theme";
// This example also uses switch: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/switch.json
export function Example() {
return (
<div className="flex w-full max-w-sm flex-col gap-5">
<Field>
<FieldLabel hint="(optional)">Notifications</FieldLabel>
<Switch />
<FieldDescription>Email me about new features.</FieldDescription>
</Field>
<Field disabled>
<FieldLabel>Sound volume</FieldLabel>
<FieldControl>
{(props) => <input type="range" {...props} />}
</FieldControl>
</Field>
</div>
);
}With react-hook-form
Field takes the error from the form library: Controller hands the control its ref, name, value and onBlur, and the message becomes FieldError's children. The preview below runs the same markup on plain state.
import { useForm, Controller } from "react-hook-form";
import { Button } from "@/components/sheenui/button/button";
import { Field, FieldControl, FieldError, FieldLabel } from "@/components/sheenui/field/field";
import { Input } from "@/components/sheenui/input/input";
// Package build (after the first npm release, or a packed tarball): import { Button, Field, FieldControl, FieldError, FieldLabel, Input } from "@sheenui/theme";
// This example also uses button, input: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/input.json
type Values = { name: string };
export function Example() {
const { control, handleSubmit } = useForm<Values>({ defaultValues: { name: "" } });
return (
<form onSubmit={handleSubmit((values) => console.log(values))} className="flex max-w-sm flex-col gap-4">
<Controller
name="name"
control={control}
rules={{ required: "Name is required." }}
render={({ field, fieldState }) => (
<Field required invalid={fieldState.invalid}>
<FieldLabel>Full name</FieldLabel>
<FieldControl>
<Input {...field} />
</FieldControl>
<FieldError>{fieldState.error?.message}</FieldError>
</Field>
)}
/>
<Button type="submit">Save</Button>
</form>
);
}One size scale for buttons and fields
Input, Select, Combobox, NumberField, DatePicker, TagsInput, Textarea, OtpInput and the color field take size sm (32px), md (40px, default) or lg (48px), and so does Button: one height scale, so a button beside a field lines up at every size. On touch screens (any coarse pointer) sm and md grow to 44px.
import { Button } from "@/components/sheenui/button/button";
import { Combobox } from "@/components/sheenui/combobox/combobox";
import { DatePicker } from "@/components/sheenui/date-picker/date-picker";
import { Input } from "@/components/sheenui/input/input";
import { NumberField } from "@/components/sheenui/number-field/number-field";
import { Select } from "@/components/sheenui/select/select";
// Package build (after the first npm release, or a packed tarball): import { Button, Combobox, DatePicker, Input, NumberField, Select } from "@sheenui/theme";
// This example also uses button, combobox, date-picker, input, number-field, select: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/combobox.json https://lokendra-builds.github.io/sheenui/r/date-picker.json https://lokendra-builds.github.io/sheenui/r/input.json https://lokendra-builds.github.io/sheenui/r/number-field.json https://lokendra-builds.github.io/sheenui/r/select.json
const fruit = [{ value: "apple", label: "Apple" }, { value: "pear", label: "Pear" }];
export function Example() {
return (
<div className="flex flex-col gap-4">
{(["sm", "md", "lg"] as const).map((size) => (
<div key={size} className="flex flex-wrap items-center gap-3">
<Button size={size}>{size}</Button>
<Input size={size} aria-label={`Input ${size}`} placeholder="Input" className="w-36" />
<Select size={size} aria-label={`Select ${size}`} items={fruit} placeholder="Select" className="w-44" />
<Combobox size={size} aria-label={`Combobox ${size}`} options={fruit} placeholder="Combobox" className="w-44" />
<NumberField size={size} aria-label={`Number ${size}`} defaultValue={3} className="w-36" />
<DatePicker size={size} aria-label={`Date ${size}`} className="w-44" />
</div>
))}
</div>
);
}Every control with react-hook-form
One Row helper, one line per control. Each control takes ref, name, onBlur and aria-invalid from the Controller field, so a failed validation focuses the right control; the value prop and the change handler are the only things that differ (checked and onCheckedChange for Checkbox and Switch, a Date or a DateRange for the pickers, an array for TagsInput and FileUpload).
import type { ReactNode } from "react";
import { Controller, useForm, type Control, type ControllerFieldState, type ControllerRenderProps } from "react-hook-form";
import { Button } from "@/components/sheenui/button/button";
import { type DateRange } from "@/components/sheenui/calendar/date-utils";
import { Checkbox } from "@/components/sheenui/checkbox/checkbox";
import { ColorField } from "@/components/sheenui/color-picker/color-picker";
import { Combobox } from "@/components/sheenui/combobox/combobox";
import { DatePicker, DateRangePicker } from "@/components/sheenui/date-picker/date-picker";
import { FileUpload } from "@/components/sheenui/file-upload/file-upload";
import { type FileUploadItem } from "@/components/sheenui/file-upload/parts/model";
import { NumberField } from "@/components/sheenui/number-field/number-field";
import { OtpInput } from "@/components/sheenui/otp-input/otp-input";
import { RadioGroup } from "@/components/sheenui/radio-group/radio-group";
import { Rating } from "@/components/sheenui/rating/rating";
import { SegmentedControl } from "@/components/sheenui/segmented/segmented";
import { Select } from "@/components/sheenui/select/select";
import { Slider } from "@/components/sheenui/slider/slider";
import { Switch } from "@/components/sheenui/switch/switch";
import { TagsInput } from "@/components/sheenui/tags-input/tags-input";
// Package build (after the first npm release, or a packed tarball): import { Button, Checkbox, ColorField, Combobox, DatePicker, DateRangePicker, FileUpload, NumberField, OtpInput, RadioGroup, Rating, SegmentedControl, Select, Slider, Switch, TagsInput, type DateRange, type FileUploadItem } from "@sheenui/theme";
// This example also uses button, calendar, checkbox, color-picker, combobox, date-picker, file-upload, number-field, otp-input, radio-group, rating, segmented, select, slider, switch, tags-input: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json https://lokendra-builds.github.io/sheenui/r/calendar.json https://lokendra-builds.github.io/sheenui/r/checkbox.json https://lokendra-builds.github.io/sheenui/r/color-picker.json https://lokendra-builds.github.io/sheenui/r/combobox.json https://lokendra-builds.github.io/sheenui/r/date-picker.json https://lokendra-builds.github.io/sheenui/r/file-upload.json https://lokendra-builds.github.io/sheenui/r/number-field.json https://lokendra-builds.github.io/sheenui/r/otp-input.json https://lokendra-builds.github.io/sheenui/r/radio-group.json https://lokendra-builds.github.io/sheenui/r/rating.json https://lokendra-builds.github.io/sheenui/r/segmented.json https://lokendra-builds.github.io/sheenui/r/select.json https://lokendra-builds.github.io/sheenui/r/slider.json https://lokendra-builds.github.io/sheenui/r/switch.json https://lokendra-builds.github.io/sheenui/r/tags-input.json
// The value each control holds. Every control takes ref, name, onBlur and aria-invalid, so the Controller
// field spreads on without wrappers; only the change handler and the value prop differ.
type Values = {
fruit: string | null;
city: string | null;
size: string | null;
plan: string;
volume: number;
quantity: number | null;
stars: number | null;
code: string;
tags: string[];
day: Date | null;
stay: DateRange | null;
brand: string;
files: FileUploadItem[];
terms: boolean;
news: boolean;
};
const options = [
{ value: "apple", label: "Apple" },
{ value: "banana", label: "Banana" },
];
type RowProps<K extends keyof Values> = {
name: K;
control: Control<Values>;
render: (field: ControllerRenderProps<Values, K>, state: ControllerFieldState) => ReactNode;
};
function Row<K extends keyof Values>({ name, control, render }: RowProps<K>) {
return <Controller name={name} control={control} render={({ field, fieldState }) => <>{render(field, fieldState)}</>} />;
}
export function Example() {
const { control, handleSubmit } = useForm<Values>({
defaultValues: { fruit: null, city: null, size: null, plan: "month", volume: 30, quantity: null, stars: null, code: "", tags: [], day: null, stay: null, brand: "#7c5cff", files: [], terms: false, news: false },
});
return (
<form onSubmit={handleSubmit((values) => console.log(values))} className="grid max-w-xl gap-4">
<Row name="fruit" control={control} render={(f, s) => <Select label="Fruit" items={options} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="city" control={control} render={(f, s) => <Combobox label="City" options={options} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="size" control={control} render={(f, s) => <RadioGroup label="Size" items={options} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="plan" control={control} render={(f, s) => <SegmentedControl aria-label="Billing" items={[{ value: "month", label: "Monthly" }, { value: "year", label: "Yearly" }]} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="volume" control={control} render={(f, s) => <Slider label="Volume" name={f.name} value={f.value} onValueChange={(v) => f.onChange(v)} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="quantity" control={control} render={(f, s) => <NumberField label="Quantity" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="stars" control={control} render={(f, s) => <Rating label="Rating" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="code" control={control} render={(f, s) => <OtpInput length={4} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="tags" control={control} render={(f, s) => <TagsInput label="Tags" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="day" control={control} render={(f, s) => <DatePicker label="Day" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="stay" control={control} render={(f, s) => <DateRangePicker label="Stay" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="brand" control={control} render={(f, s) => <ColorField label="Brand color" alpha={false} name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="files" control={control} render={(f, s) => <FileUpload label="Attachments" name={f.name} value={f.value} onValueChange={f.onChange} onBlur={f.onBlur} ref={f.ref} aria-invalid={s.invalid} />} />
<Row name="terms" control={control} render={(f, s) => <Checkbox label="I accept the terms" name={f.name} checked={f.value} onCheckedChange={f.onChange} onBlur={f.onBlur} ref={f.ref} error={s.error?.message} />} />
<Row name="news" control={control} render={(f, s) => <Switch label="Send me news" name={f.name} checked={f.value} onCheckedChange={f.onChange} onBlur={f.onBlur} ref={f.ref} error={s.error?.message} />} />
<Button type="submit">Save</Button>
</form>
);
}Field props
Other parts
FieldControl props
FieldError props
FieldLabel props
Label props
All exports on this page
Field— A form field wrapper that wires a label, a description and an error message to any control.FieldControl— Wires the one control inside it to its Field: the element gets id, aria-describedby, aria-invalid, required and disabled.FieldDescription— Muted helper text under the control; its id is added to the control's aria-describedby while it is rendered.FieldError— The error message of a Field.FieldLabel— The label of a Field: Label already pointing at the field's control, with the required asterisk.Label— A text label for a control.groupBlur— Wraps onBlur for a control made of several focusable parts: it fires only when focus leaves the whole group.mergeRefs— Combine refs (callback or object) into one callback ref; undefined entries are skipped.useField— The state of the surrounding Field (invalid, required, disabled, controlId).useFocusProxyRef— A ref for a control made of several focusable parts (a radio group, a rating, an OTP field).useMergedRef— mergeRefs as a hook: returns one stable callback ref that forwards the node to every ref passed in, always the latest ones.
Keyboard
Accessibility
- FieldLabel is a real <label for> pointing at the control, so the control has an accessible name and a larger click target.
- FieldDescription and FieldError ids are added to the control's aria-describedby only while they are rendered, merged with any aria-describedby the control already has.
- FieldError is role="alert", so an error that appears after a submit or a blur is announced; it renders only while the field is invalid.
- The required asterisk is aria-hidden; the control itself gets required, so assistive tech says it once.
- Form libraries: every SheenUI input forwards ref, onBlur, name, required, disabled, aria-invalid and aria-describedby, so Field works with react-hook-form's Controller (pass invalid={fieldState.invalid} and the message as FieldError's children).
When to use the Field and Label
- Use it as a react-hook-form field wrapper around a native input, a Slider or a Switch that does not draw its own label, so the label names the control and the description and error are read together with it.
- Use it with react-hook-form: pass invalid={fieldState.invalid} to Field and put the validation message inside FieldError, and the control gets aria-invalid automatically.
- Use it to build a settings row such as a webhook URL with helper text, where a FieldDescription explains the format and a FieldError appears only after a failed save.
- Skip it when the control already takes label, description and error props; Input, Select and the other SheenUI form components wire those themselves, so wrapping them adds nothing.
- Pair it with FieldControl around a native input or your own component, with Slider or Switch, which join the field on their own, and with the useField hook when writing your own field parts.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .