InputOTP
One-time code input with a box per character, paste support and SMS autofill on mobile.
Status: beta. Category: Forms.
import { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator, REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS } from '@glyphui/ui'
Exports: InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator, inputOtpStyles, REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS, REGEXP_ONLY_DIGITS_AND_CHARS.
Notes
- It is one real input under the boxes, so paste, autofill and screen readers work as with a normal field.
- Restrict characters with pattern, e.g. REGEXP_ONLY_DIGITS.
InputOTP props
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | Custom slot layout (InputOTPGroup, InputOTPSlot, InputOTPSeparator). Defaults to one group of length slots. | |
containerClassName | string | ||
length | number | Number of characters. Default 6. | |
nonce | string | ||
noScriptCSSFallback | string | null | ||
onChange | ((newValue: string) => unknown) | ||
onComplete | ((...args: any[]) => unknown) | ||
pasteTransformer | ((pasted: string) => string) | ||
pushPasswordManagerStrategy | "none" | "increase-width" | ||
textAlign | "center" | "right" | "left" | ||
value | string |
Example: Six digits
Verifies once the last digit is typed.
import { Field, InputOTP, REGEXP_ONLY_DIGITS } from '@glyphui/ui'
import { useState } from 'react'
export default function Example() {
const [status, setStatus] = useState<'idle' | 'checking' | 'done'>('idle')
return (
<Field
label="Verification code"
description={
status === 'done'
? 'Verified.'
: status === 'checking'
? 'Checking…'
: 'We sent a code to •••• 4821.'
}
>
<InputOTP
pattern={REGEXP_ONLY_DIGITS}
onComplete={() => {
setStatus('checking')
setTimeout(() => setStatus('done'), 800)
}}
/>
</Field>
)
}
Example: Custom groups
Compose groups and separators yourself.
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
REGEXP_ONLY_DIGITS_AND_CHARS,
} from '@glyphui/ui'
export default function Example() {
return (
<InputOTP aria-label="Recovery code" length={8} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
<InputOTPSlot index={6} />
<InputOTPSlot index={7} />
</InputOTPGroup>
</InputOTP>
)
}
Live page: https://glyphui.xyz/components/input-otp