import { useLayoutEffect, useRef } from "react"; import type { ClipboardEvent, FormEvent, KeyboardEvent } from "react"; import { CODE_INPUT_LENGTH, activeCodeBox, codeInputBackspace, codeInputChange, codeInputKeyDown, codeInputState, normalizeCode, } from "./codeInputModel"; import "./codeInput.css"; export interface CodeInputProps { id: string; value: string; onChange: (value: string) => void; onKeyboardComplete?: (value: string) => void; disabled?: boolean; invalid?: boolean; describedBy?: string; autoFocus?: boolean; } export function CodeInput({ id, value, onChange, onKeyboardComplete, disabled = false, invalid = false, describedBy, autoFocus = false, }: CodeInputProps) { const inputRef = useRef(null); const stateRef = useRef(codeInputState(value)); const composingRef = useRef(false); const code = normalizeCode(value); const active = activeCodeBox(code); function clearKey() { stateRef.current = codeInputState(stateRef.current.value); } function pinSelection(input: HTMLInputElement) { const end = input.value.length; if (input.selectionStart !== end || input.selectionEnd !== end) { input.setSelectionRange(end, end); } } useLayoutEffect(() => { stateRef.current = codeInputState(value); if (disabled) composingRef.current = false; const input = inputRef.current; if (input && document.activeElement === input) pinSelection(input); }, [value, disabled]); function handleInput(event: FormEvent) { const input = event.currentTarget; const native = event.nativeEvent as InputEvent; if (disabled) { clearKey(); input.value = stateRef.current.value; pinSelection(input); return; } if (composingRef.current || native.isComposing) clearKey(); const previous = stateRef.current.value; const edit = codeInputChange(stateRef.current, input.value, native.inputType ?? ""); stateRef.current = edit.state; input.value = edit.state.value; pinSelection(input); if (edit.state.value !== previous) onChange(edit.state.value); if (edit.keyboardComplete) onKeyboardComplete?.(edit.state.value); } function handleKeyDown(event: KeyboardEvent) { clearKey(); const composing = composingRef.current || event.nativeEvent.isComposing; if (!composing && event.key !== "Delete") pinSelection(event.currentTarget); if (disabled || composing) return; const modified = event.ctrlKey || event.metaKey || event.altKey; if (event.key === "Backspace" && !modified) { event.preventDefault(); const previous = stateRef.current.value; stateRef.current = codeInputBackspace(stateRef.current); if (stateRef.current.value !== previous) onChange(stateRef.current.value); return; } stateRef.current = codeInputKeyDown(stateRef.current, event.key, modified); } function handlePaste(event: ClipboardEvent) { event.preventDefault(); clearKey(); if (disabled) return; // Read before native maxLength can truncate a spaced clipboard value. const edit = codeInputChange(stateRef.current, event.clipboardData.getData("text/plain"), "insertFromPaste"); const previous = stateRef.current.value; stateRef.current = edit.state; event.currentTarget.value = edit.state.value; pinSelection(event.currentTarget); if (edit.state.value !== previous) onChange(edit.state.value); } return (
{Array.from({ length: CODE_INPUT_LENGTH }, (_, index) => ( ))} { composingRef.current = true; clearKey(); }} onCompositionEnd={(event) => { composingRef.current = false; clearKey(); pinSelection(event.currentTarget); }} onFocus={(event) => { clearKey(); pinSelection(event.currentTarget); }} onBlur={() => { clearKey(); composingRef.current = false; }} onSelect={(event) => { const input = event.currentTarget; // Native caret movement must leave the digit marker for its input event. if (input.selectionStart !== input.value.length || input.selectionEnd !== input.value.length) clearKey(); const replacesAll = input.value.length > 0 && input.selectionStart === 0 && input.selectionEnd === input.value.length; // A full selection lets browser fill replace the value before input pins it. if (!composingRef.current && !replacesAll) pinSelection(input); }} onClick={(event) => { clearKey(); pinSelection(event.currentTarget); }} />
); }