From 48e75b4c698d979dc4ab89eb619c42822425f89c Mon Sep 17 00:00:00 2001 From: Igor Klemenski Date: Thu, 11 Feb 2021 16:50:38 -0800 Subject: [PATCH] [FIX] Cursor resetting in TextInput (#7056) * Fix cursor resetting in TextInput. * Change files * Do no preserve selection, just cursor. Co-authored-by: Igor Klemenski --- ...ative-windows-2dead2dd-8acc-4a17-a3f2-7e3091cde5ad.json | 7 +++++++ vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 change/react-native-windows-2dead2dd-8acc-4a17-a3f2-7e3091cde5ad.json 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 badf5616544..0ec7133e947 100644 --- a/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp +++ b/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp @@ -596,10 +596,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());