Field

Wraps a single control with its label, helper text and error message, and wires them together — htmlFor, aria-describedby, aria-invalid, required and disabled — through context.

StableSource

We only use it for receipts.

Import

tsx
import { Field } from "@merid/react";

Examples

Error

When error is set, the control gets aria-invalid="true" and the invalid style, and the message is appended to its aria-describedby. Pass undefined, null or false to clear it.

That username is taken.

Controlled input

Field does not own the value. Control the child as usual and derive error from your state.

Shown in the sidebar.

Uncontrolled input

With defaultValue and name, the control keeps its own state and submits with the form.

Disabled

disabled disables the control and dims the label.

Which controls read Field

Input, Textarea and NativeSelect read the full wiring. Props set on the control itself always win over the Field's.

  • Checkbox is named by the Field label. Its own children still render as a clickable label but become its description (aria-describedby), so it is never double-labelled.
  • Switch takes the Field id (so the Field label names it), aria-describedby, aria-invalid and disabled.
  • RadioGroup is named by the Field label (aria-labelledby) and picks up its description, error, required and disabled.

Every control follows the same convention: aria-invalid carries the semantics and data-invalid drives the styling.

API reference

Field renders a <div>, forwards its ref and passes other div attributes through.

PropTypeDefaultDescription
labelRequiredReactNode–Visible label, wired to the control via htmlFor.
childrenRequiredReactNode–A single Merid control; it picks up the wiring from context.
descriptionReactNode–Helper text below the control, linked via aria-describedby.
errorReactNode–Error message. Sets aria-invalid on the control and links the message.
requiredbooleanfalseMarks the control required and shows the visual marker.
disabledbooleanfalseDisables the control and dims the label.
idstring–Explicit id for the control. Generated when omitted.

For custom controls, useFieldContext() returns the wiring (controlId, labelId, descriptionId, errorId, invalid, required, disabled) or null outside a Field.

Styling

HookValues
.mrd-fieldRoot <div>
.mrd-field__descriptionHelper text
.mrd-field__errorError message
data-invalidPresent when error is set
data-disabledPresent when disabled

The label is a Label (.mrd-label).

Accessibility

  • The label names the control; the description and error are read after it through aria-describedby.
  • The error is not a live region. When validating on submit, move focus to the first invalid control so the message is announced.
  • required is set on the control, so it is announced and native validation applies.

Guidelines

Do

Write errors that say how to fix the problem: “Use at least 3 characters.”

Avoid

Show “Invalid input”, or put more than one control inside a Field.