Skip to content

Commit 842bcb9

Browse files
hank121314facebook-github-bot
authored andcommitted
Android/ColorProps: ColorProps with value null should be defaultColor instead of transparent (#29830)
Summary: This pr: - Fixes: #30183 - Fixes: #30056 - Fixes: #29950 - Fixes: #29717 - Fixes: #29495 - Fixes: #29412 - Fixes: #29378 Because most of ReactProps(name = ViewProps.COLOR) accept @ Nullable Integer. For example: https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java#L472-L479 After update to react-native 0.63.2 to make PlatformColor work, there is a new ColorPropSetter. https://github.com/facebook/react-native/blob/abb6433f506851430dffb66f0dd34c1e70a223fe/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java#L194-L215 But ColorPropSetter won't return an nullable value with getValueOrDefault, it will always return it's defaultValue which is 0. And 0 is equal to TRANSPARENT, will cause <Text /> disappear. ## Changelog [Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent Pull Request resolved: #29830 Test Plan: Please initiated a new project and replaced the app with the following code: ``` import * as React from 'react'; import {Text, View, TouchableOpacity, PlatformColor} from 'react-native'; export default function App() { const [active, setActive] = React.useState(false); return ( <View> <Text style={active ? {color: 'green'} : null}>Example</Text> <Text style={ active ? {color: PlatformColor('android:color/holo_purple')} : null }> Example2 </Text> <TouchableOpacity onPress={() => setActive(!active)}> <Text>Toggle Active</Text> </TouchableOpacity> </View> ); } ``` Thanks you so much for your code review! Reviewed By: JoshuaGross Differential Revision: D30209262 Pulled By: lunaleaps fbshipit-source-id: bc223f84a92f742266cb7b40eb26722551940d76
1 parent f971ea9 commit 842bcb9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,21 @@ public BoxedIntPropSetter(ReactPropGroup prop, Method setter, int index) {
325325
}
326326
}
327327

328+
private static class BoxedColorPropSetter extends PropSetter {
329+
330+
public BoxedColorPropSetter(ReactProp prop, Method setter) {
331+
super(prop, "mixed", setter);
332+
}
333+
334+
@Override
335+
protected @Nullable Object getValueOrDefault(Object value, Context context) {
336+
if (value != null) {
337+
return ColorPropConverter.getColor(value, context);
338+
}
339+
return null;
340+
}
341+
}
342+
328343
/*package*/ static Map<String, String> getNativePropsForView(
329344
Class<? extends ViewManager> viewManagerTopClass,
330345
Class<? extends ReactShadowNode> shadowNodeTopClass) {
@@ -418,7 +433,7 @@ private static PropSetter createPropSetter(
418433
return new BoxedBooleanPropSetter(annotation, method);
419434
} else if (propTypeClass == Integer.class) {
420435
if ("Color".equals(annotation.customType())) {
421-
return new ColorPropSetter(annotation, method);
436+
return new BoxedColorPropSetter(annotation, method);
422437
}
423438
return new BoxedIntPropSetter(annotation, method);
424439
} else if (propTypeClass == ReadableArray.class) {

0 commit comments

Comments
 (0)