Skip to content

Commit

Permalink
Align logic in BaseTextInputShadowNode to determine updateStateIfNeed…
Browse files Browse the repository at this point in the history
…ed with AndroidTextInputShadowNode

Summary:
[Changelog] [Internal] - Align logic in BaseTextInputShadowNode to determine updateStateIfNeeded with AndroidTextInputShadowNode

As a preparation for facebook#48165 this aligns the implementation of those 2 methods

Differential Revision: D68004755
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jan 10, 2025
1 parent ecf75dd commit 7d54a21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,36 @@ class BaseTextInputShadowNode : public ConcreteViewShadowNode<
const auto& stateData = BaseShadowNode::getStateData();
const auto& reactTreeAttributedString = getAttributedString(layoutContext);

react_native_assert(textLayoutManager_);
if (stateData.reactTreeAttributedString.isContentEqual(
reactTreeAttributedString)) {
// Tree is often out of sync with the value of the TextInput.
// This is by design - don't change the value of the TextInput in the State,
// and therefore in the host platform, unless the tree itself changes.
if (stateData.reactTreeAttributedString == reactTreeAttributedString) {
return;
}

// If props event counter is less than what we already have in state, skip
// it
const auto& props = BaseShadowNode::getConcreteProps();
TextInputState newState(
if (props.mostRecentEventCount < stateData.mostRecentEventCount) {
return;
}

// Even if we're here and updating state, it may be only to update the
// layout manager If that is the case, make sure we don't update text: pass
// in the current attributedString unchanged, and pass in zero for the
// "event count" so no changes are applied There's no way to prevent a state
// update from flowing to the host platform, so we just ensure it's a noop
// in those cases.
auto newEventCount = stateData.reactTreeAttributedString.isContentEqual(
reactTreeAttributedString)
? 0
: props.mostRecentEventCount;

BaseShadowNode::setStateData(TextInputState{
AttributedStringBox{reactTreeAttributedString},
reactTreeAttributedString,
props.paragraphAttributes,
props.mostRecentEventCount);
BaseShadowNode::setStateData(std::move(newState));
newEventCount});
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ LayoutConstraints AndroidTextInputShadowNode::getTextConstraints(

void AndroidTextInputShadowNode::updateStateIfNeeded() {
ensureUnsealed();

const auto& stateData = getStateData();
auto reactTreeAttributedString = getAttributedString();
const auto& state = getStateData();

// Tree is often out of sync with the value of the TextInput.
// This is by design - don't change the value of the TextInput in the State,
// and therefore in Java, unless the tree itself changes.
if (state.reactTreeAttributedString == reactTreeAttributedString) {
if (stateData.reactTreeAttributedString == reactTreeAttributedString) {
return;
}

// If props event counter is less than what we already have in state, skip it
if (getConcreteProps().mostRecentEventCount < state.mostRecentEventCount) {
const auto& props = BaseShadowNode::getConcreteProps();
if (props.mostRecentEventCount < stateData.mostRecentEventCount) {
return;
}

Expand All @@ -141,16 +141,16 @@ void AndroidTextInputShadowNode::updateStateIfNeeded() {
// current attributedString unchanged, and pass in zero for the "event count"
// so no changes are applied There's no way to prevent a state update from
// flowing to Java, so we just ensure it's a noop in those cases.
auto newEventCount =
state.reactTreeAttributedString.isContentEqual(reactTreeAttributedString)
auto newEventCount = stateData.reactTreeAttributedString.isContentEqual(
reactTreeAttributedString)
? 0
: getConcreteProps().mostRecentEventCount;
: props.mostRecentEventCount;
auto newAttributedString = getMostRecentAttributedString();

setStateData(TextInputState{
AttributedStringBox(newAttributedString),
reactTreeAttributedString,
getConcreteProps().paragraphAttributes,
props.paragraphAttributes,
newEventCount});
}

Expand Down

0 comments on commit 7d54a21

Please sign in to comment.