From 2209bb7b6a894ee8cd18f24c2ab546e4172023d5 Mon Sep 17 00:00:00 2001 From: hank121314 Date: Tue, 17 Aug 2021 18:00:01 -0700 Subject: [PATCH] `Android/ColorProps`: ColorProps with value null should be defaultColor instead of transparent (#29830) Summary: This pr: - Fixes: https://github.com/facebook/react-native/issues/30183 - Fixes: https://github.com/facebook/react-native/issues/30056 - Fixes: https://github.com/facebook/react-native/issues/29950 - Fixes: https://github.com/facebook/react-native/issues/29717 - Fixes: https://github.com/facebook/react-native/issues/29495 - Fixes: https://github.com/facebook/react-native/issues/29412 - Fixes: https://github.com/facebook/react-native/issues/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 disappear. ## Changelog [Android] [Fixed] - ColorProps with value null should be defaultColor instead of transparent Pull Request resolved: https://github.com/facebook/react-native/pull/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 ( Example Example2 setActive(!active)}> Toggle Active ); } ``` Thanks you so much for your code review! Reviewed By: JoshuaGross Differential Revision: D30209262 Pulled By: lunaleaps fbshipit-source-id: bc223f84a92f742266cb7b40eb26722551940d76 --- .../uimanager/ViewManagersPropertyCache.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java index 6e2a1691fd16c0..f856e85d53cff5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java @@ -325,6 +325,21 @@ public BoxedIntPropSetter(ReactPropGroup prop, Method setter, int index) { } } + private static class BoxedColorPropSetter extends PropSetter { + + public BoxedColorPropSetter(ReactProp prop, Method setter) { + super(prop, "mixed", setter); + } + + @Override + protected @Nullable Object getValueOrDefault(Object value, Context context) { + if (value != null) { + return ColorPropConverter.getColor(value, context); + } + return null; + } + } + /*package*/ static Map getNativePropsForView( Class viewManagerTopClass, Class shadowNodeTopClass) { @@ -418,7 +433,7 @@ private static PropSetter createPropSetter( return new BoxedBooleanPropSetter(annotation, method); } else if (propTypeClass == Integer.class) { if ("Color".equals(annotation.customType())) { - return new ColorPropSetter(annotation, method); + return new BoxedColorPropSetter(annotation, method); } return new BoxedIntPropSetter(annotation, method); } else if (propTypeClass == ReadableArray.class) {