Skip to content

Commit

Permalink
fix: check if its first render before updating from props (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg authored Jul 5, 2024
1 parent 1b30540 commit fbaf59f
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions ios/MarkdownCommitHook.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,31 @@
stateData.attributedStringBox);

// Handles the first render, where the text stored in props is
// different than the one stored in state The one in state is empty,
// while the one in props is passed from JS If we don't update the
// state here, we'll end up with a one-default-line-sized text
// input. A better condition to do that can be probably chosen, but
// this seems to work
auto plainString =
std::string([[nsAttributedString string] UTF8String]);
if (plainString != textInputProps.text) {
// creates new AttributedString from props, adapted from
// TextInputShadowNode (ios one, text inputs are
// platform-specific)
auto attributedString = AttributedString{};
attributedString.appendFragment(AttributedString::Fragment{
textInputProps.text, defaultTextAttributes});

auto attachments = BaseTextShadowNode::Attachments{};
BaseTextShadowNode::buildAttributedString(
defaultTextAttributes, *nodes.textInput, attributedString,
attachments);

// convert the newly created attributed string to
// NSAttributedString
nsAttributedString = RCTNSAttributedStringFromAttributedStringBox(
AttributedStringBox{attributedString});
// different than the one stored in state. The one in state is empty,
// while the one in props is passed from JS. If we don't update the
// state here, we'll end up with a one-default-line-sized text input
if (textInputState.getRevision() == State::initialRevisionValue) {
auto plainStringFromState =
std::string([[nsAttributedString string] UTF8String]);

if (plainStringFromState != textInputProps.text) {
// creates new AttributedString from props, adapted from
// TextInputShadowNode (ios one, text inputs are
// platform-specific)
auto attributedString = AttributedString{};
attributedString.appendFragment(AttributedString::Fragment{
textInputProps.text, defaultTextAttributes});

auto attachments = BaseTextShadowNode::Attachments{};
BaseTextShadowNode::buildAttributedString(
defaultTextAttributes, *nodes.textInput, attributedString,
attachments);

// convert the newly created attributed string to
// NSAttributedString
nsAttributedString = RCTNSAttributedStringFromAttributedStringBox(
AttributedStringBox{attributedString});
}
}

// apply markdown
Expand Down

0 comments on commit fbaf59f

Please sign in to comment.