Skip to content

The open alternative to Salesforce, designed for AI.

6.5 KB
Raw Blame
---
title: Field
description: Connect a form control to its label, description, and validation feedback.
---

import { PageBanner } from "/snippets/page-banner.mdx"

import { StoryEmbed } from '/snippets/ui/StoryEmbed.mdx';
import FieldReference from '/snippets/ui/generated/input/field.mdx';

## Overview

Use `Field` to group one control with its accessible label and helper text. Components such as [Input](/ui/primitives/input/input) integrate with the field directly.

<StoryEmbed
  storyId="ui-input-field--display"
  title="Field example"
  height={180}
/>

## Usage

```tsx
import { Field, Input } from 'twenty-ui/primitives/input';

export const EmailField = () => (
  <Field.Root name="email" validationMode="onBlur">
    <Field.Label>Work email</Field.Label>
    <Input type="email" required placeholder="[email protected]" />
    <Field.Description>
      We send account updates to this address.
    </Field.Description>
    <Field.Error match="valueMissing">Enter your email address.</Field.Error>
    <Field.Error match="typeMismatch">Enter a valid email address.</Field.Error>
  </Field.Root>
);
```

`Field.Root` manages the control's name, disabled state, and validity. `Field.Label` and `Field.Description` connect their content to the control. Set validation timing with `validationMode`; use `validate` for custom validation.

## Anatomy

Place one control and its supporting parts inside `Field.Root`. This tree shows a field built with the styled `Input` component.

```text
Field.Root
├── Field.Label
├── Input
├── Field.Description
└── Field.Error
```

| Part                                  | Requirement         | Purpose                                                                      |
| ------------------------------------- | ------------------- | ---------------------------------------------------------------------------- |
| `Root`                                | Required            | Shares the name, disabled state, and validation with the control.            |
| `Item`                                | Optional for groups | Associates each radio or checkbox with its own label and description.        |
| `Label`                               | Recommended         | Supplies a visible, associated label. An accessible name is always required. |
| `Input` or another integrated control | Required            | Receives user input and connects to the field.                               |
| `Control`                             | Alternative control | Provides an unstyled input when you do not use an integrated control.        |
| `Description`                         | Optional            | Adds persistent helper text associated with the control.                     |
| `Error`                               | Optional            | Shows feedback for a matching validation state.                              |
| `Validity`                            | Optional            | Exposes validity through a children render function.                         |

`Input` and `Textarea` already integrate with the field. Place them directly inside `Root`; they supply the control behavior themselves. Compound controls such as [Select](/ui/primitives/input/select#compose-with-a-field) keep their own root and parts inside the field.

## External errors

Use `invalid` when your application owns the error state. An explicit `match` makes the error message visible.

```tsx
import { Field, Input } from 'twenty-ui/primitives/input';

type UsernameFieldProps = { error?: string };

export const UsernameField = ({ error }: UsernameFieldProps) => (
  <Field.Root name="username" invalid={Boolean(error)}>
    <Field.Label>Username</Field.Label>
    <Input autoComplete="username" />
    <Field.Error match={Boolean(error)}>{error}</Field.Error>
  </Field.Root>
);
```

Keep error messages actionable and provide a visible label even when the control has a placeholder.

## Grouped controls

Use `Field.Item` inside a radio or checkbox group to associate each choice with its own label and description. Keep the group's label outside the items. An item can be disabled independently; a disabled `Field.Root` disables every item.

<StoryEmbed
  storyId="ui-input-field--grouped-items"
  title="Field items"
  height={220}
/>

```tsx
import { Field, Radio, RadioGroup } from 'twenty-ui/primitives/input';
import { Text } from 'twenty-ui/primitives/typography';

export const NotificationsField = () => (
  <Field.Root name="notifications">
    <Field.Label>Notifications</Field.Label>
    <RadioGroup defaultValue="important" style={{ gap: 16, marginTop: 12 }}>
      <Field.Item style={{ display: 'flex', alignItems: 'start', gap: 12 }}>
        <Radio value="important" />
        <Text>
          <Field.Label>Important updates</Field.Label>
          <Field.Description>
            Only messages that need your attention
          </Field.Description>
        </Text>
      </Field.Item>
      <Field.Item style={{ display: 'flex', alignItems: 'start', gap: 12 }}>
        <Radio value="all" />
        <Text>
          <Field.Label>All updates</Field.Label>
          <Field.Description>Every change to your records</Field.Description>
        </Text>
      </Field.Item>
      <Field.Item
        disabled
        style={{ display: 'flex', alignItems: 'start', gap: 12 }}
      >
        <Radio value="digest" />
        <Text>
          <Field.Label>Daily digest</Field.Label>
          <Field.Description>Coming soon</Field.Description>
        </Text>
      </Field.Item>
    </RadioGroup>
  </Field.Root>
);
```

## Native integration and types

Field preserves Base UI validation timing, custom validation, controlled and uncontrolled control values, and value-change event details. `Validity` exposes the validation result through its children function. Integrated controls retain their native form behavior.

`Root`, `Item`, `Label`, `Control`, `Description`, and `Error` forward native props, refs, and `render` composition. Their default elements are a div, div, label, input, paragraph, and div respectively. `Validity` does not render its own element.

Import `FieldRootProps`, `FieldItemProps`, `FieldLabelProps`, `FieldControlProps`, `FieldDescriptionProps`, `FieldErrorProps`, and `FieldValidityProps` from `twenty-ui/primitives/input` for the corresponding upstream part contracts.

## Props

The reference is generated from the public component types. Native attributes, including accessible names and event handlers, are also accepted on parts that render elements.

<FieldReference />

<PageBanner illustration="page-cover-input" />