Skip to content

Commit

Permalink
Align logic in BaseTextInputShadowNode to calculate placeholder strin…
Browse files Browse the repository at this point in the history
…g with AndroidTextInputShadowNode (#48584)

Summary:
Pull Request resolved: #48584

[Changelog] [Internal] - Align logic in BaseTextInputShadowNode to calculate placeholder string with AndroidTextInputShadowNode

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

Reviewed By: NickGerleman

Differential Revision: D68004218

fbshipit-source-id: 722a33bb2665c59347ef1b0cd8ed7b35a05b2113
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jan 10, 2025
1 parent cf5ab03 commit 55d0bc4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,7 @@ class BaseTextInputShadowNode : public ConcreteViewShadowNode<
auto attributedString = getAttributedString(layoutContext);

if (attributedString.isEmpty()) {
auto placeholderString = !props.placeholder.empty()
? props.placeholder
: BaseTextShadowNode::getEmptyPlaceholder();
auto textAttributes =
props.getEffectiveTextAttributes(layoutContext.fontSizeMultiplier);
attributedString.appendFragment(
{std::move(placeholderString), textAttributes, {}});
attributedString = getPlaceholderAttributedString(layoutContext);
}

// Yoga expects a baseline relative to the Node's border-box edge instead of
Expand Down Expand Up @@ -217,21 +211,33 @@ class BaseTextInputShadowNode : public ConcreteViewShadowNode<
: getAttributedString(layoutContext);

if (attributedString.isEmpty()) {
const auto& props = BaseShadowNode::getConcreteProps();
auto placeholder = props.placeholder;
// Note: `zero-width space` is insufficient in some cases (e.g. when we
// need to measure the "hight" of the font).
// TODO T67606511: We will redefine the measurement of empty strings as
// part of T67606511
auto string = !placeholder.empty()
? placeholder
: BaseTextShadowNode::getEmptyPlaceholder();
auto textAttributes =
props.getEffectiveTextAttributes(layoutContext.fontSizeMultiplier);
attributedString.appendFragment({string, textAttributes, {}});
attributedString = getPlaceholderAttributedString(layoutContext);
}
return AttributedStringBox{attributedString};
}

// For measurement purposes, we want to make sure that there's at least a
// single character in the string so that the measured height is greater
// than zero. Otherwise, empty TextInputs with no placeholder don't
// display at all.
// TODO T67606511: We will redefine the measurement of empty strings as part
// of T67606511
AttributedString getPlaceholderAttributedString(
const LayoutContext& layoutContext) const {
const auto& props = BaseShadowNode::getConcreteProps();

AttributedString attributedString;
auto placeholderString = !props.placeholder.empty()
? props.placeholder
: BaseTextShadowNode::getEmptyPlaceholder();
auto textAttributes =
props.getEffectiveTextAttributes(layoutContext.fontSizeMultiplier);
attributedString.appendFragment(
{.string = std::move(placeholderString),
.textAttributes = textAttributes,
.parentShadowView = {}});
return attributedString;
}
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,19 @@ AttributedString AndroidTextInputShadowNode::getMostRecentAttributedString()
// of T67606511
AttributedString AndroidTextInputShadowNode::getPlaceholderAttributedString()
const {
// Return placeholder text, since text and children are empty.
auto textAttributedString = AttributedString{};
auto fragment = AttributedString::Fragment{};
fragment.string = getConcreteProps().placeholder;

if (fragment.string.empty()) {
fragment.string = BaseTextShadowNode::getEmptyPlaceholder();
}
const auto& props = BaseShadowNode::getConcreteProps();

AttributedString attributedString;
auto placeholderString = !props.placeholder.empty()
? props.placeholder
: BaseTextShadowNode::getEmptyPlaceholder();
auto textAttributes = TextAttributes::defaultTextAttributes();
textAttributes.apply(getConcreteProps().textAttributes);

// If there's no text, it's possible that this Fragment isn't actually
// appended to the AttributedString (see implementation of appendFragment)
fragment.textAttributes = textAttributes;
fragment.parentShadowView = ShadowView(*this);
textAttributedString.appendFragment(std::move(fragment));

return textAttributedString;
textAttributes.apply(props.textAttributes);
attributedString.appendFragment(
{.string = std::move(placeholderString),
.textAttributes = textAttributes,
.parentShadowView = ShadowView(*this)});
return attributedString;
}

} // namespace facebook::react

0 comments on commit 55d0bc4

Please sign in to comment.