# Field (/docs/native/components/field)

<Preview id="field/states/live" title={null} />

## Installation [#installation]

<ComponentInstall name="field" />

## Usage [#usage]

```tsx title="app/index.tsx"
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { useState } from "react";
import { View } from "react-native";

export default function Example() {
  const [email, setEmail] = useState("");

  return (
    <View className="flex-1 justify-center p-4">
      <Field>
        <Field.Label>Email</Field.Label>
        <Input value={email} onChangeText={setEmail} />
        <Field.Description>We only use this to send receipts.</Field.Description>
      </Field>
    </View>
  );
}
```

The four parts together — a label, the control, a description and an error:

<Preview id="field/anatomy/all-four" />

## States [#states]

`isInvalid` and `isDisabled` live on the `Field` and reach the control inside it.

<Preview id="field/states/one-flag-three-things" />

```tsx
<Field isInvalid>
  <Field.Label>Email</Field.Label>
  <Input value={email} onChangeText={setEmail} />
  <Field.Error>{error}</Field.Error>
</Field>
```

The label turns destructive, and so does the `Input` — with nothing said on the input itself. A control
opts out explicitly: `<Input isInvalid={false} />`.

## Orientation [#orientation]

Use `orientation` to stack the label over the control or put them side by side, with
`Field.Content` grouping a label and its description as one block.

<Preview id="field/orientation/horizontal-with-a-description" />

```tsx
<Field orientation="horizontal">
  <Field.Content>
    <Field.Label>Accept the terms</Field.Label>
    <Field.Description>You can withdraw consent at any time.</Field.Description>
  </Field.Content>
  <Checkbox isChecked={accepted} onCheckedChange={setAccepted} />
</Field>
```

Once a control inside offers a press, the field becomes a `Pressable` and the whole row drives it —
tapping the label, or the description under it, ticks the checkbox.

## Sets and groups [#sets-and-groups]

Use `Field.Set` for a named section of a form and `Field.Group` for the fields under it.

<Preview id="field/grouping/set-and-legend" />

```tsx
<Field.Set>
  <Field.Legend>Notifications</Field.Legend>
  <Field.Description>Choose what reaches you, and how.</Field.Description>

  <Field.Group>
    <Field orientation="horizontal">
      <Field.Label>Email</Field.Label>
      <Checkbox isChecked={email} onCheckedChange={setEmail} />
    </Field>
    <Field orientation="horizontal">
      <Field.Label>Push</Field.Label>
      <Checkbox isChecked={push} onCheckedChange={setPush} />
    </Field>
  </Field.Group>
</Field.Set>
```

`Field.Legend` takes `variant="label"` for a nested set, so it matches the fields under it rather
than titling them.

## Separators [#separators]

A labelled separator draws two rules with the label between them.

<Preview id="field/grouping/separator" />

```tsx
<Field.Separator />
<Field.Separator>or</Field.Separator>
```

## Errors [#errors]

`Field.Error` renders nothing when it has no children, so the line removes itself once the value is
fixed.

<Preview id="field/states/live" />

```tsx
<Field.Error>{error}</Field.Error>
```

## Reading the field's state [#reading-the-fields-state]

Use `useField()` to read the field from a control inside it, without prop drilling.

```tsx
import { useField, useFieldContext } from "@delacour/react-native-ui/field";

const { isInvalid, isDisabled, orientation } = useField();
```

`useField()` throws outside a `<Field>`. Use `useFieldContext()` where the field is optional — as
every control does.

## API Reference [#api-reference]

### Anatomy [#anatomy]

<TypeTable
  type={{
    "Field": {
      type: "FieldProps",
      description: "One field. Owns isInvalid, isDisabled and orientation, and becomes a Pressable once a control inside registers a press.",
    },
    "Field.Set": {
      type: "ViewProps",
      description: "A named section of a form — a legend, a description, and the fields under them.",
    },
    "Field.Legend": {
      type: "FieldLegendProps",
      description: 'The title of a Field.Set. variant="label" for a nested one.',
    },
    "Field.Group": {
      type: "ViewProps",
      description: "A stack of fields, spaced so two never read as one. Inserts no dividers.",
    },
    "Field.Label": {
      type: "FieldTextProps",
      description: "The control's name. Renders Text.Label and turns destructive with the control when the field is invalid.",
    },
    "Field.Description": {
      type: "FieldTextProps",
      description: "Supporting copy. Renders Text.Caption and stays muted in every state.",
    },
    "Field.Error": {
      type: "FieldTextProps",
      description: "What is wrong with the value. Renders Text.Caption on the destructive token, and renders nothing when it has no children.",
    },
    "Field.Content": {
      type: "ViewProps",
      description: "A label and its description as one block, for a horizontal field.",
    },
    "Field.Separator": {
      type: "ViewProps",
      description: "A rule between sections, optionally labelled between two rules.",
    },
  }}
/>

### FieldProps [#fieldprops]

React Native's `ViewProps`, plus:

<TypeTable
  type={{
    orientation: {
      type: '"vertical" | "horizontal"',
      default: '"vertical"',
      description: "vertical stacks label over control; horizontal puts them side by side.",
    },
    isInvalid: {
      type: "boolean",
      default: "false",
      description: "Reports an invalid value. The label and the control inside both turn destructive.",
    },
    isDisabled: {
      type: "boolean",
      default: "false",
      description: "The control inside is unavailable. Fades the label and disables the control.",
    },
    className: { type: "string" },
  }}
/>

### FieldTextProps [#fieldtextprops]

`TextProps` with `variant` omitted — each part already is one, and naming a second would let a
caller turn a description into a heading and lose the type scale the component exists to keep.

### FieldLegendProps [#fieldlegendprops]

`FieldTextProps`, plus:

<TypeTable
  type={{
    variant: {
      type: '"legend" | "label"',
      default: '"legend"',
      description: "legend titles a set; label matches the fields under it.",
    },
  }}
/>

### useField() [#usefield]

<TypeTable
  type={{
    isInvalid: { type: "boolean", required: true },
    isDisabled: { type: "boolean", required: true },
    orientation: { type: '"vertical" | "horizontal"', required: true },
    registerPress: {
      type: "(onPress: (() => void) | null) => void",
      required: true,
      description: "A control inside offers its press here so the whole row can drive it. Pass null on unmount. A field holds one control, so a second registration replaces the first.",
    },
    label: {
      type: "string | null",
      required: true,
      description: "The Field.Label's text, for a control with no text of its own to read as its accessible name. null until a string label registers.",
    },
    registerLabel: {
      type: "(label: string | null) => void",
      required: true,
      description: "Field.Label offers its text here. Pass null on unmount. A field holds one label, so a second registration replaces the first.",
    },
  }}
/>

### Exports [#exports]

```tsx
import {
  FIELD_ORIENTATIONS,
  FIELD_LEGEND_VARIANTS,
  FIELD_TEXT_PARTS,
  fieldVariants,
  resolveFieldTextColor,
  resolveFieldInteractive,
  resolveFieldLabelText,
} from "@delacour/react-native-ui/field";
```
