# Field

A label, hint and error row around any control, wired for screen readers; plus the label, description and error parts.

- Family: form
- Import: `import { Field, FieldLabel, FieldDescription, FieldError } from "@senvenli/react/form";`
- Page: https://design.senvenli.com/docs/components/field
- JSON: https://design.senvenli.com/docs/components/field.json

## Example: Around a native control

The row gives a control the library does not provide its label and hint, and wires both to it.

```tsx
import { Field } from "@senvenli/react/form";

export default function Example() {
  return (
    <Field label="Reminder" htmlFor="reminder-days" description="Days before the due date that we email the customer.">
      <input id="reminder-days" type="range" min={1} max={14} defaultValue={3} className="block w-full" />
    </Field>
  );
}
```

## Examples

### Around an input group

The label, hint and error reach the group's input, not its frame.

```tsx
import { Field, InputAddon, InputGroup, InputGroupInput } from "@senvenli/react/form";

export default function Example() {
  return (
    <Field label="Budget" htmlFor="budget" description="Monthly, before tax." errorMessage="Enter a budget of at least $50.">
      <InputGroup>
        <InputAddon position="start">$</InputAddon>
        <InputGroupInput inputMode="decimal" defaultValue="20" />
        <InputAddon position="end">USD</InputAddon>
      </InputGroup>
    </Field>
  );
}
```

### With an error

The error replaces the hint and marks the control invalid; a library control draws that state itself.

```tsx
import { Field, Input } from "@senvenli/react/form";

export default function Example() {
  return (
    <Field label="Support line" isRequired errorMessage="Enter the full number with its area code: (415) 555-0132.">
      <Input type="tel" name="support-phone" defaultCountry="US" suggestedCountries={["US", "CA", "GB"]} defaultValue="+1415555" />
    </Field>
  );
}
```

## Guidelines

### When to use it

- Wrapping a control the library does not provide, so it matches the other fields.

### Use something else when

- [input](https://design.senvenli.com/docs/components/input.md): A text input is enough; it has the field row built in.

## Accessibility

Built in:

- Label, hint and error are linked to the control with htmlFor, aria-describedby and aria-invalid.

You provide:

- A control with a matching `id`, or a single child Field can give one to.

## API reference

### Field

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `ReactNode` |  | no |  |
| htmlFor | `string` |  | no | Control id for the label. Optional: when omitted, Field generates one and injects it onto a single-element child. Field is for a control the library does not provide (a native input, a third-party widget) and for `InputGroup` with `InputGroupInput`, which forwards it to its input. The library's own fields (TextField, Select…) carry their own label, hint and error, so they do not go inside a Field. |
| errorMessage | `ReactNode` |  | no | What went wrong and what to do; shown under the control and announced. |
| description | `ReactNode` |  | no | A line of help under the control (hidden while there is an error). |
| isRequired | `boolean` |  | no |  |
| requiredClassName | `string` |  | no | Class for the required "*" asterisk. Defaults to the danger text token; a brand marketing form can override it without forking this component. |
| children | `ReactNode` |  | yes |  |
| className | `string` |  | no |  |

### FieldLabel

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes |  |
| isRequired | `boolean` |  | no | Shows `*` in `ds-text-danger` (hidden from screen readers: the control is `aria-required`). |
| isOptional | `boolean` |  | no | Shows "(optional)" in the provider locale; use it when most fields of the form are required. |

### FieldDescription

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes |  |

### FieldError

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | no |  |

### Tokens it reads

`--ds-background-disabled`, `--ds-background-input`, `--ds-background-input-hovered`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-border`, `--ds-border-bold`, `--ds-border-danger`, `--ds-border-focused`, `--ds-easing-standard`, `--ds-icon-subtle`, `--ds-radius-control-medium`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-size-control`, `--ds-size-control-compact`, `--ds-size-control-large`, `--ds-size-icon`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-surface-sunken`, `--ds-text`, `--ds-text-danger`, `--ds-text-disabled`, `--ds-text-subtle`, `--ds-text-subtlest`

## Related

- [input](https://design.senvenli.com/docs/components/input.md)
- [form](https://design.senvenli.com/docs/components/form.md)
