# TextField

Crate: `libero`
Import: `use libero::components::TextField;`
Feature: `text-field` (on by default, turns on `use-icon`, `visually-hidden`, `use-id`, `loader`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/form/text_field.rs>
Index: [index.md](index.md) lists every other page
Description: A single-line text field with a label, a description, helper text and a validation message.

A single-line text field. Like every field, it stacks a label, a description,
the control, helper text and a validation message. Pass `value` and `oninput`
to control it, or leave `value` out and the input keeps its own text. `leading`
and `trailing` put content inside the border, beside the control. Fields side by
side in a `Flex` row line up when the row sets `align: "flex-start"` (a row
centres by default) and each field sets `description_position: "below"`.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{FieldStatus, TextField};

#[component]
fn Demo() -> Element {
    let mut value = use_signal(String::new);

    rsx! {
        TextField {
            value: value(),
            oninput: move |next| value.set(next),
            label: "Username",
            placeholder: "ada",
        }
    }
}
```

## Props

### `TextField`

| Prop | Type | Default | Description |
|---|---|---|---|
| `size` | `Responsive<Size>` | `md` | Height, padding and font size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent; the slot buttons follow too. |
| `radius` | `Responsive<ThemeAwareValue>` | `sm` | Corner radius, independent of `size`. Or any CSS, e.g. `radius: "0"`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `value` | `Option<String>` | - | The text in the field. Leave it out and the input keeps its own text. |
| `oninput` | `EventHandler<String>` | - | Fires on every keystroke with the text the field should hold next. |
| `validate` | `Validators<String>` | - | Rules over the text, shown once the field loses focus or its form is submitted. |
| `name` | `FieldName<String>` | - | What the field posts as. A path such as `Signup::FIELDS.email()` also binds the text to the surrounding `Form`'s value when the field has no `oninput`. |
| `placeholder` | `String` | - | Shown while the field is empty. |
| `leading` | `Element` | - | Inside the frame, before the control, such as a search icon or a currency sign. |
| `trailing` | `Element` | - | Inside the frame, after the control, such as a clear button or a unit. |
| `describe_leading` | `bool` | `false` | Set it when `leading` is text that belongs to the value, such as `@`, so a screen reader reads it with the input. Not for an icon or a button. |
| `describe_trailing` | `bool` | `false` | The same for `trailing`, such as `kg` or a `12/20` counter. |
| `label` | `Caption` | - | The field's caption, above the control. It names the field. Takes a string or an `Element`. The label's element id is `{id}-label`, so a `Combobox`'s `labelled_by` can name its list from it. |
| `description` | `Caption` | - | Between the label and the control. What to enter. |
| `label_trailing` | `Caption` | - | Content in the label row, outside the `<label>`, at the row's end: a button cannot sit inside the label. Takes a string or an `Element`. |
| `description_position` | `DescriptionPosition` | `Above` | Where the description sits: `above` the control, or `below` it. `below` keeps the controls of side-by-side fields level when only some have a description; give the row `align: "flex-start"`. Unset, the theme's `field.description_position`. A checkbox, switch or radio draws its description under the label either way. |
| `helper` | `Caption` | - | Under the control. Formatting rules, limits or a counter. |
| `status` | `FieldStatus` | `Valid` | Validation state, under the helper. A bare `&str` is an error; an empty one or `None` is `Valid`. |
| `required` | `bool` | `false` | Marks the field required and adds an asterisk to the label; a field without a visible `label` shows no mark. Inside a `Form`, an empty one fails the submit. |
| `disabled` | `bool` | `false` | Disables and dims the field. |
| `readonly` | `bool` | `false` | Focusable and posted with the form, but not editable. `disabled` drops the field from the tab order and the post instead. |

`TextField` also takes the `<input>` HTML attributes (`maxlength`,
`autocomplete`, `type`, ...) and, like every component, the shared props `sx`,
`class`, `style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `FieldPart::Label` | `label` | The label above the control. |
| `FieldPart::Required` | `required` | The required asterisk, in the label. |
| `FieldPart::LabelRow` | `label-row` | The row holding the label and its `label_trailing`; drawn only with a trailing slot. |
| `FieldPart::LabelTrailing` | `label-trailing` | The `label_trailing` slot, at the end of the label row. |
| `FieldPart::Description` | `description` | The caption between the label and the control. |
| `FieldPart::Frame` | `frame` | The bordered box around the control. |
| `FieldPart::Leading` | `leading` | The slot before the control: an icon, a prefix. |
| `FieldPart::Control` | `control` | The element the label names. |
| `FieldPart::Trailing` | `trailing` | The slot after the control: a chevron, a toggle. |
| `FieldPart::Helper` | `helper` | The caption under the control. |
| `FieldPart::Status` | `status` | The validation message. |

## Accessibility

### Libero handles

- A string `description` or `helper` is read with the input.
- An error status marks the input invalid, a warning does not.

### You must

- Leave `label` unset only when something else names the field, such as an
  `aria_label`.
- Markup in `description` or `helper` is shown but not read, so its
  accessibility is yours.
- A `leading` or `trailing` slot is not read with the input. When it is text
  that belongs to the value, such as a unit or a counter, set
  `describe_leading` or `describe_trailing`.

### Example

An email field, `TextField { label: "Email", helper: "We never share it." }`:
a screen reader reads the label, then the helper. An error status marks the
input invalid and is read the same way.

## Theme defaults

Most of it is `FieldDefaults`, shared by every field. `TextFieldDefaults` holds
only the `size` and `radius` it starts at.

| Field | Type | Description |
|---|---|---|
| `field.gap` | `&'static str` | Vertical gap between the slots. |
| `field.frame_gap` | `&'static str` | Horizontal gap between leading, control and trailing. |
| `field.sizes` | `Sizes<FieldSizeLevel>` | `label_font_size`, `caption_font_size`, `font_size`, `height`, `padding_y`, `padding_x` per size. |
| `text_field.size` | `Size` | Default `size` when the prop is omitted, `md`. |
| `text_field.radius` | `Size` | Default `radius` when the prop is omitted, `sm`. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-field-gap` | Vertical gap between the slots. |
| `--lsx-field-label-font-size-<size>` | The label's `font-size` for that size step. |
| `--lsx-field-caption-font-size-<size>` | `font-size` of the description, helper and status text. |
| `--lsx-field-frame-gap` | Horizontal gap inside the frame. |
| `--lsx-field-font-size-<size>` | `font-size` of the control for that size step. |
| `--lsx-field-height-<size>` | The frame's `min-height` for that size step. |
| `--lsx-field-padding-y-<size>` | The frame's vertical padding, which sets the height once the control wraps. |
| `--lsx-field-padding-x-<size>` | The frame's horizontal padding for that size step. |

`radius` reads the shared `--lsx-radius-<size>` scale.

## Data attributes

State tokens on the wrapper's and the frame's `data-state`, space separated.
The `<input>` itself carries none.

| Token | Condition |
|---|---|
| `size-<size>` | The `size` in effect. |
| `radius-<size>` | The `radius` in effect. |
| `disabled` | `disabled` is set. |
| `required` | `required` is set. |
| `warning` | `status` is a `Warning`. |
| `error` | `status` is an `Error`. |

The caption slots carry `data-slot="description"`, `"helper"`, `"status"` and
`"required"`. `leading` and `trailing` carry theirs inside the frame.
