Field
A label, a control, a hint and an error, wired together with ids.
We only use it for receipts.
Use Field around any form control to get a visible label, optional helper text and an error message, with the id and aria-describedby wiring done for you.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/field.json
Field does not depend on a specific control. Install Input or Textarea alongside it.
Usage
Field takes a function as its child and gives it the props to spread on your control. This keeps the wiring explicit and works in Server Components.
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
<Field label="Email" hint="We only use it for receipts." error={errors.email}>
{(control) => <Input type="email" name="email" {...control} />}
</Field>With an error
We only use it for receipts.
Enter an email like name@example.com.
With a textarea
Up to 500 characters.
With a phone number
For a number that may come from any country, spread the control onto Phone input, which adds a searchable country dropdown.
<Field label="Phone number" hint="Choose your country, then type the number.">
{(control) => <PhoneInput {...control} name="phone" />}
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | – | Visible label text. Required. |
| hint | ReactNode | – | Helper text under the control; linked with aria-describedby. |
| error | ReactNode | – | Error message. Marks the control aria-invalid and is announced (role=alert). |
| required | boolean | – | Passes required to the control and shows an asterisk (hidden from screen readers; the native required state is announced). |
| children | (control: FieldControlProps) => ReactNode | – | Receives id, aria-describedby, aria-invalid and required to spread on your control. |
Field also accepts native <div> props, which are spread on the wrapper.
Accessibility
- The label is a native
<label for>pointing at the control's generated id (useId, so it is stable across server and client). - Hint and error are linked with
aria-describedby; the error also usesrole="alert"so it is announced when it appears. - The control gets
aria-invalidonly while there is an error. - Errors are text, never colour alone.
Tokens used
--foreground, --muted-foreground, --danger.
Known limitations
- It wires one control. Groups such as radio sets need a
fieldsetandlegend, which will come with those components. - It does not validate; pass your own
error.
Source
import { type ComponentProps, type ReactNode, useId } from "react";import { cn } from "@/lib/utils";/** Props to spread onto the form control so it is labelled and described correctly. */export interface FieldControlProps { id: string; "aria-describedby"?: string; "aria-invalid"?: true; required?: boolean;}export interface FieldProps extends Omit<ComponentProps<"div">, "children"> { /** Visible label text. */ label: ReactNode; /** Helper text shown under the control. */ hint?: ReactNode; /** Error message. Presence marks the control invalid and is announced to screen readers. */ error?: ReactNode; required?: boolean; /** Receives the props to spread on your control, e.g. `{(p) => <Input {...p} />}`. */ children: (control: FieldControlProps) => ReactNode;}/** * Label + control + hint + error, wired together with ids. * A render function (not cloneElement) keeps the wiring explicit and Server Component safe. */export function Field({ label, hint, error, required, children, className, ...props }: FieldProps) { const id = useId(); const hintId = hint ? `${id}-hint` : undefined; const errorId = error ? `${id}-error` : undefined; const describedBy = [hintId, errorId].filter(Boolean).join(" ") || undefined; return ( <div className={cn("flex flex-col gap-1.5", className)} {...props}> <label htmlFor={id} className="text-sm font-medium leading-none text-foreground"> {label} {required ? ( <span aria-hidden="true" className="ms-0.5 text-danger"> * </span> ) : null} </label> {children({ id, "aria-describedby": describedBy, "aria-invalid": error ? true : undefined, required, })} {hint ? ( <p id={hintId} className="text-sm text-muted-foreground"> {hint} </p> ) : null} {/* role=alert so a newly appearing error is announced; the id keeps it linked too. */} {error ? ( <p id={errorId} role="alert" className="text-sm font-medium text-danger"> {error} </p> ) : null} </div> );}