1import React from 'react';
2import { NativeModules, UIManager, ViewPropTypes, requireNativeComponent } from 'react-native';
3
4// To make the transition from React Native's `requireNativeComponent` to Expo's
5// `requireNativeViewManager` as easy as possible, `requireNativeViewManager` is a drop-in
6// replacement for `requireNativeComponent`.
7//
8// For each view manager, we create a wrapper component that accepts all of the props available to
9// the author of the universal module. This wrapper component splits the props into two sets: props
10// passed to React Native's View (ex: style, testID) and custom view props, which are passed to the
11// adapter view component in a prop called `proxiedProperties`.
12
13// NOTE: React Native is moving away from runtime PropTypes and may remove ViewPropTypes, in which
14// case we will need another way to separate standard React Native view props from other props,
15// which we proxy through the adapter
16const ViewPropTypesKeys = Object.keys(ViewPropTypes);
17
18type NativeExpoComponentProps = {
19  proxiedProperties: object;
20};
21
22/**
23 * A drop-in replacement for `requireNativeComponent`.
24 */
25export function requireNativeViewManager<P = any>(viewName: string): React.ComponentType<P> {
26  if (__DEV__) {
27    const { NativeUnimoduleProxy } = NativeModules;
28    if (!NativeUnimoduleProxy.viewManagersNames.includes(viewName)) {
29      const exportedViewManagerNames = NativeUnimoduleProxy.viewManagersNames.join(', ');
30      console.warn(
31        `The native view manager required by name (${viewName}) from NativeViewManagerAdapter isn't exported by @unimodules/react-native-adapter. Views of this type may not render correctly. Exported view managers: [${exportedViewManagerNames}].`
32      );
33    }
34  }
35
36  // Set up the React Native native component, which is an adapter to the universal module's view
37  // manager
38  const reactNativeViewName = `ViewManagerAdapter_${viewName}`;
39  const ReactNativeComponent =
40    requireNativeComponent<NativeExpoComponentProps>(reactNativeViewName);
41  const reactNativeUIConfiguration = (UIManager.getViewManagerConfig
42    ? UIManager.getViewManagerConfig(reactNativeViewName)
43    : UIManager[reactNativeViewName]) || {
44    NativeProps: {},
45    directEventTypes: {},
46  };
47  const reactNativeComponentPropNames = [
48    'children',
49    ...ViewPropTypesKeys,
50    ...Object.keys(reactNativeUIConfiguration.NativeProps),
51    ...Object.keys(reactNativeUIConfiguration.directEventTypes),
52  ];
53
54  // Define a component for universal-module authors to access their native view manager
55  function NativeComponentAdapter(props, ref) {
56    const nativeProps = pick(props, reactNativeComponentPropNames);
57    const proxiedProps = omit(props, reactNativeComponentPropNames);
58    return <ReactNativeComponent {...nativeProps} proxiedProperties={proxiedProps} ref={ref} />;
59  }
60  NativeComponentAdapter.displayName = `Adapter<${viewName}>`;
61  return React.forwardRef(NativeComponentAdapter);
62}
63
64function omit(props: Record<string, any>, propNames: string[]) {
65  const copied = { ...props };
66  for (const propName of propNames) {
67    delete copied[propName];
68  }
69  return copied;
70}
71
72function pick(props: Record<string, any>, propNames: string[]) {
73  return propNames.reduce((prev, curr) => {
74    if (curr in props) {
75      prev[curr] = props[curr];
76    }
77    return prev;
78  }, {});
79}
80