UNPKG

2.27 kBPlain TextView Raw
1'use strict';
2/* eslint-disable */
3
4import type { ShadowNodeWrapper } from './commonTypes';
5
6let findHostInstance_DEPRECATED: (ref: unknown) => void;
7let getInternalInstanceHandleFromPublicInstance: (ref: unknown) => {
8 stateNode: { node: unknown };
9};
10
11export function getShadowNodeWrapperFromRef(
12 ref: React.Component
13): ShadowNodeWrapper {
14 // load findHostInstance_DEPRECATED lazily because it may not be available before render
15 if (findHostInstance_DEPRECATED === undefined) {
16 try {
17 findHostInstance_DEPRECATED =
18 require('react-native/Libraries/Renderer/shims/ReactFabric').findHostInstance_DEPRECATED;
19 } catch (e) {
20 findHostInstance_DEPRECATED = (_ref: unknown) => null;
21 }
22 }
23
24 if (getInternalInstanceHandleFromPublicInstance === undefined) {
25 try {
26 getInternalInstanceHandleFromPublicInstance =
27 require('react-native/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricPublicInstance')
28 .getInternalInstanceHandleFromPublicInstance ??
29 ((_ref: any) => _ref._internalInstanceHandle);
30 } catch (e) {
31 getInternalInstanceHandleFromPublicInstance = (_ref: any) =>
32 _ref._internalInstanceHandle;
33 }
34 }
35
36 // taken from https://github.com/facebook/react-native/commit/803bb16531697233686efd475f004c1643e03617#diff-d8172256c6d63b5d32db10e54d7b10f37a26b337d5280d89f5bfd7bcea778292R196
37 // @ts-ignore some weird stuff on RN 0.74 - see examples with scrollView
38 const scrollViewRef = ref?.getScrollResponder?.()?.getNativeScrollRef?.();
39 // @ts-ignore some weird stuff on RN 0.74 - see examples with scrollView
40 const otherScrollViewRef = ref?.getNativeScrollRef?.();
41 // @ts-ignore some weird stuff on RN 0.74 - see setNativeProps example
42 const textInputRef = ref?.__internalInstanceHandle?.stateNode?.node;
43
44 let resolvedRef;
45 if (scrollViewRef) {
46 resolvedRef = scrollViewRef.__internalInstanceHandle.stateNode.node;
47 } else if (otherScrollViewRef) {
48 resolvedRef = otherScrollViewRef.__internalInstanceHandle.stateNode.node;
49 } else if (textInputRef) {
50 resolvedRef = textInputRef;
51 } else {
52 resolvedRef = getInternalInstanceHandleFromPublicInstance(
53 findHostInstance_DEPRECATED(ref)
54 ).stateNode.node;
55 }
56
57 return resolvedRef;
58}