Skip to content

Commit

Permalink
Controlled TextInput doesn't work most of the time (#7658) (#7713)
Browse files Browse the repository at this point in the history
* If initial value is set, updated TextInput.value changes will not be reflected

* Change files

* default member initializers for bit-fields requires at least '/std:c++latest'
  • Loading branch information
acoates-ms committed May 4, 2021
1 parent afd6c8d commit 82f12b4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"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"
}
19 changes: 15 additions & 4 deletions vnext/Microsoft.ReactNative/Views/TextInputViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class TextInputShadowNode : public ShadowNodeBase {
void SetSelection(int64_t start, int64_t end);
winrt::Shape FindCaret(xaml::DependencyObject element);

bool m_initialUpdateComplete = false;
bool m_shouldClearTextOnFocus = false;
bool m_shouldSelectTextOnFocus = false;
bool m_contextMenuHidden = false;
Expand Down Expand Up @@ -171,6 +172,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);
Expand All @@ -195,14 +199,20 @@ void TextInputShadowNode::registerEvents() {
// another TextChanged event with correct event count.
if (m_isTextBox) {
m_passwordBoxPasswordChangingRevoker = {};
m_textBoxTextChangingRevoker = control.as<xaml::Controls::TextBox>().TextChanging(
winrt::auto_revoke, [=](auto &&, auto &&) { m_nativeEventCount++; });
m_textBoxTextChangingRevoker =
control.as<xaml::Controls::TextBox>().TextChanging(winrt::auto_revoke, [=](auto &&, auto &&) {
if (m_initialUpdateComplete)
m_nativeEventCount++;
});
} else {
m_textBoxTextChangingRevoker = {};

if (control.try_as<xaml::Controls::IPasswordBox4>()) {
m_passwordBoxPasswordChangingRevoker = control.as<xaml::Controls::IPasswordBox4>().PasswordChanging(
winrt::auto_revoke, [=](auto &&, auto &&) { m_nativeEventCount++; });
m_passwordBoxPasswordChangingRevoker =
control.as<xaml::Controls::IPasswordBox4>().PasswordChanging(winrt::auto_revoke, [=](auto &&, auto &&) {
if (m_initialUpdateComplete)
m_nativeEventCount++;
});
}
}

Expand Down Expand Up @@ -603,6 +613,7 @@ void TextInputShadowNode::updateProperties(winrt::Microsoft::ReactNative::JSValu

Super::updateProperties(props);
m_updating = false;
m_initialUpdateComplete = true;
}

void TextInputShadowNode::SetText(const winrt::Microsoft::ReactNative::JSValue &text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@ function InternalTextInput(props: Props): React.Node {
ref={_setNativeRef}
{...props}
dataDetectorTypes={props.dataDetectorTypes}
mostRecentEventCount={mostRecentEventCount}
onBlur={_onBlur}
onChange={_onChange}
onContentSizeChange={props.onContentSizeChange}
Expand Down

0 comments on commit 82f12b4

Please sign in to comment.