Command palette

@yasinsensoy/native

React Native · Components

OtpInput

One-time password (OTP), verification code and PIN input.

6 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

You can resend in 0:15

Try typing, pasting the SMS text and clicking the boxes.

Code

import { OtpField } from "@yasinsensoy/native";

<OtpField label="Verification code" name="code" groups={3} required
  onResend={resendCode}
  onComplete={verify} />

Events

  1. Events appear here when you interact with the preview.

Verification on the server: the boxes shake on a wrong code (the right code is 123456)

You can resend in 0:30

PIN: 4 digits, hidden, underline

License key: letters+digits, uppercase, groups of 4, joined boxes

X
X
X
X
X
X
X
X
X
X
X
X

Custom set (no 0/O and 1/I) and a dot as the separator

Controlled usage

4
2

Value: "42"

Import

import { OtpInput } from "@yasinsensoy/native";

OtpInput

component

One-time password (OTP), verification code and PIN input. The code shows in boxes but there is a single TextInput behind them: SMS code suggestions on iOS (oneTimeCode), SMS autofill on Android (sms-otp), pasting and screen readers work without issues. The code inside a pasted SMS is found ("Your code: 482-913" → 482913). The same rules as OtpInput on web (@yasinsensoy/core/otp-charset).

OtpInput(props: OtpInputProps): ReactElement<unknown, string | JSXElementConstructor<any>>

Examples

<OtpInput label="Verification code" onComplete={verify} onResend={resend} />
<OtpInput length={4} mask label="PIN" />
<OtpInput length={8} charset="alphanumeric" casing="upper" groups={4} />

Characters that can be typed in a one-time code field (OtpInput).

  • "numeric": digits only (0-9). Opens the numeric keyboard on mobile.
  • "alphabetic": letters of the English alphabet only (A-Z).
  • "alphanumeric": letters and digits (A-Z, 0-9).
  • "hex": hexadecimal (0-9, A-F).
  • "any": any character except whitespace.
  • Any other string: only the characters in that string (e.g. a set without 0/O and 1/I to avoid confusion: "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"). Can also be passed from server components.
  • RegExp: expression tested against each character (e.g. /[0-9A-F]/i). Can't be passed from a server component to the client; only in "use client" files.
type OtpCharset = "numeric" | "alphabetic" | "alphanumeric" | "hex" | "any" | RegExp | (string & {})

Case letters are converted to.

type OtpCasing = "upper" | "lower"

Look of the boxes: separate boxes, joined boxes or an underline.

type OtpInputVariant = "outline" | "joined" | "underline"

Methods of the OtpInput ref.

NameTypeDescription
focus() => voidFocuses the field (the keyboard opens).
blur() => voidReleases focus.
clear() => voidClears the code and focuses the first box.

OtpInputProps

interface

Props of OtpInput.

Extends: FieldProps

NameTypeDefaultDescription
ref?Ref<OtpInputHandle>Ref: focus, blur, clear.
length?number6Number of boxes.
charset?OtpCharset"numeric"Characters that can be typed. See OtpCharset.
casing?OtpCasingConverts letters to upper or lower case.
value?stringCode (controlled).
defaultValue?string""Initial code (uncontrolled).
onValueChange?(value: string) => voidWhen the code changes.
onComplete?(value: string) => voidWhen all boxes are filled.
groups?number | readonly number[]Groups the boxes: 3 → 123-456, [3, 2, 3].
separator?ReactNodea hyphenSeparator between groups.
mask?booleanCharacters are hidden (PIN); a typed one is visible briefly.
placeholder?stringCharacter shown in an empty box (e.g. "○").
size?Size"md"Box size.
variant?OtpInputVariant | (string & {})"outline"Variant.
effect?EffectPropEffect of the boxes ("glow", "beam", "border"…); stays on in the box to type into. Defaults to appearance.defaults.otpInput.effect. See BuiltinEffect.
autoFocus?booleanFocuses when opened.
onResend?() => unknownWhen given, a "Resend code" link appears below; it counts down resendDelay seconds. If it returns a Promise it waits until it settles.
resendDelay?number60Resend wait time (seconds).
accessibilityLabel?stringScreen reader label.
style?StyleProp<ViewStyle>Container style.