-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodegenNativeComponent.js
75 lines (65 loc) · 2.52 KB
/
codegenNativeComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* 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.
*
* @format
* @flow strict-local
*/
// TODO: move this file to shims/ReactNative (requires React update and sync)
import type {HostComponent} from '../../Libraries/Renderer/shims/ReactNativeTypes';
import requireNativeComponent from '../../Libraries/ReactNative/requireNativeComponent';
import UIManager from '../ReactNative/UIManager';
// TODO: import from CodegenSchema once workspaces are enabled
type Options = $ReadOnly<{|
interfaceOnly?: boolean,
paperComponentName?: string,
paperComponentNameDeprecated?: string,
excludedPlatforms?: $ReadOnlyArray<'iOS' | 'android'>,
|}>;
export type NativeComponentType<T> = HostComponent<T>;
// If this function runs then that means the view configs were not
// generated at build time using `GenerateViewConfigJs.js`. Thus
// we need to `requireNativeComponent` to get the view configs from view managers.
// `requireNativeComponent` is not available in Bridgeless mode.
// e.g. This function runs at runtime if `codegenNativeComponent` was not called
// from a file suffixed with NativeComponent.js.
function codegenNativeComponent<Props>(
componentName: string,
options?: Options,
): NativeComponentType<Props> {
if (global.RN$Bridgeless === true) {
const errorMessage =
"Native Component '" +
componentName +
"' that calls codegenNativeComponent was not code generated at build time. Please check its definition.";
console.error(errorMessage);
}
let componentNameInUse =
options && options.paperComponentName != null
? options.paperComponentName
: componentName;
if (options != null && options.paperComponentNameDeprecated != null) {
if (UIManager.hasViewManagerConfig(componentName)) {
componentNameInUse = componentName;
} else if (
options.paperComponentNameDeprecated != null &&
UIManager.hasViewManagerConfig(options.paperComponentNameDeprecated)
) {
// $FlowFixMe[incompatible-type]
componentNameInUse = options.paperComponentNameDeprecated;
} else {
throw new Error(
`Failed to find native component for either ${componentName} or ${
options.paperComponentNameDeprecated ?? '(unknown)'
}`,
);
}
}
return (requireNativeComponent<Props>(
// $FlowFixMe[incompatible-call]
componentNameInUse,
): HostComponent<Props>);
}
export default codegenNativeComponent;