DevactionUI.OtpInput (devaction_ui v0.2.3)

Copy Markdown View Source

The boxes of a one-time code, one character each: the code sent by SMS or e-mail, a two-factor code. Typing a digit fills its box and moves on; pasting the code fills every box; Backspace clears a box and, on an empty one, steps back; the arrows, Home and End move between boxes (DevactionUI.OtpInput hook, logic in text.js).

The boxes have no name: the code goes, joined, into one hidden input (name), which fires input (and change when every box is full) so a form with phx-change hears it. Every box has autocomplete="one-time-code" and a numeric keyboard (inputmode="numeric"), so the phone offers the code it just got, which arrives as one value and is spread over the boxes. numeric false takes letters and digits.

The state is status: :idle (an empty box has a quiet border, a filled one a stronger one, the focused one the ink), :error (red boxes, aria-invalid, and the helper is announced as an alert) or :ok (green boxes). The line under the boxes is the helper attr (2 digits to go), and the line under that, such as Did not arrive? Resend in 0:24, is the resend slot, so the caller draws the countdown and the button. Words are all attrs and slots: label names the group and box_label names each box (Digit %{n} of %{total}).

Summary

Functions

input(assigns)

Attributes

  • id (:string) (required)
  • name (:string) (required) - the name of the hidden input that carries the joined code.
  • label (:string) (required) - names the group of boxes, e.g. Verification code.
  • box_label (:string) (required) - names each box; %{n} and %{total} are filled in, as in Digit %{n} of %{total}.
  • value (:string) - the code so far, as the server knows it. Defaults to "".
  • length (:integer) - how many boxes. Defaults to 6.
  • separator_after (:integer) - puts a separator after this many boxes, e.g. 3 for 482 – 913. Defaults to nil.
  • status (:atom) - Defaults to :idle. Must be one of :idle, :error, or :ok.
  • numeric (:boolean) - digits only, with a numeric keyboard; false takes letters too. Defaults to true.
  • disabled (:boolean) - Defaults to false.
  • autofocus (:boolean) - focuses the first box on load. Defaults to false.
  • helper (:string) - the line under the boxes, such as 2 digits to go or Wrong code. 2 tries left.. Defaults to nil.
  • class (:any) - Defaults to nil.
  • Global attributes are accepted.

Slots

  • separator - between the boxes at separator_after; a short dash when empty.
  • resend - the line under the helper, such as Did not arrive? <button>Resend in 0:24</button>.