OtpInput
HkOtpInput is the one-cell-per-character code field: TOTP, SMS and e-mail verification codes, invite codes and recovery codes. The cells are a projection of a single modelValue string, so the field stays controlled: typing a run spreads it forward across cells, a paste pours the whole clipboard through them, Backspace steps back a cell, and complete fires once on the short-to-full edge. Reach for it whenever a code is short, contiguous and typed from another device.
- For a numeric quantity inside a form, use HkNumberInput.
- For an arbitrary string the user composes, use HkInput.
- For the phone number that receives the code, use HkPhoneInput.
Import
;
use *; // OtpInput, OtpSize
use *; // rsx!, VNode
The Rust form is a plain function, OtpInput(OtpInputProps { … }). length is clamped to 1..=12 in the mirror, exactly like Vue's cellCount.
Length and grouping
length sets the cell count — 6 is the universal TOTP and SMS width, 4 a PIN. separated splits an even row into two groups (the 123 456 shape) and is ignored when the count is odd. size — sm / md / lg — is class-only: the stylesheet owns the cell box, the gap and the font ceiling for each ramp.
Alphabet
Digits only by default; alphanumeric widens the accepted set to [0-9A-Za-z] for invite and recovery codes. The alphabet is enforced twice — filtering on the way in and a numeric keypad on touch devices via inputmode — so a letters-only paste into a digits-only row is dropped instead of being painted into a cell.
Six digits.
Letters and digits.
States and messages
error paints the row and the cells in the error channel and prints the message line; hint is the quiet line shown while there is no error. disabled and readonly reach every cell, and the disabled cells also publish aria-disabled. The short-to-full edge is where a host submits or verifies — complete fires there once, and autoSubmit turns it into a submit signal.
Six digits, sent by SMS.
That code has expired.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| modelValue | String | "" | — |
| length | Number | 6 | — |
| error | String | undefined | — |
| hint | String | undefined | — |
| label | String | undefined | — |
| size | <code>String as () => "sm" | "md" | "lg"</code> | "md" | — |
| disabled | Boolean | false | — |
| readonly | Boolean | false | — |
| alphanumeric | Boolean | false | — |
| separated | Boolean | false | — |
| autofocus | Boolean | false | — |
| id | String | undefined | — |
| submitOnEnter | Function as PropType<() => void> | undefined | — |
| autoSubmit | Boolean | false | — |
| ariaLabel | String | undefined | — |
| cellAriaLabel | Function as PropType<(index: number) => string> | undefined | — |
Emits
| Event | Payload |
|---|---|
| update:modelValue | string |
| complete | string |
| focus | FocusEvent |
| blur | FocusEvent |
| keydown | KeyboardEvent |
Rust
Props — OtpInput
| Prop | Type | Default | Description |
|---|---|---|---|
| model_value | String | "" | The composed code, at most length characters (Vue: modelValue). |
| length | i32 | 6 | Cell count; clamped to 1..=12 like Vue's cellCount. |
| error | Option<String> | None | Error ink + message line; also marks every cell aria-invalid. |
| hint | Option<String> | None | Quiet message under the row, shown only while error is unset. |
| label | Option<String> | None | Visible caption above the row. |
| size | OtpSize | OtpSize::Md | — |
| disabled | bool | false | — |
| readonly | bool | false | — |
| alphanumeric | bool | false | Accept [0-9A-Za-z] instead of digits only (invite / recovery codes). |
| separated | bool | false | Split an even row into two groups (the "123 456" SMS shape). |
| id | Option<String> | None | First cell's id (Vue: id); cell *n* gets {id}-{n}. |
| submit_on_enter | Option<EventHandler<()>> | None | Submit intent: Enter in any cell, and auto_submit on the fill edge. |
| auto_submit | bool | false | Fire submit_on_enter the moment the row fills (Vue: autoSubmit). |
| aria_label | Option<String> | None | Accessible name for the row (Vue: ariaLabel, falls back to label). |
| cell_aria_label | Option<Callback<usize, String>> | None | Per-cell accessible name (Vue: cellAriaLabel, called with the 1-based position). |
| on_cursor_change | Option<EventHandler<usize>> | None | The caret index Vue would have moved to (see the header delta) — auto-advance after a keystroke/paste, the step-back target after Backspace, the landing cell after ←/→/Home/End. |
| on_input | Option<EventHandler<String>> | None | Vue: update:modelValue, emitted with the sanitized code. |
| on_complete | Option<EventHandler<String>> | None | Vue: complete, fired on the short → full edge only. |
| on_focus | Option<EventHandler<FocusEvent>> | None | Vue: the focus emit (per cell). |
| on_blur | Option<EventHandler<FocusEvent>> | None | Vue: the blur emit (per cell). |
| on_keydown | Option<EventHandler<KeyboardEvent>> | None | Vue: the keydown emit, forwarded after the field's own handling. |
| class | String | "" | Host classes, appended to the row (Vue merges $attrs.class there). |
| style | String | "" | Host style, applied to the row (Vue merges $attrs.style there). |
Accessibility
- The row is a role="group" named by aria-label, falling back to label; its optional description points at the hint line's id ({id}-hint). The visible caption is a <label> whose for targets the first cell's id, and each cell after the first gets {id}-{index}.
- Every cell carries its own name, Digit {n} by default or whatever cellAriaLabel returns for the 1-based position — so a screen reader hears "Digit 2 of 6" rather than eight anonymous inputs. Pass ariaLabel when the row needs a name the caption cannot give.
- error sets aria-invalid on every cell and renders the message in a role="alert" paragraph, so a verification failure is announced without a manual refocus.
- Keyboard: typed characters advance the caret, Backspace clears the current cell or steps back into the previous one, Delete clears only the current cell, and the arrow keys, Home and End walk the row (Home/End jump to the ends). Each cell is its own tab stop.
- In the browser the row opts in to one-time-code autofill — the first cell carries autocomplete="one-time-code" — and turns off autocapitalize, autocorrect and spellcheck so a code is never "corrected".
- The Rust mirror cannot move focus, so it reports the index Vue would have focused through on_cursor_change; the host performs the movement. Enter submits where Vue's submitOnEnter would, and autoSubmit fires it on the short-to-full edge.
Related components
- HkPhoneInput — the number the code is sent to.
- HkInput — any free-text or secret entry.
- HkNumberInput — a numeric quantity with steppers.
- HkSearchInput — the bare search field.