# PinField

Crate: `libero`
Import: `use libero::components::PinField;`
Feature: `pin-field` (off 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/pin_field.rs>
Index: [index.md](index.md) lists every other page
Description: A pin, one character per cell, with auto-advance, paste spreading and an `oncomplete` that fires the moment the last cell fills.

A pin, one character per cell. Typing fills a cell and moves to the next,
Backspace clears and steps back, and the arrows move without changing anything.
A code pasted into any cell spreads across the rest, and characters the field
does not take are dropped, so `"4 2-1 3"` lands as `4213`. Extra HTML attributes
land on the group, not on a cell.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::PinField;

#[component]
fn Demo() -> Element {
    rsx! {
        PinField {
            label: "Verification code",
        }
    }
}
```

## Props

### `PinField`

| Prop | Type | Default | Description |
|---|---|---|---|
| `size` | `Responsive<Size>` | `md` | The cell's square, its font size and the gap. A cell is as tall as a `TextField` of the same size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. |
| `radius` | `Responsive<ThemeAwareValue>` | `sm` | Corner radius of each cell, independent of `size`. Or any CSS, e.g. `radius: "0"`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `length` | `usize` | `4` | How many cells, clamped to 1 through 32. |
| `kind` | `PinKind` | `numeric` | `numeric` takes the digits 0-9, `alphanumeric` the ASCII letters and digits. Any other character, an accented letter too, is ignored as it is typed. |
| `value` | `Option<String>` | - | The pin so far, one character per filled cell. Leave it out and the field keeps its own pin. |
| `oninput` | `EventHandler<String>` | - | Fires for every change of the pin, a typed character, a clear or a paste, with the pin the field should hold next. |
| `validate` | `Validators<String>` | - | Rules over the pin, shown once the field loses focus or its form is submitted. |
| `oncomplete` | `EventHandler<String>` | - | Fires once when the last empty cell fills. Clearing a cell arms it again. |
| `mask` | `bool` | `false` | Hides the characters as in a password field. The value is unaffected. |
| `one_time_code` | `bool` | `true` | Lets a phone offer the code it just received. |
| `separator` | `Element` | - | Rendered between the cells, such as a dash. On a row too narrow for every cell, the separators shrink away first, so the cells keep their touch size. |
| `name` | `FieldName<String>` | - | What the pin posts as. A path such as `Login::FIELDS.code()` also binds the pin to the surrounding `Form`'s value when the field has no `oninput`. |
| `read_paste` | `Callback<String, String>` | - | Reads the code out of pasted or autofilled text, such as an invite link that ends in it. It gets text of three characters or more, never a typed character. What it returns is filtered by `kind` like typed characters. |
| `autofocus` | `bool` | `false` | Focuses the first cell on mount, also inside a dialog, whose focus trap starts there. |
| `label` | `Caption` | - | The field's caption, above the cells. It names the group of cells. |
| `description` | `Caption` | - | Between the label and the cells. Where the code came from. |
| `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 cells. How long the code lasts, how to get another. |
| `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 every cell. |
| `readonly` | `bool` | `false` | Focusable and posted with the form, but not editable. `disabled` drops the field from the tab order and the post instead. |

Like every component, it also takes 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` | Each cell's box. |
| `FieldPart::Control` | `control` | Each cell's input. |
| `FieldPart::Helper` | `helper` | The caption under the control. |
| `FieldPart::Status` | `status` | The validation message. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Left` or `Right` | Moves to the previous or next cell. |
| `Home` or `End` | Moves to the first or last cell. |
| `Backspace` | Removes the character, so the ones after it move one cell left, and moves back, except from the last cell; on an empty cell, only moves back. |
| `Delete` | Removes the character, so the ones after it move one cell left, and stays. |
| `Space` | Moves to the next cell without typing. |
| `Tab` | Moves to the next cell, and past the last one leaves the field, as in any group of inputs. |

### Libero handles

- Each cell is a tab stop.
- The pin has no holes: clearing a middle cell moves the characters after it
  one cell left, and a character typed past the pin lands in the first empty
  cell.
- A separator is decoration, hidden from screen readers.
- Each cell is named for its place, such as "Character 1 of 6", from the
  localization's `PinFieldLabels`, and reads the helper and the error too.

### You must

- Give the field a `label`, which names the whole group.

### Example

A sign-in code, `PinField { label: "Verification code", length: 6, .. }`: a
screen reader reads the group as "Verification code" and each cell as
"Character 1 of 6" and so on.

## Theme defaults

`PinFieldDefaults` holds `length`, `size`, `radius`, `kind` and `gap`, the
space between the cells, emitted as `--lsx-pin-field-gap`. Everything else is
`FieldDefaults`, so a pin field and a text field line up in one form.

## Data attributes

State tokens on the wrapper's and each cell frame's `data-state`: size, radius,
status, `disabled`, `required` and the `kind`. Each cell input carries
`data-pin-index`.
