Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 5,396 Bytes
89af3b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 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<HTMLInputElement>(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<HTMLInputElement>) {
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<HTMLInputElement>) {
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<HTMLInputElement>) {
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 (
<div className="code-input" data-invalid={invalid || undefined} data-disabled={disabled || undefined}>
{Array.from({ length: CODE_INPUT_LENGTH }, (_, index) => (
<span className="code-input-box" key={index} aria-hidden="true" data-active={index === active || undefined}>
{code[index] ?? ""}
</span>
))}
<input
ref={inputRef}
className="code-input-field"
id={id}
type="text"
value={code}
inputMode="numeric"
autoComplete="one-time-code"
maxLength={CODE_INPUT_LENGTH}
disabled={disabled}
aria-invalid={invalid || undefined}
aria-describedby={describedBy}
autoFocus={autoFocus}
onInput={handleInput}
onKeyDown={handleKeyDown}
onKeyUp={clearKey}
onPaste={handlePaste}
onCompositionStart={() => { 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); }}
/>
</div>
);
}
|