From 961bd63da6d46622f86fe13fbdd427373ed5b809 Mon Sep 17 00:00:00 2001 From: Thomas Halling Date: Mon, 13 Mar 2023 16:34:02 +0100 Subject: [PATCH] 618 Solve a case when prefix is active, and a user has selected all text and types to overwrite. In this caret caret should be placed after entered input, so add length of prefix to the caretPosition --- src/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/index.js b/src/index.js index 07988e0f..04a3d4b4 100644 --- a/src/index.js +++ b/src/index.js @@ -592,6 +592,11 @@ class PhoneInput extends React.Component { if (lastChar == ')') { this.numberInputRef.setSelectionRange(formattedNumber.length - 1, formattedNumber.length - 1); + } else if (caretPosition === 1 && !this.props.disableCountryCode) { + // https://github.com/bl00mber/react-phone-input-2/issues/618 + // Solve a case when prefix is active, and a user has selected all text and types to overwrite. In this caret caret should be placed after entered input, so add length of prefix to the caretPosition + caretPosition += (prefix ? prefix.length : 0); + this.numberInputRef.setSelectionRange(caretPosition, caretPosition); } else if (caretPosition > 0 && oldFormattedText.length >= formattedNumber.length) { this.numberInputRef.setSelectionRange(caretPosition, caretPosition); } else if (oldCaretPosition < oldFormattedText.length) {