Client component

Field

The form-field wrapper: label wiring, help text, error messages, and state that flows into any input.

We’ll never share your email.

Import#

import { Field } from "astralis-ui";
// Field.Label, Field.HelpText, Field.ErrorText
// useFieldContext, for your own controls

Usage#

Field is context, not chrome: it generates an id, wires Field.Label to the input, appends the required *, and pushes invalid, disabled, required and readOnly down to any Astralis input inside: Input, Checkbox, Radio, Switch, Select, Slider… no prop repetition.

Validation state#

Flip one invalid flag on the Field and everything reacts: the input border, and your swap from HelpText to ErrorText.

Props#

PropTypeDefaultDescription
invalidbooleanfalseError styling + `aria-invalid` on the child input.
disabledbooleanfalseDisables the child input.
requiredbooleanfalseAppends `*` to the label, sets `aria-required`.
readOnlybooleanfalseRead-only state.
idstringautoExplicit id for the label/input pair.

Field.Label, Field.HelpText and Field.ErrorText accept their native element attributes.

Custom controls#

useFieldContext() gives a control of your own the same wiring the built-in inputs get. It returns null outside a Field, so the control still works on its own.

import { Field, useFieldContext } from "astralis-ui";

function ColorInput(props: React.ComponentProps<"input">) {
  const field = useFieldContext();
  return (
    <input
      type="color"
      id={field?.id}
      aria-describedby={field?.describedBy}
      aria-invalid={field?.invalid || undefined}
      disabled={field?.disabled}
      required={field?.required}
      {...props}
    />
  );
}

<Field required>
  <Field.Label>Brand color</Field.Label>
  <ColorInput />
  <Field.HelpText>Used for buttons and links.</Field.HelpText>
</Field>
ValueTypeDescription
idstringThe id Field.Label points at. Put it on the focusable element.
describedBystring | undefinedIds of the help and error text currently shown, for aria-describedby.
labelledBystring | undefinedId of the rendered label, for controls a <label for> can't name, such as a radio group.
invalid · disabled · required · readOnlyboolean | undefinedThe Field's state flags.

Accessibility#

  • The label's htmlFor and the input's id are wired automatically.
  • Field.ErrorText renders with role="alert" and aria-live="polite", so screen readers announce validation failures as they happen.