Native view configs in bridgeless mode: constantsToExport support (#39519)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39519

If the `constantsToExport` method is overridden by RCTViewManager subclass, we'll out those constants in "Constants" property of the view config.
This diff also defines a property on UIManager for every view configs. This add support for `UIManager.RNTMyLegacyNativeView.Constants.PI` syntax in bridgeless mode.
Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D49372561

fbshipit-source-id: e9333c94ad882ee38a5a3729ccc19330d7736657
This commit is contained in:
Dmitry Rykun
2023-09-25 14:23:56 -07:00
committed by Facebook GitHub Bot
parent 2de964cfd2
commit 3f1ee478bb
3 changed files with 19 additions and 3 deletions
@@ -29,7 +29,7 @@ function getCachedConstants(): Object {
return cachedConstants;
}
module.exports = {
const UIManagerJS: {[string]: $FlowFixMe} = {
getViewManagerConfig: (viewManagerName: string): mixed => {
if (ReactNativeFeatureFlags.enableNativeViewConfigsInBridgelessMode()) {
return getCachedConstants()[viewManagerName];
@@ -178,3 +178,11 @@ module.exports = {
dismissPopupMenu: (): void =>
console.error(errorMessageForMethod('dismissPopupMenu')),
};
if (ReactNativeFeatureFlags.enableNativeViewConfigsInBridgelessMode()) {
Object.keys(getCachedConstants()).forEach(viewConfigName => {
UIManagerJS[viewConfigName] = getCachedConstants()[viewConfigName];
});
}
module.exports = UIManagerJS;
@@ -1490,8 +1490,7 @@ NSMutableDictionary<NSString *, id> *RCTModuleConstantsForDestructuredComponent(
// New Architecture. To make native view configs work in the New Architecture we will populate these properties in
// native.
moduleConstants[@"Commands"] = viewConfig[@"Commands"];
// In the Old Architecture "Constants" are empty.
moduleConstants[@"Constants"] = [NSDictionary new];
moduleConstants[@"Constants"] = viewConfig[@"Constants"];
// Add direct events
for (NSString *eventName in viewConfig[@"directEvents"]) {
@@ -414,6 +414,14 @@ static RCTPropBlock createNSInvocationSetter(NSMethodSignature *typeSignature, S
return commands;
}
+ (NSDictionary<NSString *, id> *)constantsForViewMangerClass:(Class)managerClass
{
if ([managerClass instancesRespondToSelector:@selector(constantsToExport)]) {
return [[managerClass new] constantsToExport];
}
return @{};
}
+ (NSDictionary<NSString *, id> *)viewConfigForViewMangerClass:(Class)managerClass
{
NSMutableArray<NSString *> *bubblingEvents = [NSMutableArray new];
@@ -498,6 +506,7 @@ static RCTPropBlock createNSInvocationSetter(NSMethodSignature *typeSignature, S
@"capturingEvents" : capturingEvents,
@"baseModuleName" : superClass == [NSObject class] ? (id)kCFNull : RCTViewManagerModuleNameForClass(superClass),
@"Commands" : commands,
@"Constants" : [self constantsForViewMangerClass:managerClass],
};
}