| 35 | const showCaret = isActive && code.length < length; |
| 36 | |
| 37 | const onInputChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 38 | const digits = toDigits(e.target.value, length); |
| 39 | setCode(digits); |
| 40 | onChange?.(digits); |
| 41 | |
| 42 | if (digits.length < length) { |
| 43 | submittedRef.current = null; |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | // AutoFill can deliver a single insertion as two change events. |
| 48 | if (submittedRef.current === digits) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | submittedRef.current = digits; |
| 53 | onSubmit(digits); |
| 54 | }; |
| 55 | |
| 56 | // A collapsed caret parked mid-string would scramble the order of the digits |
| 57 | // after it; a deliberate range selection (select-all, drag) stays native. |