Skip to content
ComponentsMarkdown

Field

Form field wrapper that associates a label, helper description, and validation error with its child control via React context. Generates a stable useId identifier and wires htmlFor, aria-describedby, aria-invalid, and aria-required automatically.

Live example

Clear the input to test automatic error wiring.

Props

PropTypeDefaultDescription
labelReactNoderequiredVisible label text bound to the control via htmlFor.
descriptionReactNodeundefinedOptional helper text linked via aria-describedby.
errorReactNodeundefinedError message rendered with role='alert', linked via aria-describedby, and setting aria-invalid.
requiredbooleanfalseDisplays a required marker and sets required and aria-required on the child control.
idstringuseId()Optional explicit control id overriding the generated useId value.
childrenReactNoderequiredForm control such as Input or Textarea that reads Field context.

Sizes and tokens

Tier / RoleTokenValue
Stack spacinggap-2 (--spacing-2)8px vertical gap between label, control, description, and error
Label typographytext-caption (--text-caption) + --foreground12px / 16px medium weight
Description texttext-caption + --muted-foreground12px / 16px neutral step 11
Error texttext-caption + --destructive-text12px / 16px red step 11 with role='alert'

States

Visible in audit logs.

Must use HTTPS.

Code

import { Field, Input } from "@freshgemui/monoplinth";

export function Example({ error }: { error?: string }) {
  return (
    <Field
      label="Workspace slug"
      description="Lowercase letters and hyphens only."
      error={error}
      required
    >
      <Input placeholder="monoplinth" />
    </Field>
  );
}

Do and don't

Do: Always provide visible error text when validation fails
Pass a concrete error string to Field so screen readers announce the failure and users never rely on border color alone.
Don't: Hardcode matching ids by hand or hide labels visually
Do not wire htmlFor and aria-describedby manually for every input when Field handles id generation and state attributes.

Accessibility

  • Generates a deterministic useId and connects <label htmlFor> to the child control's id.
  • Joins both description and error element ids into aria-describedby so assistive technology reads guidance and errors together.
  • Sets aria-invalid="true" on the child control only when error is present, and renders error text in a role="alert" paragraph so state never relies on color alone (WCAG 1.4.1).
  • Sets both required and aria-required="true" on the child control when required is true.

Rationale

Centralizing id generation and ARIA attribute wiring in Field prevents mismatched htmlFor or aria-describedby references across forms while keeping Input and Textarea composable on their own.