'use client' import { Input as GuiInput, XStack } from '@hanzo/gui'; import * as React from 'react'; export interface PinInputProps { readonly rootRef?: React.Ref; readonly count?: number; readonly inputProps?: React.InputHTMLAttributes; readonly attached?: boolean; readonly placeholder?: string; readonly value?: Array; readonly onValueChange?: (details: { value: Array }) => void; readonly onValueComplete?: (details: { value: Array }) => void; readonly disabled?: boolean; readonly invalid?: boolean; readonly otp?: boolean; readonly name?: string; readonly bgColor?: string; readonly className?: string; } // One box's frame. Colour and focus/hover/disabled states are the same // `--color-input-*` tokens `Input` itself uses — a pin box is a text control, // just a square one. `::placeholder` and the invalid border are the existing // `.lux-control` rule in tokens.css (a pseudo-element and a selector, neither // of which a prop reaches); `data-invalid` is what wires this box into it. const BOX = { width: 40, height: 40, textAlign: 'center' as const, fontSize: 14, fontWeight: 500 as const, borderRadius: 6, borderWidth: 2, borderStyle: 'solid' as const, outlineWidth: 0, backgroundColor: 'var(--color-input-bg)' as never, color: 'var(--color-input-fg)' as never, borderColor: 'var(--color-input-border)' as never, hoverStyle: { borderColor: 'var(--color-input-border-hover)' as never }, focusStyle: { borderColor: 'var(--color-input-border-focus)' as never, boxShadow: 'var(--shadow-md)', }, disabledStyle: { opacity: 0.4 }, }; export const PinInput = React.forwardRef( function PinInput(props, ref) { const { count = 6, inputProps, rootRef, attached, placeholder = ' ', value: controlledValue, onValueChange, onValueComplete, disabled, invalid, otp, name, bgColor, className, } = props; const inputRefs = React.useRef>([]); const values = React.useMemo( () => controlledValue ?? Array.from({ length: count }).fill(''), [ controlledValue, count ], ); const updateValue = React.useCallback((index: number, char: string): void => { const next = [ ...values ]; next[index] = char; onValueChange?.({ value: next }); if (next.every((v) => v.length > 0)) { onValueComplete?.({ value: next }); } }, [ values, onValueChange, onValueComplete ]); const focusInput = React.useCallback((index: number): void => { const clamped = Math.max(0, Math.min(index, count - 1)); inputRefs.current[clamped]?.focus(); }, [ count ]); const handleInput = React.useCallback((index: number, e: React.FormEvent): void => { const target = e.currentTarget; const char = target.value.slice(-1); updateValue(index, char); if (char && index < count - 1) { focusInput(index + 1); } }, [ count, updateValue, focusInput ]); const handleKeyDown = React.useCallback((index: number, e: React.KeyboardEvent): void => { if (e.key === 'Backspace') { if (values[index]) { updateValue(index, ''); } else if (index > 0) { updateValue(index - 1, ''); focusInput(index - 1); } e.preventDefault(); } else if (e.key === 'ArrowLeft' && index > 0) { focusInput(index - 1); e.preventDefault(); } else if (e.key === 'ArrowRight' && index < count - 1) { focusInput(index + 1); e.preventDefault(); } }, [ count, values, updateValue, focusInput ]); const handlePaste = React.useCallback((e: React.ClipboardEvent): void => { e.preventDefault(); const pasted = e.clipboardData.getData('text/plain').trim(); if (!pasted) { return; } const chars = pasted.slice(0, count).split(''); const next = [ ...values ]; chars.forEach((char, i) => { next[i] = char; }); onValueChange?.({ value: next }); if (next.every((v) => v.length > 0)) { onValueComplete?.({ value: next }); } focusInput(Math.min(chars.length, count - 1)); }, [ count, values, onValueChange, onValueComplete, focusInput ]); const handleFocus = React.useCallback((e: React.FocusEvent): void => { e.currentTarget.select(); }, []); const handleNoop = React.useCallback((): void => { /* noop */ }, []); const setInputRef = React.useCallback((index: number) => (el: HTMLInputElement | null): void => { inputRefs.current[index] = el; }, []); const onInputAtIndex = React.useCallback((index: number) => (e: React.FormEvent): void => { handleInput(index, e); }, [ handleInput ]); const onKeyDownAtIndex = React.useCallback((index: number) => (e: React.KeyboardEvent): void => { handleKeyDown(index, e); }, [ handleKeyDown ]); const boxBackground = bgColor ? `var(--color-${ bgColor.replace(/\./g, '-') })` as never : BOX.backgroundColor; return ( { /* Hidden input for form submission */ } { Array.from({ length: count }).map((_, index) => ( 0 ? -2 : undefined } onInput={ onInputAtIndex(index) } onKeyDown={ onKeyDownAtIndex(index) } onPaste={ handlePaste } onFocus={ handleFocus } onChange={ handleNoop } /> )) } ); }, );