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
onChangereceives the current value on each accepted change.onCompletereceives the code whenever an accepted change fills every cell.onIncompleteSubmitruns 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
| Prop | Type | Default |
|---|---|---|
autoCompleteOneTimeCodeNo description available | boolean | false |
autoFocusNo description available | boolean | false |
compactNo description available | boolean | false |
data-testidNo description available | enumstring | undefined | |
isInvalidNo description available | boolean | false |
label (required)No description available | string | |
length (required)No description available | number | |
nameNo description available | enumstring | undefined | code-input |
onChangeNo description available | enum(value: string) => void | |
onCompleteNo description available | enum(value: string) => void | |
onIncompleteSubmitNo description available | enum() => void | |
revealNo description available | enum"visible" | "masked" | "peek" | undefined | visible |
splitNo description available | enumnumber | undefined |