# `LedgerUI.Components.OtpInput`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/otp_input.ex#L1)

A one-time code made of single-character boxes.

    <.otp_input field={@form[:code]} label="Codigo de 6 digitos" />
    <.otp_input name="pin" length={4} mask autofocus />
    <.otp_input field={@form[:code]} separator={3} on_complete="verify" />
    <.otp_input name="invite" mode="alphanumeric" length={8} size="lg" />

Typing fills a box and moves to the next, Backspace on an empty box goes back and empties the
previous one, the arrows, Home and End move between boxes, and a paste or an autofill spreads
its characters over the boxes. Characters the `mode` does not allow are dropped.

The boxes have no `name`: a hidden input named like the field carries the joined code and fires
a bubbling `input` event whenever it changes, so a surrounding `phx-change` receives
`%{"name" => "482913"}` (a partial code while it is typed). A `value` the server sends later
wins over the one in the browser (render `value=""` to clear a rejected code).

`on_complete` is an event name or a `%JS{}`; it is sent to `target` (or the LiveView) each time
the code changes while every box is filled, with `%{"id" => id, "name" => name, "value" => code}`.

The behaviour lives in the `LedgerUI.Otp` hook (file `otp.js`), so the component needs a stable
`id` (from `field` or `name`).

With `autofocus` the box that takes the focus first (the first empty one) is rendered with the
`on` class, so the caret shows before the hook runs.

# `otp_input`

Renders the boxes, with their caption, help and errors when given.
## Attributes

* `id` (`:string`) - Defaults to `nil`.
* `name` (`:string`) - Defaults to `nil`.
* `value` (`:any`) - Defaults to `nil`.
* `field` (`Phoenix.HTML.FormField`) - Defaults to `nil`.
* `label` (`:string`) - Defaults to `nil`.
* `label_class` (`:any`) - Defaults to `nil`.
* `help` (`:string`) - Defaults to `nil`.
* `errors` (`:list`) - Defaults to `[]`.
* `state` (`:string`) - Defaults to `nil`.Must be one of `nil`, `""`, `"success"`, or `"error"`.
* `length` (`:integer`) - Defaults to `6`.
* `mode` (`:string`) - Defaults to `"numeric"`. Must be one of `"numeric"`, or `"alphanumeric"`.
* `size` (`:string`) - Defaults to `nil`.Must be one of `nil`, `"sm"`, `"md"`, or `"lg"`.
* `mask` (`:boolean`) - Defaults to `false`.
* `disabled` (`:boolean`) - Defaults to `false`.
* `autofocus` (`:boolean`) - Defaults to `false`.
* `separator` (`:any`) - Defaults to `nil`.
* `on_complete` (`:any`) - Defaults to `nil`.
* `target` (`:any`) - Defaults to `nil`.
* `aria_label` (`:string`) - Defaults to `"One-time code"`.
* `digit_label` (`:string`) - Defaults to `"Digit"`.
* `wrapper_class` (`:any`) - Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
