# Field

Encapsulate input elements with labels, descriptions, and validation.

```tsx
import { Field, Input } from '@fluxui-dev/react';

<Field name="email">
    <Field.Label>Email</Field.Label>

    <Input type="email" />

    <Field.Error />
</Field>
```

## Shorthand props

Because using the field component in its full form can be verbose and repetitive, all form controls in Flux allow you to pass a `label` and a `description` prop directly. Under the hood, they will be wrapped in a field with an error component automatically.

```tsx
<Input name="email" label="Email" type="email" />
```

## Automatic validation errors

Every field shows the validation error for its `name`, from the surrounding Inertia `<Form>` or the page's errors. See the [Inertia guide](https://fluxui.dev/react/docs/inertia).

```tsx
<Form {...store.form()}>
    <Input label="Email" type="email" name="email" />

    <Input label="Password" type="password" name="password" />
</Form>
```

## Customizing field parts

Wrap the control in `Field` and write `Field.Label`, `Field.Description`, or `Field.Error` yourself to pass them props such as `srOnly` or `className`; the field still looks up the validation error for its `name`.

```tsx
import { Field, Otp } from '@fluxui-dev/react';

<Field name="code" error="The code field must be 6 characters.">
    <Field.Label srOnly>Authentication code</Field.Label>

    <Otp name="code" length={6} className="mx-auto" />

    <Field.Error className="text-center" />
</Field>
```

## Spacing a field

A control's `className` stays on the control when `label` is set, so a margin lands between the label and the control, as in Flux for Blade. To space the whole field, write the `Field` yourself and pass it `className`.

```tsx
<Field name="email" className="mt-6">
    <Field.Label>Email</Field.Label>

    <Input type="email" />

    <Field.Error />
</Field>
```

## With error

Pass a message to the `error` prop to mark the control invalid and display it in `Field.Error`, taking precedence over the error looked up by `name`.

```tsx
<Field error="The email field must be a valid email address.">
    <Field.Label>Email</Field.Label>

    <Input type="email" defaultValue="caleb@" />

    <Field.Error />
</Field>
```

## With trailing description

Position the field description directly below the input, or pass `descriptionTrailing` to the control.

```tsx
<Field name="password">
    <Field.Label>Password</Field.Label>

    <Input type="password" />

    <Field.Error />

    <Field.Description>Must be at least 8 characters long, include an uppercase letter, a number, and a special character.</Field.Description>
</Field>
```

## With badge

Badges allow you to enhance a field with additional information such as being "required" or "optional" when it might not be expected.

```tsx
<Field name="email">
    <Field.Label badge="Required">Email</Field.Label>

    <Input type="email" required />

    <Field.Error />
</Field>

<Field name="phone">
    <Field.Label badge="Optional">Phone number</Field.Label>

    <Input type="tel" placeholder="(555) 555-5555" />

    <Field.Error />
</Field>
```

## Split layout

Display multiple fields horizontally in the same row.

```tsx
<div className="grid grid-cols-2 gap-4">
    <Input label="First name" placeholder="River" />

    <Input label="Last name" placeholder="Porzio" />
</div>
```

## Reference

### Field

A container component that provides structure for form inputs with labels, descriptions, and error messages.

**Prop:**

- `variant` - Visual style variant. Options: `block`, `inline`, `bare` (no spacing). Default: `block`.
- `name` - Field name used to name the control inside and look up its validation error from `FluxProvider`'s `errors`.
- `error` - Error message that marks the control invalid and renders in `Field.Error`, defaulting to the validation error for the field's `name`.
- `invalid` - Marks the control invalid without an error message, taking precedence over `error`.
- `disabled` - Disables the control inside the field and dims its label.
- `className` - CSS classes applied to the root element, which wraps the label, control, and error, such as a margin around the whole field.

**Slot:**

- `children` - The form control elements (input, checkbox, etc.) and their associated labels, descriptions, and error messages.

**Attribute:**

- `data-flux-field` - Applied to the root element for styling and identification.

### Field.Label

**Prop:**

- `badge` - Optional text to display as a badge (e.g., "Required", "Optional").
- `aside` - Text displayed after the label and styled as a badge.
- `trailing` - Optional text to display at the end of the label.
- `srOnly` - Hides the label visually while keeping it accessible to screen readers.

**Slot:**

- `children` - The label text content.

### Field.Description

**Prop:**

- `srOnly` - Hides the description visually while keeping it accessible to screen readers.

**Slot:**

- `children` - The descriptive text content.

### Field.Error

**Prop:**

- `name` - The name of the field to display validation errors for.
- `message` - Custom error message content (optional).
- `icon` - The icon to display inline with the error message, a component or an element. Default: `ExclamationTriangleIcon`. Set to `false` to hide the icon.
