Skip to content

Commit

Permalink
Fix raw text update clearing Text (#11000)
Browse files Browse the repository at this point in the history
* Fix raw text update clearing Text

In #10811, a bug was introduced where a raw text update to a
non-optimized Text component would call ClearValue on the
TextBlock::TextProperty. This behavior was introduced to ensure that we
cleared out the TextProperty before transitioning from optimized to
non-optimized text.

To fix this bug, we check that the text is actually optimized before
clearing the TextProperty, and for the case of transitioning text from
optimized to non-optimized, added an optional flag that must be set to
true before attempting to clear the text.

Please note, we never transition from non-optimized to optimized text,
so we don't need to worry about that case.

* Change files
  • Loading branch information
rozele committed Dec 15, 2022
1 parent c6a151f commit 9e22298
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix raw text update clearing Text",
"packageName": "react-native-windows",
"email": "erozell@outlook.com",
"dependentChangeType": "patch"
}
9 changes: 4 additions & 5 deletions vnext/Microsoft.ReactNative/Views/TextViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TextShadowNode final : public ShadowNodeBase {
UpdateOptimizedText();
} else if (wasOptimized) {
// Remove optimized text and re-construct as Inline tree
UpdateOptimizedText();
UpdateOptimizedText(true);
if (const auto uiManager = GetNativeUIManager(GetViewManager()->GetReactContext()).lock()) {
for (size_t i = 0; i < m_children.size(); ++i) {
if (const auto childNode =
Expand All @@ -88,8 +88,7 @@ class TextShadowNode final : public ShadowNodeBase {

void removeAllChildren() override {
if (m_isTextOptimized) {
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.ClearValue(xaml::Controls::TextBlock::TextProperty());
UpdateOptimizedText();
} else {
Super::removeAllChildren();
}
Expand All @@ -107,7 +106,7 @@ class TextShadowNode final : public ShadowNodeBase {
GetViewManager()->MarkDirty(m_tag);
}

void UpdateOptimizedText() {
void UpdateOptimizedText(bool clearOptimizedText = false) {
if (m_children.size() > 0 && m_isTextOptimized) {
if (const auto uiManager = GetNativeUIManager(GetViewManager()->GetReactContext()).lock()) {
winrt::hstring text = L"";
Expand All @@ -120,7 +119,7 @@ class TextShadowNode final : public ShadowNodeBase {
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.Text(text);
}
} else {
} else if (m_isTextOptimized || clearOptimizedText) {
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.ClearValue(xaml::Controls::TextBlock::TextProperty());
}
Expand Down

0 comments on commit 9e22298

Please sign in to comment.