import { useRef, useState } from 'react' export interface OtpInputProps { value: string onChange: (value: string) => void length?: number disabled?: boolean 'aria-label'?: string } /** Segmented one-time-code field: 6 cells, dark-bordered active cell with orange caret. */ export function OtpInput({ value, onChange, length = 6, disabled, ...aria }: OtpInputProps) { const inputRef = useRef(null) const [focused, setFocused] = useState(false) return (
inputRef.current?.focus()}> {Array.from({ length }, (_, i) => { const digit = value[i] ?? '' const isActive = focused && i === value.length && value.length < length return (
{digit || (isActive && )}
) })} onChange(e.target.value.replace(/\D/g, '').slice(0, length))} onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} className="absolute inset-0 size-full cursor-default opacity-0" {...aria} />
) }