1
- /* eslint-disable no-param-reassign */
2
1
import type * as React from 'react' ;
3
2
import { isValidElement } from 'react' ;
4
3
import { ForwardRef , isFragment , isMemo } from 'react-is' ;
5
4
import useMemo from './hooks/useMemo' ;
6
5
7
- export function fillRef < T > ( ref : React . Ref < T > , node : T ) {
6
+ export const fillRef = < T > ( ref : React . Ref < T > , node : T ) => {
8
7
if ( typeof ref === 'function' ) {
9
8
ref ( node ) ;
10
9
} else if ( typeof ref === 'object' && ref && 'current' in ref ) {
11
10
( ref as any ) . current = node ;
12
11
}
13
- }
12
+ } ;
14
13
15
14
/**
16
15
* Merge refs into one ref function to support ref passing.
17
16
*/
18
- export function composeRef < T > ( ...refs : React . Ref < T > [ ] ) : React . Ref < T > {
19
- const refList = refs . filter ( ref => ref ) ;
17
+ export const composeRef = < T > ( ...refs : React . Ref < T > [ ] ) : React . Ref < T > = > {
18
+ const refList = refs . filter ( Boolean ) ;
20
19
if ( refList . length <= 1 ) {
21
20
return refList [ 0 ] ;
22
21
}
23
-
24
22
return ( node : T ) => {
25
23
refs . forEach ( ref => {
26
24
fillRef ( ref , node ) ;
27
25
} ) ;
28
26
} ;
29
- }
27
+ } ;
30
28
31
- export function useComposeRef < T > ( ...refs : React . Ref < T > [ ] ) : React . Ref < T > {
29
+ export const useComposeRef = < T > ( ...refs : React . Ref < T > [ ] ) : React . Ref < T > = > {
32
30
return useMemo (
33
31
( ) => composeRef ( ...refs ) ,
34
32
refs ,
35
33
( prev , next ) =>
36
34
prev . length !== next . length || prev . every ( ( ref , i ) => ref !== next [ i ] ) ,
37
35
) ;
38
- }
36
+ } ;
39
37
40
- export function supportRef ( nodeOrComponent : any ) : boolean {
38
+ export const supportRef = ( nodeOrComponent : any ) : boolean => {
41
39
const type = isMemo ( nodeOrComponent )
42
40
? nodeOrComponent . type . type
43
41
: nodeOrComponent . type ;
@@ -60,17 +58,16 @@ export function supportRef(nodeOrComponent: any): boolean {
60
58
return false ;
61
59
}
62
60
return true ;
63
- }
61
+ } ;
64
62
65
- export function supportNodeRef < T = any > (
63
+ export const supportNodeRef = < T = any > (
66
64
node : React . ReactNode ,
67
- ) : node is React . ReactElement & React . RefAttributes < T > {
65
+ ) : node is React . ReactElement & React . RefAttributes < T > => {
68
66
if ( ! isValidElement ( node ) ) {
69
67
return false ;
70
68
}
71
69
if ( isFragment ( node ) ) {
72
70
return false ;
73
71
}
74
72
return supportRef ( node ) ;
75
- }
76
- /* eslint-enable */
73
+ } ;
0 commit comments