Skip to content

Commit 00db406

Browse files
authored
Fix caret position on edit (#481)
Co-authored-by: bowie <abovsunovskii@spotware.com>
1 parent 1c7d05c commit 00db406

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ class PhoneInput extends React.Component {
565565
newSelectedCountry = newSelectedCountry.dialCode ? newSelectedCountry : selectedCountry;
566566
}
567567

568+
const oldCaretPosition = e.target.selectionStart;
568569
let caretPosition = e.target.selectionStart;
569570
const oldFormattedText = this.state.formattedNumber;
570571
const diff = formattedNumber.length - oldFormattedText.length;
@@ -582,9 +583,10 @@ class PhoneInput extends React.Component {
582583

583584
if (lastChar == ')') {
584585
this.numberInputRef.setSelectionRange(formattedNumber.length - 1, formattedNumber.length - 1);
585-
}
586-
else if (caretPosition > 0 && oldFormattedText.length >= formattedNumber.length) {
586+
} else if (caretPosition > 0 && oldFormattedText.length >= formattedNumber.length) {
587587
this.numberInputRef.setSelectionRange(caretPosition, caretPosition);
588+
} else if (oldCaretPosition < oldFormattedText.length) {
589+
this.numberInputRef.setSelectionRange(oldCaretPosition, oldCaretPosition);
588590
}
589591

590592
if (onChange) onChange(formattedNumber.replace(/[^0-9]+/g,''), this.getCountryData(), e, formattedNumber);

0 commit comments

Comments
 (0)