Skip to content

Commit ed40f38

Browse files
makovkastarkelset
authored andcommitted
Fix backgroundColor top level prop of TextInput
Summary: Changelog: [Android] [FIXED] - Fix backgroundColor top level prop of TextInput This diff fixes two issues with the `backgroundColor` top level property of TextInput on Android: * Now it is possible to set a **string** value for the top-level `backgroundColor` property of TextInput (crashed the app previously): ``` <TextInput backgroundColor="#ffccbb">Hello, React Native</TextInput> ``` * Now it's possible to set an **integer** value for the top-level `backgroundColor` property of TextInput (had no effect previously): ``` <TextInput backgroundColor={0xffccbbff}>Hello, React Native</TextInput> ``` A `customType = "Color"` annotation parameter must be provided for `ReactBaseTextShadowNode.setBackgroundColor(...)` since the color value must be previously processed in JS before sending it over the bridge to the native code. The JS code will parse the color value and return the proper ARGB color integer to the native platforms (https://fburl.com/uqup52tn). Without providing the custom type for the background color, if a string value is set for the top-level `backgroundColor` property in the JS code, the Android code will crash since it expects an integer value for the color in `ReactBaseTextShadowNode.setBackgroundColor(...)`, but a string will be passed from JS without any conversion and there will be a `ClassCastException` thrown. If an integer value without the alpha component (like `0xffccbb`) is set, the Android native view would get an integer color value with its alpha component set to `0x00`, which means a transparent color. On a side note: the alpha component of a color must always be set when using an integer value for `backgroundColor` since the JS code, while processing the color type, shifts the rightmost 8 bytes (alpha component) to the leftmost position. If those 8 bytes are not the alpha component, you will get the wrong color in the end. It doesn't seem to be a problem for string values of `backgroundColor` though. Reviewed By: mdvacca Differential Revision: D15453980 fbshipit-source-id: f3f5d9c9877cdbce79a67f2ed93ad4589576d166
1 parent 8d61a4e commit ed40f38

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ public void setColor(@Nullable Integer color) {
459459
markUpdated();
460460
}
461461

462-
@ReactProp(name = ViewProps.BACKGROUND_COLOR)
463-
public void setBackgroundColor(Integer color) {
462+
@ReactProp(name = ViewProps.BACKGROUND_COLOR, customType = "Color")
463+
public void setBackgroundColor(@Nullable Integer color) {
464464
// Background color needs to be handled here for virtual nodes so it can be incorporated into
465465
// the span. However, it doesn't need to be applied to non-virtual nodes because non-virtual
466466
// nodes get mapped to native views and native views get their background colors get set via

0 commit comments

Comments
 (0)