# Field

A form field that wires its label, control, description and error messages together. Field generates the ids and sets `for`, `aria-describedby` and `aria-invalid`, so you don’t have to. Built on Base UI Field; use [Label and Input](https://design-system.blocks-ui.com/components/inputs.md) when you want to wire native controls yourself.

## Import

From a Server Component, import each part by name. In a `'use client'` file, `Field.Label`, `Field.Control`, `Field.Description` and `Field.Error` are the same parts.

```tsx
import { Field, FieldControl, FieldDescription, FieldError, FieldLabel } from '@blocks/design-system'
```

## Validation

`FieldControl` keeps the native constraint attributes (`required`, `min`, `max`, `pattern`, `type`). Add a `FieldError` per constraint with `match` (`valueMissing`, `rangeOverflow`, `patternMismatch`, and so on), or one without `match` to show the browser’s own message.

Base UI validates on submit only inside its `<Form>`. With a native form, set `validationMode="onBlur"` (or `"onChange"`) on Field so the message appears as the reader leaves the control; the browser still blocks an invalid submit.

**Example: Station elevation** (`examples/field/station-elevation.tsx`)

```tsx
// A Server Component: the named Field parts are client components that share
// one context, and the form itself is native.
import { Button, Field, FieldControl, FieldDescription, FieldError, FieldLabel, Stack } from '@blocks/design-system'

export default function StationElevation() {
  return (
    <Stack as="form" action="/components/field" method="get" gap="md" aria-label="Station details">
      <Field name="elevation" validationMode="onBlur">
        <FieldLabel>Elevation (ft)</FieldLabel>
        <FieldControl type="number" required min={0} max={20000} defaultValue="5370" />
        <FieldDescription>Measured at the snow pillow, between 0 and 20,000 ft.</FieldDescription>
        <FieldError match="valueMissing">Enter the station’s elevation.</FieldError>
        <FieldError match="rangeOverflow">Elevation must be 20,000 ft or less.</FieldError>
        <FieldError match="rangeUnderflow">Elevation can’t be negative.</FieldError>
      </Field>
      <div>
        <Button type="submit" variant="primary">
          Save station
        </Button>
      </div>
    </Stack>
  )
}
```

## Controlled, custom validation

In client-authored code, control the value with `value` and `onValueChange`, and validate with `validate`, which returns the message or `null`. `render` swaps the input for another control, here a Textarea.

**Example: Notes with a limit** (`examples/field/client-dot-api.tsx`)

```tsx
'use client'

import { Field, Textarea } from '@blocks/design-system'
import { useState } from 'react'

export default function ClientDotApi() {
  const [notes, setNotes] = useState('')
  return (
    <Field name="notes" validationMode="onChange" validate={(value) => (String(value).length > 80 ? 'Keep notes to 80 characters.' : null)}>
      <Field.Label>Observer notes</Field.Label>
      <Field.Control render={<Textarea rows={3} />} value={notes} onValueChange={(value) => setNotes(String(value))} />
      <Field.Description>{80 - notes.length} characters left.</Field.Description>
      <Field.Error />
    </Field>
  )
}
```

## Disabled

`disabled` on Field disables the control and dims the label.

**Example: Disabled** (`examples/field/disabled-field.tsx`)

```tsx
import { Field, FieldControl, FieldDescription, FieldLabel } from '@blocks/design-system'

export default function DisabledField() {
  return (
    <Field disabled>
      <FieldLabel>Station ID</FieldLabel>
      <FieldControl defaultValue="651:OR:SNTL" />
      <FieldDescription>Assigned by the network; it can’t be changed.</FieldDescription>
    </Field>
  )
}
```

## Props

**`Field` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `actionsRef` | `RefObject<FieldRootActions \| null>` | — | A ref to imperative actions. - `validate`: Validates the field when called. |
| `dirty` | `boolean` | — | Whether the field's value has been changed from its initial value. Useful when the field state is controlled by an external library. |
| `disabled` | `boolean` | `false` | Whether the component should ignore user interaction. Takes precedence over the `disabled` prop on the `<Field.Control>` component. |
| `invalid` | `boolean` | — | Whether the field is invalid. Useful when the field state is controlled by an external library. |
| `name` | `string` | — | Identifies the field when a form is submitted. Takes precedence over the `name` prop on the `<Field.Control>` component. |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, FieldRootState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
| `touched` | `boolean` | — | Whether the field has been touched. Useful when the field state is controlled by an external library. |
| `validate` | `((value: unknown, formValues: Record<string, any>) => string \| void \| string[] \| Promise<string \| void \| string[] \| null> \| null)` | — | A function for custom validation. Return a string or an array of strings with the error message(s) if the value is invalid. Returning nothing, `null`, an empty string, or an empty array means the value is valid. Asynchronous functions are supported, but they do not prevent form submission when using `validationMode="onSubmit"`. |
| `validationDebounceTime` | `number` | `0` | How long to wait between `validate` callbacks if `validationMode="onChange"` is used. Specified in milliseconds. |
| `validationMode` | `"onBlur" \| "onChange" \| "onSubmit"` | `'onSubmit'` | Determines when the field should be validated. This takes precedence over the `validationMode` prop on `<Form>`. - `onSubmit`: triggers validation when the form is submitted, and re-validates on change after submission. - `onBlur`: triggers validation when the control loses focus. - `onChange`: triggers validation on every change to the control value. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`FieldLabel` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `nativeLabel` | `boolean` | `true` | Whether the component renders a native `<label>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a label (for example, `<div>`). This is useful to avoid inheriting label behaviors on `<button>` controls (such as `<Select.Trigger>` and `<Combobox.Trigger>`), including avoiding `:hover` on the button when hovering the label, and preventing clicks on the label from firing on the button. |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, FieldLabelState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`FieldControl` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `defaultValue` | `string \| number \| readonly string[]` | — | The initial value. Use when uncontrolled. |
| `onValueChange` | `((value: string, eventDetails: { … }) => void)` | — | Callback fired when the `value` changes. Use when controlled. |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, FieldControlState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`FieldDescription` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, FieldDescriptionState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

**`FieldError` props**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `match` | `boolean \| keyof ValidityState` | — | Determines whether to show the error message according to the field's [ValidityState](https://developer.mozilla.org/en-US/docs/Web/API/ValidityState). Specifying `true` will always show the error message, and lets external libraries control the visibility. |
| `render` | `ReactElement<unknown, string \| JSXElementConstructor<any>> \| ComponentRenderFn<HTMLProps, FieldErrorState>` | — | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

Also accepts [styling escape hatches](https://design-system.blocks-ui.com/components/box.md#escape-hatches) and [a ref](https://design-system.blocks-ui.com/components/box.md#refs), and the native attributes of the element it renders.

## Accessibility

- The label, description and error are announced with the control. Keep errors specific: say what to change.
- Errors appear only after the reader leaves the field or submits, not while they are still typing the first value.
- Checkbox, Switch, RadioGroup and Select inside a Field join it automatically.
