Skip to content

Forms

Field

tsx
"use client";
import { Field, Input } from "@cherry-design-system/react";

export function Username({ error }: { error?: string }) {
  return (
    <Field label="Username" required error={error}>
      <Input name="username" />
    </Field>
  );
}

Label, control and hint or error in one wrapper. Field generates the control's id, links the label to it, points aria-describedby at the hint or error, and passes invalid, disabled and required down to the control. Wrap every form control in a Field unless it has its own visible label.

Examples

With a hint

The hint shows under the control and is hidden while an error shows.

tsx
"use client";
import { Field, Input } from "@cherry-design-system/react";
import { Mail } from "lucide-react";

export function Email() {
  return (
    <Field label="Email" hint="We never share it.">
      <Input type="email" leadingIcon={<Mail />} placeholder="you@example.com" />
    </Field>
  );
}

Error state

error replaces the hint, marks the control aria-invalid and switches it to its error style.

tsx
"use client";
import { Field, Password } from "@cherry-design-system/react";

export function NewPassword() {
  return (
    <Field label="Password" hint="At least 12 characters." error="Too short.">
      <Password autoComplete="new-password" />
    </Field>
  );
}

Disabled and required

tsx
"use client";
import { Field, Select } from "@cherry-design-system/react";

export function Region() {
  return (
    <Field label="Region" required disabled>
      <Select placeholder="Select a region">
        <option value="eu">Europe</option>
        <option value="us">United States</option>
      </Select>
    </Field>
  );
}

Group controls

For a RadioGroup or an OTPInput, Field's label names the whole group through aria-labelledby.

tsx
"use client";
import { Field, Radio, RadioGroup } from "@cherry-design-system/react";

export function Plan() {
  return (
    <Field label="Plan">
      <RadioGroup name="plan" defaultValue="pro">
        <Radio value="free" label="Free" />
        <Radio value="pro" label="Pro" />
      </RadioGroup>
    </Field>
  );
}

Custom controls

useFieldControl returns the props a control inside a Field should spread: id, aria-describedby, aria-invalid, disabled and required. Explicit props on the control win.

tsx
"use client";
import type { InputHTMLAttributes } from "react";
import { useFieldControl } from "@cherry-design-system/react";

export function ColorInput(props: InputHTMLAttributes<HTMLInputElement>) {
  const control = useFieldControl(props);
  return <input type="color" {...control} />;
}

Properties

labelReactNoderequired

Visible label, rendered as a <label> linked to the control.

childrenReactNoderequired

The control.

hintReactNode

Helper text under the control. Hidden while an error shows.

errorReactNode

Error message. Marks the control aria-invalid and replaces the hint.

idstring

The control's id. Generated when omitted.

disabledboolean

Disables the control. Defaults to false.

requiredboolean

Sets the native required attribute on the control and shows an asterisk. Defaults to false.

Renders a <div>, forwards its ref and passes through native <div> attributes. Also exported: FieldContext and useFieldControl.

Styling

Root class cherry-field with state attributes data-invalid and data-disabled. Parts: cherry-field__label, cherry-field__required, cherry-field__hint, cherry-field__error.

Reads --cherry-fg-default (label), --cherry-fg-muted (hint) and --cherry-danger-fg (error).

Accessibility

Links the label to the control and the hint or error through aria-describedby; error sets aria-invalid on the control. required uses the native required attribute, not only the asterisk (which is hidden from screen readers). Works with Input, Textarea, Select, Password, NumberField, Combobox, OTPInput, Checkbox, RadioGroup, Range, Dropzone, AvatarDropzone and the date and time pickers.