mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35953 DimensionValue is a reserved prop type that can be a number or string (such as '50%'). On Java, it will get converted to a YogaValue (converter added to this diff); on C++ it will get converted to a YGValue (converter already exists as it's used in Fabric). Changelog: [Internal][Added] - Add codegen support for DimensionValue for components Reviewed By: cipolleschi Differential Revision: D42650799 fbshipit-source-id: 1d2bc30bbd93837dedbbb4c74f814963c8140957
131 lines
3.5 KiB
JavaScript
131 lines
3.5 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type {ComponentShape} from '../../CodegenSchema';
|
|
|
|
function upperCaseFirst(inString: string): string {
|
|
return inString[0].toUpperCase() + inString.slice(1);
|
|
}
|
|
|
|
function getInterfaceJavaClassName(componentName: string): string {
|
|
return `${componentName.replace(/^RCT/, '')}ManagerInterface`;
|
|
}
|
|
|
|
function getDelegateJavaClassName(componentName: string): string {
|
|
return `${componentName.replace(/^RCT/, '')}ManagerDelegate`;
|
|
}
|
|
|
|
function toSafeJavaString(
|
|
input: string,
|
|
shouldUpperCaseFirst?: boolean,
|
|
): string {
|
|
const parts = input.split('-');
|
|
|
|
if (shouldUpperCaseFirst === false) {
|
|
return parts.join('');
|
|
}
|
|
|
|
return parts.map(upperCaseFirst).join('');
|
|
}
|
|
|
|
function getImports(
|
|
component: ComponentShape,
|
|
type: 'interface' | 'delegate',
|
|
): Set<string> {
|
|
const imports: Set<string> = new Set();
|
|
|
|
component.extendsProps.forEach(extendProps => {
|
|
switch (extendProps.type) {
|
|
case 'ReactNativeBuiltInType':
|
|
switch (extendProps.knownTypeName) {
|
|
case 'ReactNativeCoreViewProps':
|
|
imports.add('import android.view.View;');
|
|
return;
|
|
default:
|
|
(extendProps.knownTypeName: empty);
|
|
throw new Error('Invalid knownTypeName');
|
|
}
|
|
default:
|
|
(extendProps.type: empty);
|
|
throw new Error('Invalid extended type');
|
|
}
|
|
});
|
|
|
|
function addImportsForNativeName(
|
|
name:
|
|
| 'ColorPrimitive'
|
|
| 'EdgeInsetsPrimitive'
|
|
| 'ImageSourcePrimitive'
|
|
| 'PointPrimitive'
|
|
| 'DimensionPrimitive'
|
|
| $TEMPORARY$string<'ColorPrimitive'>
|
|
| $TEMPORARY$string<'EdgeInsetsPrimitive'>
|
|
| $TEMPORARY$string<'ImageSourcePrimitive'>
|
|
| $TEMPORARY$string<'PointPrimitive'>
|
|
| $TEMPORARY$string<'DimensionPrimitive'>,
|
|
) {
|
|
switch (name) {
|
|
case 'ColorPrimitive':
|
|
if (type === 'delegate') {
|
|
imports.add('import com.facebook.react.bridge.ColorPropConverter;');
|
|
}
|
|
return;
|
|
case 'ImageSourcePrimitive':
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
return;
|
|
case 'PointPrimitive':
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
return;
|
|
case 'EdgeInsetsPrimitive':
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
return;
|
|
case 'DimensionPrimitive':
|
|
if (type === 'delegate') {
|
|
imports.add(
|
|
'import com.facebook.react.bridge.DimensionPropConverter;',
|
|
);
|
|
} else {
|
|
imports.add('import com.facebook.yoga.YogaValue;');
|
|
}
|
|
return;
|
|
default:
|
|
(name: empty);
|
|
throw new Error(`Invalid ReservedPropTypeAnnotation name, got ${name}`);
|
|
}
|
|
}
|
|
|
|
component.props.forEach(prop => {
|
|
const typeAnnotation = prop.typeAnnotation;
|
|
|
|
if (typeAnnotation.type === 'ReservedPropTypeAnnotation') {
|
|
addImportsForNativeName(typeAnnotation.name);
|
|
}
|
|
|
|
if (typeAnnotation.type === 'ArrayTypeAnnotation') {
|
|
imports.add('import com.facebook.react.bridge.ReadableArray;');
|
|
}
|
|
|
|
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
|
|
imports.add('import com.facebook.react.bridge.ReadableMap;');
|
|
}
|
|
});
|
|
|
|
return imports;
|
|
}
|
|
|
|
module.exports = {
|
|
getInterfaceJavaClassName,
|
|
getDelegateJavaClassName,
|
|
toSafeJavaString,
|
|
getImports,
|
|
};
|