Skip to content
Components

Code input

A numeric code input rendered as individual cells, for passcodes, one-time passwords (OTP) and authenticator (TOTP) codes.

Basic usage

First, import the component.

import {CodeInput} from '@pleo-io/telescope'

Then use it like so:

import {CodeInput} from '@pleo-io/telescope'
;<CodeInput length={6} label="Verification code" autoCompleteOneTimeCode />

The field uses inputMode="numeric" and rejects non-digit input after trimming surrounding whitespace. autoCompleteOneTimeCode enables the browser's one-time-code autofill hint for SMS codes.

See the API reference for all props and Guidelines for when and why to use the various options.


Examples

Passcode

reveal controls how digits are shown: visible (default), masked (hidden behind •) or peek (masked, but the last typed digit is shown for one second). masked and peek use a password input.

import {CodeInput} from '@pleo-io/telescope'
;<CodeInput length={4} label="Passcode" reveal="peek" />

Authenticator code

split sets the number of cells per dash-separated group. For example, split={3} on a six-digit code gives two groups of three.

import {CodeInput} from '@pleo-io/telescope'
;<CodeInput length={6} split={3} label="Authenticator code" />

Callbacks and reset

  • onChange receives the current value on each accepted change.
  • onComplete receives the code whenever an accepted change fills every cell.
  • onIncompleteSubmit runs when the field's form is submitted with an incomplete code. Pressing Enter submits the form, but an empty field is blocked by native required-field validation.

To clear the code from outside, for example after a failed verification, attach a ref typed CodeInputRef and call reset(). It also calls onChange with an empty string and moves focus back to the field.

import {useRef} from 'react'
import {CodeInput} from '@pleo-io/telescope'
import type {CodeInputRef} from '@pleo-io/telescope'


export function VerificationCode({verify}: {verify: (code: string) => Promise<boolean>}) {
    const ref = useRef<CodeInputRef>(null)


    const handleComplete = async (code: string) => {
        const isValid = await verify(code)
        if (!isValid) {
            ref.current?.reset()
        }
    }


    return <CodeInput ref={ref} length={6} label="Verification code" onComplete={handleComplete} />
}

Accessibility

label is required and becomes the input's aria-label. Pass an already translated string: the component ships no default label.

isInvalid sets aria-invalid on the input as well as the visual error state on the cells.

API reference

PropTypeDefault
autoCompleteOneTimeCode
boolean
false
autoFocus
boolean
false
compact
boolean
false
data-testid
enum
isInvalid
boolean
false
label (required)
string
length (required)
number
name
enum
code-input
onChange
enum
onComplete
enum
onIncompleteSubmit
enum
reveal
enum
visible
split
enum
Didn't find what you were looking for?
Edit this page