codegenNativeComponent.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. // TODO: move this file to shims/ReactNative (requires React update and sync)
  11. 'use strict';
  12. import requireNativeComponent from '../../Libraries/ReactNative/requireNativeComponent';
  13. import type {HostComponent} from '../../Libraries/Renderer/shims/ReactNativeTypes';
  14. import UIManager from '../ReactNative/UIManager';
  15. // TODO: import from CodegenSchema once workspaces are enabled
  16. type Options = $ReadOnly<{|
  17. interfaceOnly?: boolean,
  18. paperComponentName?: string,
  19. paperComponentNameDeprecated?: string,
  20. excludedPlatform?: 'iOS' | 'android',
  21. |}>;
  22. export type NativeComponentType<T> = HostComponent<T>;
  23. function codegenNativeComponent<Props>(
  24. componentName: string,
  25. options?: Options,
  26. ): NativeComponentType<Props> {
  27. let componentNameInUse =
  28. options && options.paperComponentName
  29. ? options.paperComponentName
  30. : componentName;
  31. if (options != null && options.paperComponentNameDeprecated != null) {
  32. if (UIManager.getViewManagerConfig(componentName)) {
  33. componentNameInUse = componentName;
  34. } else if (
  35. options.paperComponentNameDeprecated != null &&
  36. UIManager.getViewManagerConfig(options.paperComponentNameDeprecated)
  37. ) {
  38. componentNameInUse = options.paperComponentNameDeprecated;
  39. } else {
  40. throw new Error(
  41. `Failed to find native component for either ${componentName} or ${options.paperComponentNameDeprecated ||
  42. '(unknown)'}`,
  43. );
  44. }
  45. }
  46. // If this function is run at runtime then that means the view configs were not
  47. // generated with the view config babel plugin, so we need to require the native component.
  48. //
  49. // This will be useful during migration, but eventually this will error.
  50. return (requireNativeComponent<Props>(
  51. componentNameInUse,
  52. ): HostComponent<Props>);
  53. }
  54. export default codegenNativeComponent;