Fragiola

API: components

OTPField

A one-time code, one native input per slot in a named group with one tab stop, with paste, autofill, forms and Field support, in each framework.

A one-time code entered one character per slot: a sign-in code, a verification code. Typing moves forward, Backspace moves back, a paste or the browser's one-time-code autofill fills the slots, and the code works in a native <form> and in a Field. The APG has no pattern for it; Base UI's OTP Field decides where the APG says nothing (the behaviour rule).

Sign-in code
Gallery

Behaviour

  • One native input per slot, in a group with one tab stop: Tab enters and leaves the code once. Each slot is named by you, saying its position (aria-label="Digit 2 of 6"): no text of the library's own. A slot with no name takes a Field's label.
  • Value: a string with no gap, at most length long; onValueChange reports input-change, input-clear, input-paste, keyboard or none (a reset); onValueComplete reports the complete code; onValueInvalid the text whose characters validationType refused.
  • Typing: a character fills the slot and moves on; a refused one changes nothing; a paste spreads from the focused slot. A slot after the first empty one cannot take the focus.
  • Keys: ArrowLeft and ArrowRight (mirrored in RTL), Home and End, Backspace (moves back), Delete, Control or Meta + Backspace (clears).
  • Autofill: the first slot has autocomplete="one-time-code" and maxlength the length.
  • Forms: a hidden input submits the code; required refuses an empty code and its pattern an incomplete one; a reset returns to defaultValue.
  • mask hides the characters (type="password").

Anatomy

partelementdata-primitives-partrole
Rootdivotp-field-rootgroup; holds the value; renders the hidden input after it
Inputinputotp-field-inputone slot
(hidden input)input type="text"otp-field-hidden-inputwhat the form submits

Usage

import { Label } from "@fragiola/primitives-react/label";
import { OTPField } from "@fragiola/primitives-react/otp-field";

<Label id="code-label" htmlFor="code">Verification code</Label>
<OTPField.Root id="code" name="code" length={6} aria-labelledby="code-label" onValueComplete={verify}>
    {[1, 2, 3, 4, 5, 6].map((slot) => (
        <OTPField.Input key={slot} aria-label={`Digit ${slot} of 6`} />
    ))}
</OTPField.Root>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. Vue binds the value as v-model:value with @value-change, @value-complete and @value-invalid; Angular as [(value)] with (valueChangeDetails), (valueComplete) and (valueInvalid) (each { value, details }).

Root

proptypedefaultdescription
lengthnumberrequiredthe number of slots
valuestringthe code, controlled
defaultValuestring""the initial code, uncontrolled
onValueChange(value: string, details: { reason, event }) => voidevery change
onValueComplete(value: string, details: { reason, event }) => voidthe code became length long (or a complete code was pasted again)
onValueInvalid(text: string, details: { reason, event }) => voidtyped or pasted text held refused characters
validationType"numeric" | "alpha" | "alphanumeric" | "none" | RegExp"numeric"the characters a slot accepts (a RegExp one character must match)
maskbooleanfalsethe slots hide their characters
autoCompletestring"one-time-code"the first slot's and the hidden input's autocomplete
inputModestringnumeric for digits, else textthe slots' virtual keyboard
requiredbooleanfalsethe hidden input is required
readOnlybooleanfalsefocusable and submitted, but nothing changes it
disabledbooleanfalseignores every interaction, submits nothing
namestringthe hidden input's name (a Field's wins)
idstringgeneratedthe first slot's id; slot n gets <id>-<n>

Input

No props of its own: its name is yours (aria-label).

State and part attributes

Every part carries data-disabled, data-readonly, data-required, data-complete and, inside a Field, the Field's state.

partattributes
Rootrole="group"; inside a Field, aria-labelledby and aria-describedby
Inputid, type (text, or password with mask), inputmode, autocomplete, maxlength (the first slot), enterkeyhint, the roving tabindex, data-filled, data-index, aria-invalid inside an invalid Field

Keyboard

keydoes
ArrowLeft, ArrowRightthe previous or the next slot (mirrored in RTL); with Control or Meta, the first or the first empty one
Home, ArrowUpthe first slot
End, ArrowDownthe first empty slot
Backspaceremoves a character and moves back; Control or Meta: clears the code
Deleteremoves the slot's character
Tableaves the code

Exports

entryexports
@fragiola/primitives-react/otp-fieldChangeDetails, ChangeReason, OTPField, OTPFieldInputProps, OTPFieldInputState, OTPFieldRootProps, OTPFieldState, OtpValidationType, RenderProp
@fragiola/primitives-vue/otp-fieldChangeDetails, ChangeReason, OTPField, OTPFieldInputState, OTPFieldRootEmits, OTPFieldRootProps, OTPFieldState, OtpValidationType, RenderSlotProps
@fragiola/primitives-angular/otp-fieldChangeDetails, ChangeReason, OTPFieldInput, OTPFieldInputState, OTPFieldRoot, OTPFieldState, OtpValidationType, ValueChangeEvent