diff --git a/src/food-market.public/src/components/PhoneInput.tsx b/src/food-market.public/src/components/PhoneInput.tsx index 3a146cc..0b58aef 100644 --- a/src/food-market.public/src/components/PhoneInput.tsx +++ b/src/food-market.public/src/components/PhoneInput.tsx @@ -43,11 +43,21 @@ export function PhoneInput({ value, onChange, className, disabled, ...rest }: Ph } const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.metaKey || e.ctrlKey || e.altKey) return + if (['Tab', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'Enter', 'Escape'].includes(e.key)) return const el = e.currentTarget const start = el.selectionStart ?? 0 const end = el.selectionEnd ?? 0 - if (e.key === 'Backspace' && start <= 3 && end === start) e.preventDefault() - if (e.key === 'Delete' && start < 3) e.preventDefault() + if (e.key === 'Backspace') { + if (start <= 3 && end === start) e.preventDefault() + return + } + if (e.key === 'Delete') { + if (start < 3) e.preventDefault() + return + } + // Только цифры — иначе появляется фантомная «7» из префикса. + if (!/^\d$/.test(e.key)) e.preventDefault() } const handleFocus = () => { diff --git a/src/food-market.web/src/components/PhoneInput.tsx b/src/food-market.web/src/components/PhoneInput.tsx index 7ecabd1..df05d76 100644 --- a/src/food-market.web/src/components/PhoneInput.tsx +++ b/src/food-market.web/src/components/PhoneInput.tsx @@ -59,13 +59,31 @@ export function PhoneInput({ value, onChange, className, disabled, ...rest }: Ph } const handleKeyDown = (e: React.KeyboardEvent) => { + // Cmd/Ctrl + что-то — copy/paste/select-all и прочие шорткаты, пропускаем. + if (e.metaKey || e.ctrlKey || e.altKey) return + // Навигация и форм-сабмит — пропускаем. + if (['Tab', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home', 'End', 'Enter', 'Escape'].includes(e.key)) return + const el = e.currentTarget const start = el.selectionStart ?? 0 const end = el.selectionEnd ?? 0 - // Не позволяем удалить префикс "+7 " (3 символа). Backspace на позиции - // ≤3 без выделения, либо Delete на позиции <3. - if (e.key === 'Backspace' && start <= 3 && end === start) e.preventDefault() - if (e.key === 'Delete' && start < 3) e.preventDefault() + + // Backspace/Delete — разрешаем, но не даём удалить префикс «+7 » (3 символа). + if (e.key === 'Backspace') { + if (start <= 3 && end === start) e.preventDefault() + return + } + if (e.key === 'Delete') { + if (start < 3) e.preventDefault() + return + } + + // Дальше — только цифры. Любой другой символ блокируем, чтобы он не + // попал в input и не запутал парсер. Без этого нажатие буквы при курсоре + // в любом месте ломает раскладку и приводит к появлению фантомной «7». + if (!/^\d$/.test(e.key)) { + e.preventDefault() + } } const handleFocus = (e: React.FocusEvent) => {