diff --git a/change/react-native-windows-9aaace3c-20a8-4c24-90b2-d777742f65ba.json b/change/react-native-windows-9aaace3c-20a8-4c24-90b2-d777742f65ba.json new file mode 100644 index 00000000000..74c8fd17793 --- /dev/null +++ b/change/react-native-windows-9aaace3c-20a8-4c24-90b2-d777742f65ba.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "If initial value is set, updated TextInput.value changes will not be reflected", + "packageName": "react-native-windows", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp b/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp index c435e8fe527..59eb2f7cc56 100644 --- a/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp +++ b/vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp @@ -133,14 +133,16 @@ class TextInputShadowNode : public ShadowNodeBase { void SetSelection(int64_t start, int64_t end); winrt::Shape FindCaret(xaml::DependencyObject element); + bool m_initialUpdateComplete = false; bool m_autoFocus = false; bool m_shouldClearTextOnFocus = false; bool m_shouldSelectTextOnFocus = false; bool m_contextMenuHidden = false; bool m_hideCaret = false; bool m_isTextBox = true; - winrt::Microsoft::ReactNative::JSValue m_placeholderTextColor; bool m_shouldClearTextOnSubmit = false; + + winrt::Microsoft::ReactNative::JSValue m_placeholderTextColor; std::vector m_submitKeyEvents{}; // Javascripts is running in a different thread. If the typing is very fast, @@ -175,6 +177,9 @@ void TextInputShadowNode::createView(const winrt::Microsoft::ReactNative::JSValu } void TextInputShadowNode::dispatchTextInputChangeEvent(winrt::hstring newText) { + if (!m_initialUpdateComplete) { + return; + } m_nativeEventCount++; folly::dynamic eventData = folly::dynamic::object("target", m_tag)("text", react::uwp::HstringToDynamic(newText))( "eventCount", m_nativeEventCount); @@ -199,14 +204,20 @@ void TextInputShadowNode::registerEvents() { // another TextChanged event with correct event count. if (m_isTextBox) { m_passwordBoxPasswordChangingRevoker = {}; - m_textBoxTextChangingRevoker = control.as().TextChanging( - winrt::auto_revoke, [=](auto &&, auto &&) { m_nativeEventCount++; }); + m_textBoxTextChangingRevoker = + control.as().TextChanging(winrt::auto_revoke, [=](auto &&, auto &&) { + if (m_initialUpdateComplete) + m_nativeEventCount++; + }); } else { m_textBoxTextChangingRevoker = {}; if (control.try_as()) { - m_passwordBoxPasswordChangingRevoker = control.as().PasswordChanging( - winrt::auto_revoke, [=](auto &&, auto &&) { m_nativeEventCount++; }); + m_passwordBoxPasswordChangingRevoker = + control.as().PasswordChanging(winrt::auto_revoke, [=](auto &&, auto &&) { + if (m_initialUpdateComplete) + m_nativeEventCount++; + }); } } @@ -673,6 +684,7 @@ void TextInputShadowNode::updateProperties(winrt::Microsoft::ReactNative::JSValu } m_updating = false; + m_initialUpdateComplete = true; } void TextInputShadowNode::SetText(const winrt::Microsoft::ReactNative::JSValue &text) { diff --git a/vnext/src/Libraries/Components/TextInput/TextInput.windows.js b/vnext/src/Libraries/Components/TextInput/TextInput.windows.js index 08773db5819..e38fd03bcf2 100644 --- a/vnext/src/Libraries/Components/TextInput/TextInput.windows.js +++ b/vnext/src/Libraries/Components/TextInput/TextInput.windows.js @@ -1229,6 +1229,7 @@ function InternalTextInput(props: Props): React.Node { ref={_setNativeRef} {...props} dataDetectorTypes={props.dataDetectorTypes} + mostRecentEventCount={mostRecentEventCount} onBlur={_onBlur} onChange={_onChange} onContentSizeChange={props.onContentSizeChange}