diff --git a/change/react-native-windows-2dead2dd-8acc-4a17-a3f2-7e3091cde5ad.json b/change/react-native-windows-2dead2dd-8acc-4a17-a3f2-7e3091cde5ad.json new file mode 100644 index 00000000000..210db58a4cf --- /dev/null +++ b/change/react-native-windows-2dead2dd-8acc-4a17-a3f2-7e3091cde5ad.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fix cursor resetting in TextInput.", + "packageName": "react-native-windows", + "email": "igklemen@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp b/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp index 392325a9351..7f3c7b0455a 100644 --- a/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp +++ b/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp @@ -612,10 +612,17 @@ void TextInputShadowNode::SetText(const winrt::Microsoft::ReactNative::JSValue & if (m_mostRecentEventCount == m_nativeEventCount) { if (textBox) { if (text.Type() == winrt::Microsoft::ReactNative::JSValueType::String) { + auto oldCursor = textBox.SelectionStart(); + auto oldSelectionLength = textBox.SelectionLength(); auto oldValue = textBox.Text(); auto newValue = react::uwp::asHstring(text); if (oldValue != newValue) { textBox.Text(newValue); + if (static_cast(oldCursor) <= newValue.size()) { + textBox.SelectionStart(oldCursor); + } else { + textBox.SelectionStart(newValue.size()); + } } } else if (text.IsNull()) textBox.ClearValue(xaml::Controls::TextBox::TextProperty());