From dc2ce9e66e337ef90a179853467dbb8b09b9d24c Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 25 Jan 2024 11:07:12 -0800 Subject: [PATCH] Fix incorrect measurement of TextInput (#42655) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42655 This bug is caused by a caching issue: when the user enters a new character into the textInput: ReactTextInput 1) caches the Spannable entered by the user and 2) it updates internal Fabric state, which triggers the measurement of the TextInput component using the cached Spannable. The problem is that the Spannable entered by the user has the wrong "styles" for the text input. Since measurement is using the cached Spannable, then the measurement of the TextInput ends up being is incorrect. In this diff I'm fixing the bug by updating the styles (lineHeight) of the cached spannable that is cached when the user updates the TextInput. The styles weren't updated correctly because mTextAttributes didn't have the proper style props set Changelog: [Android][Fixed] - Fix incorrect measurement of TextInput when new architecture is enabled Reviewed By: javache, sammy-SC Differential Revision: D52924982 fbshipit-source-id: ced9f2c348bdb9bf706028b1063858cebd5a071a --- packages/react-native/ReactAndroid/api/ReactAndroid.api | 2 ++ .../com/facebook/react/views/textinput/ReactEditText.java | 6 ++++++ .../react/views/textinput/ReactTextInputManager.java | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index d78986c2d98..a4b4342ca12 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -7381,6 +7381,7 @@ public class com/facebook/react/views/textinput/ReactEditText : androidx/appcomp public fun setFontWeight (Ljava/lang/String;)V public fun setInputType (I)V public fun setLetterSpacingPt (F)V + public fun setLineHeight (I)V public fun setMaxFontSizeMultiplier (F)V public fun setOnKeyPress (Z)V public fun setPlaceholder (Ljava/lang/String;)V @@ -7468,6 +7469,7 @@ public class com/facebook/react/views/textinput/ReactTextInputManager : com/face public fun setInlineImagePadding (Lcom/facebook/react/views/textinput/ReactEditText;I)V public fun setKeyboardType (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V public fun setLetterSpacing (Lcom/facebook/react/views/textinput/ReactEditText;F)V + public fun setLineHeight (Lcom/facebook/react/views/textinput/ReactEditText;I)V public fun setMaxFontSizeMultiplier (Lcom/facebook/react/views/textinput/ReactEditText;F)V public fun setMaxLength (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/Integer;)V public fun setMultiline (Lcom/facebook/react/views/textinput/ReactEditText;Z)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index d70f9e67b3c..c4d42eb13f0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -270,6 +270,12 @@ public class ReactEditText extends AppCompatEditText { return super.onKeyUp(keyCode, event); } + @Override + public void setLineHeight(int lineHeight) { + mTextAttributes.setLineHeight(lineHeight); + super.setLineHeight(lineHeight); + } + @Override protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) { super.onScrollChanged(horiz, vert, oldHoriz, oldVert); diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 376af6a81e6..74b881401ef 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -382,6 +382,11 @@ public class ReactTextInputManager extends BaseViewManager