-
Notifications
You must be signed in to change notification settings - Fork 186
/
ref.ts
106 lines (93 loc) · 2.64 KB
/
ref.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type * as React from 'react';
import { isValidElement } from 'react';
import { ForwardRef, isMemo } from 'react-is';
import useMemo from './hooks/useMemo';
import isFragment from './React/isFragment';
export const fillRef = <T>(ref: React.Ref<T>, node: T) => {
if (typeof ref === 'function') {
ref(node);
} else if (typeof ref === 'object' && ref && 'current' in ref) {
(ref as any).current = node;
}
};
/**
* Merge refs into one ref function to support ref passing.
*/
export const composeRef = <T>(...refs: React.Ref<T>[]): React.Ref<T> => {
const refList = refs.filter(Boolean);
if (refList.length <= 1) {
return refList[0];
}
return (node: T) => {
refs.forEach(ref => {
fillRef(ref, node);
});
};
};
export const useComposeRef = <T>(...refs: React.Ref<T>[]): React.Ref<T> => {
return useMemo(
() => composeRef(...refs),
refs,
(prev, next) =>
prev.length !== next.length || prev.every((ref, i) => ref !== next[i]),
);
};
export const supportRef = (nodeOrComponent: any): boolean => {
if (!nodeOrComponent) {
return false;
}
// React 19 no need `forwardRef` anymore. So just pass if is a React element.
if (
isReactElement(nodeOrComponent) &&
(nodeOrComponent as any).props.propertyIsEnumerable('ref')
) {
return true;
}
const type = isMemo(nodeOrComponent)
? nodeOrComponent.type.type
: nodeOrComponent.type;
// Function component node
if (
typeof type === 'function' &&
!type.prototype?.render &&
type.$$typeof !== ForwardRef
) {
return false;
}
// Class component
if (
typeof nodeOrComponent === 'function' &&
!nodeOrComponent.prototype?.render &&
nodeOrComponent.$$typeof !== ForwardRef
) {
return false;
}
return true;
};
interface RefAttributes<T> extends React.Attributes {
ref: React.Ref<T>;
}
function isReactElement(node: React.ReactNode) {
return isValidElement(node) && !isFragment(node);
}
export const supportNodeRef = <T = any>(
node: React.ReactNode,
): node is React.ReactElement & RefAttributes<T> => {
return isReactElement(node) && supportRef(node);
};
/**
* In React 19. `ref` is not a property from node.
* But a property from `props.ref`.
* To check if `props.ref` exist or fallback to `ref`.
*/
export const getNodeRef: <T = any>(
node: React.ReactNode,
) => React.Ref<T> | null = node => {
if (node && isReactElement(node)) {
const ele = node as any;
// Source from:
// https://github.com/mui/material-ui/blob/master/packages/mui-utils/src/getReactNodeRef/getReactNodeRef.ts
return ele.props.propertyIsEnumerable('ref') ? ele.props.ref : ele.ref;
}
return null;
};