diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index bb6cca08fa6..cb9b17d4139 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -116,6 +116,13 @@ type PropTypeTypeAnnotation = name: string, |}>, |}> + | $ReadOnly<{| + type: 'Int32EnumTypeAnnotation', + default: number, + options: $ReadOnlyArray<{| + value: number, + |}>, + |}> | $ReadOnly<{| type: 'NativePrimitiveTypeAnnotation', name: 'ColorPrimitive' | 'ImageSourcePrimitive' | 'PointPrimitive', diff --git a/packages/react-native-codegen/src/generators/components/CppHelpers.js b/packages/react-native-codegen/src/generators/components/CppHelpers.js index 14363e71b06..33807a218e3 100644 --- a/packages/react-native-codegen/src/generators/components/CppHelpers.js +++ b/packages/react-native-codegen/src/generators/components/CppHelpers.js @@ -143,9 +143,10 @@ function convertDefaultTypeToString( throw new Error('Received unknown NativePrimitiveTypeAnnotation'); } case 'ArrayTypeAnnotation': { - switch (typeAnnotation.elementType.type) { + const elementType = typeAnnotation.elementType; + switch (elementType.type) { case 'StringEnumTypeAnnotation': - if (typeAnnotation.elementType.default == null) { + if (elementType.default == null) { throw new Error( 'A default is required for array StringEnumTypeAnnotation', ); @@ -153,7 +154,7 @@ function convertDefaultTypeToString( const enumName = getEnumName(componentName, prop.name); const enumMaskName = getEnumMaskName(enumName); const defaultValue = `${enumName}::${toSafeCppString( - typeAnnotation.elementType.default || '', + elementType.default || '', )}`; return `static_cast<${enumMaskName}>(${defaultValue})`; default: @@ -167,6 +168,9 @@ function convertDefaultTypeToString( return `${getEnumName(componentName, prop.name)}::${toSafeCppString( typeAnnotation.default, )}`; + case 'Int32EnumTypeAnnotation': + // TODO: implement default + return `${getEnumName(componentName, prop.name)}::${'TODO'}`; default: (typeAnnotation: empty); throw new Error('Received invalid typeAnnotation'); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js index 9970f366888..06a8fdc081b 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js @@ -232,6 +232,8 @@ function getNativeTypeFromAnnotation( } case 'StringEnumTypeAnnotation': return getEnumName(componentName, prop.name); + case 'Int32EnumTypeAnnotation': + return getEnumName(componentName, prop.name); default: (typeAnnotation: empty); throw new Error( @@ -285,36 +287,42 @@ function generateArrayEnumString( .replace('::_TO_CASES_::', toCases); } function generateEnum(componentName, prop) { - if (!prop.typeAnnotation.options) { - return ''; + const typeAnnotation = prop.typeAnnotation; + if (typeAnnotation.type === 'StringEnumTypeAnnotation') { + // TODO: Handle Int32EnumTypeAnnotation + const values: $ReadOnlyArray = typeAnnotation.options.map( + option => option.name, + ); + const enumName = getEnumName(componentName, prop.name); + + const fromCases = values + .map( + value => + `if (string == "${value}") { result = ${enumName}::${convertValueToEnumOption( + value, + )}; return; }`, + ) + .join('\n' + ' '); + + const toCases = values + .map( + value => + `case ${enumName}::${convertValueToEnumOption( + value, + )}: return "${value}";`, + ) + .join('\n' + ' '); + + return enumTemplate + .replace(/::_ENUM_NAME_::/g, enumName) + .replace('::_VALUES_::', values.map(toSafeCppString).join(', ')) + .replace('::_FROM_CASES_::', fromCases) + .replace('::_TO_CASES_::', toCases); } - const values = prop.typeAnnotation.options.map(option => option.name); - const enumName = getEnumName(componentName, prop.name); - const fromCases = values - .map( - value => - `if (string == "${value}") { result = ${enumName}::${convertValueToEnumOption( - value, - )}; return; }`, - ) - .join('\n' + ' '); - - const toCases = values - .map( - value => - `case ${enumName}::${convertValueToEnumOption( - value, - )}: return "${value}";`, - ) - .join('\n' + ' '); - - return enumTemplate - .replace(/::_ENUM_NAME_::/g, enumName) - .replace('::_VALUES_::', values.map(toSafeCppString).join(', ')) - .replace('::_FROM_CASES_::', fromCases) - .replace('::_TO_CASES_::', toCases); + return ''; } + function generateEnumString(componentName: string, component): string { return component.props .map(prop => { @@ -334,6 +342,7 @@ function generateEnumString(componentName: string, component): string { } if (prop.typeAnnotation.type === 'ObjectTypeAnnotation') { + // TODO: Int32 Enums return prop.typeAnnotation.properties .filter( property => @@ -574,6 +583,8 @@ function generateStruct( return; case 'StringEnumTypeAnnotation': return; + case 'Int32EnumTypeAnnotation': + return; case 'DoubleTypeAnnotation': return; case 'ObjectTypeAnnotation': diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js index 32a5fda65b1..0470a5fac25 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js @@ -117,6 +117,10 @@ function getJavaValueForProp( } case 'StringEnumTypeAnnotation': return '(String) value'; + case 'Int32EnumTypeAnnotation': + return `value == null ? ${ + typeAnnotation.default + } : ((Double) value).intValue()`; default: (typeAnnotation: empty); throw new Error('Received invalid typeAnnotation'); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js index 7aa9ad6e095..228cfe01ad7 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js @@ -88,6 +88,9 @@ function getJavaValueForProp(prop: PropTypeShape, imports): string { case 'StringEnumTypeAnnotation': addNullable(imports); return '@Nullable String value'; + case 'Int32EnumTypeAnnotation': + addNullable(imports); + return '@Nullable Integer value'; default: (typeAnnotation: empty); throw new Error('Received invalid typeAnnotation'); diff --git a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js index 3377d07f552..e9e037622e4 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js +++ b/packages/react-native-codegen/src/generators/components/GenerateViewConfigJs.js @@ -47,6 +47,7 @@ function getReactDiffProcessValue(typeAnnotation) { case 'FloatTypeAnnotation': case 'ObjectTypeAnnotation': case 'StringEnumTypeAnnotation': + case 'Int32EnumTypeAnnotation': return j.literal(true); case 'NativePrimitiveTypeAnnotation': switch (typeAnnotation.name) { 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 f4b6c887e8c..eb42918692f 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 @@ -988,6 +988,46 @@ const ENUM_PROP: SchemaType = { }, }; +const INT32_ENUM_PROP: SchemaType = { + modules: { + Switch: { + components: { + Int32EnumPropsNativeComponent: { + extendsProps: [ + { + type: 'ReactNativeBuiltInType', + knownTypeName: 'ReactNativeCoreViewProps', + }, + ], + events: [], + props: [ + { + name: 'maxInterval', + optional: true, + typeAnnotation: { + type: 'Int32EnumTypeAnnotation', + default: 0, + options: [ + { + value: 0, + }, + { + value: 1, + }, + { + value: 2, + }, + ], + }, + }, + ], + commands: [], + }, + }, + }, + }, +}; + const EVENT_PROPS: SchemaType = { modules: { Switch: { @@ -1414,6 +1454,7 @@ module.exports = { OBJECT_PROPS, MULTI_NATIVE_PROP, ENUM_PROP, + // INT32_ENUM_PROP, EVENT_PROPS, EVENTS_WITH_PAPER_NAME, EVENT_NESTED_OBJECT_PROPS,