diff --git a/packages/react-native-codegen/e2e/__test_fixtures__/components/FloatPropsNativeComponent.js b/packages/react-native-codegen/e2e/__test_fixtures__/components/FloatPropsNativeComponent.js index 81e27ba526b..7675e856c05 100644 --- a/packages/react-native-codegen/e2e/__test_fixtures__/components/FloatPropsNativeComponent.js +++ b/packages/react-native-codegen/e2e/__test_fixtures__/components/FloatPropsNativeComponent.js @@ -21,7 +21,7 @@ type NativeProps = $ReadOnly<{| ...ViewProps, // Props - blurRadius?: WithDefault, + blurRadius: Float, blurRadius2?: WithDefault, blurRadius3?: WithDefault, blurRadius4?: WithDefault, diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js index 96346538350..35b50da43f6 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js @@ -40,17 +40,29 @@ function getJavaValueForProp( switch (typeAnnotation.type) { case 'BooleanTypeAnnotation': - return '(boolean) value'; + return `value == null ? ${typeAnnotation.default.toString()} : (boolean) value`; case 'StringTypeAnnotation': - return '(String) value'; + const defaultValueString = + typeAnnotation.default === null + ? 'null' + : `"${typeAnnotation.default}"`; + return `value == null ? ${defaultValueString} : (String) value`; case 'Int32TypeAnnotation': - return '((Double) value).intValue()'; + return `value == null ? ${ + typeAnnotation.default + } : ((Double) value).intValue()`; case 'FloatTypeAnnotation': - return '((Double) value).floatValue()'; + if (prop.optional) { + return `value == null ? ${ + typeAnnotation.default + }f : ((Double) value).floatValue()`; + } else { + return 'value == null ? Float.NaN : ((Double) value).floatValue()'; + } case 'NativePrimitiveTypeAnnotation': switch (typeAnnotation.name) { case 'ColorPrimitive': - return '((Double) value).intValue()'; + return 'value == null ? null : ((Double) value).intValue()'; case 'ImageSourcePrimitive': return '(ReadableMap) value'; case 'PointPrimitive': diff --git a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js index 8b3661fe7f5..025825de9e7 100644 --- a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js @@ -269,7 +269,7 @@ const FLOAT_PROPS: SchemaType = { props: [ { name: 'blurRadius', - optional: true, + optional: false, typeAnnotation: { type: 'FloatTypeAnnotation', default: 0.0, diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap index d4edbe879af..c79db25bf64 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap @@ -53,7 +53,7 @@ public class BooleanPropNativeComponentDelegate { public void setProperty(BooleanPropNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? false : (boolean) value); break; } } @@ -73,7 +73,7 @@ public class ColorPropNativeComponentDelegate { public void setProperty(ColorPropNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"tintColor\\": - viewManager.setTintColor(view, ((Double) value).intValue()); + viewManager.setTintColor(view, value == null ? null : ((Double) value).intValue()); break; } } @@ -109,7 +109,7 @@ public class CommandNativeComponentDelegate { public void setProperty(CommandNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"accessibilityHint\\": - viewManager.setAccessibilityHint(view, (String) value); + viewManager.setAccessibilityHint(view, value == null ? \\"\\" : (String) value); break; } } @@ -149,7 +149,7 @@ public class EventsNestedObjectNativeComponentDelegate { public void setProperty(EventsNestedObjectNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? false : (boolean) value); break; } } @@ -169,7 +169,7 @@ public class EventsNativeComponentDelegate { public void setProperty(EventsNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? false : (boolean) value); break; } } @@ -205,22 +205,22 @@ public class FloatPropNativeComponentDelegate { public void setProperty(FloatPropNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"blurRadius\\": - viewManager.setBlurRadius(view, ((Double) value).floatValue()); + viewManager.setBlurRadius(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case \\"blurRadius2\\": - viewManager.setBlurRadius2(view, ((Double) value).floatValue()); + viewManager.setBlurRadius2(view, value == null ? 0.001f : ((Double) value).floatValue()); break; case \\"blurRadius3\\": - viewManager.setBlurRadius3(view, ((Double) value).floatValue()); + viewManager.setBlurRadius3(view, value == null ? 2.1f : ((Double) value).floatValue()); break; case \\"blurRadius4\\": - viewManager.setBlurRadius4(view, ((Double) value).floatValue()); + viewManager.setBlurRadius4(view, value == null ? 0f : ((Double) value).floatValue()); break; case \\"blurRadius5\\": - viewManager.setBlurRadius5(view, ((Double) value).floatValue()); + viewManager.setBlurRadius5(view, value == null ? 1f : ((Double) value).floatValue()); break; case \\"blurRadius6\\": - viewManager.setBlurRadius6(view, ((Double) value).floatValue()); + viewManager.setBlurRadius6(view, value == null ? 0f : ((Double) value).floatValue()); break; } } @@ -261,13 +261,13 @@ public class IntegerPropNativeComponentDelegate { public void setProperty(IntegerPropNativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"progress1\\": - viewManager.setProgress1(view, ((Double) value).intValue()); + viewManager.setProgress1(view, value == null ? 0 : ((Double) value).intValue()); break; case \\"progress2\\": - viewManager.setProgress2(view, ((Double) value).intValue()); + viewManager.setProgress2(view, value == null ? -1 : ((Double) value).intValue()); break; case \\"progress3\\": - viewManager.setProgress3(view, ((Double) value).intValue()); + viewManager.setProgress3(view, value == null ? 10 : ((Double) value).intValue()); break; } } @@ -287,7 +287,7 @@ public class InterfaceOnlyComponentDelegate { public void setProperty(InterfaceOnlyComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"accessibilityHint\\": - viewManager.setAccessibilityHint(view, (String) value); + viewManager.setAccessibilityHint(view, value == null ? \\"\\" : (String) value); break; } } @@ -311,10 +311,10 @@ public class ImageColorPropNativeComponentDelegate { viewManager.setThumbImage(view, (ReadableMap) value); break; case \\"color\\": - viewManager.setColor(view, ((Double) value).intValue()); + viewManager.setColor(view, value == null ? null : ((Double) value).intValue()); break; case \\"thumbTintColor\\": - viewManager.setThumbTintColor(view, ((Double) value).intValue()); + viewManager.setThumbTintColor(view, value == null ? null : ((Double) value).intValue()); break; case \\"point\\": viewManager.setPoint(view, (ReadableMap) value); @@ -374,10 +374,10 @@ public class StringPropComponentDelegate { public void setProperty(StringPropComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"accessibilityHint\\": - viewManager.setAccessibilityHint(view, (String) value); + viewManager.setAccessibilityHint(view, value == null ? \\"\\" : (String) value); break; case \\"accessibilityRole\\": - viewManager.setAccessibilityRole(view, (String) value); + viewManager.setAccessibilityRole(view, value == null ? null : (String) value); break; } } @@ -397,7 +397,7 @@ public class MultiFile1NativeComponentDelegate { public void setProperty(MultiFile1NativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? false : (boolean) value); break; } } @@ -412,7 +412,7 @@ public class MultiFile2NativeComponentDelegate { public void setProperty(MultiFile2NativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? true : (boolean) value); break; } } @@ -432,7 +432,7 @@ public class MultiComponent1NativeComponentDelegate { public void setProperty(MultiComponent1NativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? false : (boolean) value); break; } } @@ -447,7 +447,7 @@ public class MultiComponent2NativeComponentDelegate { public void setProperty(MultiComponent2NativeComponentInterface viewManager, T view, String propName, Object value) { switch (propName) { case \\"disabled\\": - viewManager.setDisabled(view, (boolean) value); + viewManager.setDisabled(view, value == null ? true : (boolean) value); break; } }