Skip to content

Commit

Permalink
fix(intl-phone-input): fix carret position on backspace (#840)
Browse files Browse the repository at this point in the history
#806

Co-authored-by: Кононенко Артем Игоревич <AIKononenko@alfabank.ru>
  • Loading branch information
Artess999 and Кононенко Артем Игоревич committed Sep 27, 2021
1 parent e15cf1d commit c786ac1
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/intl-phone-input/src/usePreventCaretReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ export function usePreventCaretReset({ inputRef, clearableCountryCode, countryCo
const input = event.target as HTMLInputElement;
const caretPosition = input.selectionStart;

if (event.key !== 'Backspace' || !caretPosition) return;
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft' || !caretPosition) return;

if (!clearableCountryCode && caretPosition <= countryCodeLength) {
event.preventDefault();

return;
}

const newPosition = caretPosition - 1;
const newPosition = event.key === 'Backspace' ? caretPosition - 1 : caretPosition + 1;

requestAnimationFrame(() => {
input.setSelectionRange(newPosition, newPosition);
Expand Down

0 comments on commit c786ac1

Please sign in to comment.