File tree 3 files changed +31
-9
lines changed
3 files changed +31
-9
lines changed Original file line number Diff line number Diff line change 4
4
TransitionProps as RTGTransitionProps ,
5
5
TransitionStatus ,
6
6
} from 'react-transition-group/Transition' ;
7
- import { getReactVersion } from './utils' ;
7
+ import { getChildRef } from './utils' ;
8
8
9
9
export type TransitionProps = RTGTransitionProps & {
10
10
children :
@@ -33,15 +33,8 @@ export default function useRTGTransitionProps({
33
33
children,
34
34
...props
35
35
} : TransitionProps ) {
36
- const { major } = getReactVersion ( ) ;
37
- const childRef =
38
- major >= 19 ? ( children as any ) . props . ref : ( children as any ) . ref ;
39
-
40
36
const nodeRef = useRef < HTMLElement > ( null ) ;
41
- const mergedRef = useMergedRefs (
42
- nodeRef ,
43
- typeof children === 'function' ? null : childRef ,
44
- ) ;
37
+ const mergedRef = useMergedRefs ( nodeRef , getChildRef ( children ) ) ;
45
38
46
39
const normalize =
47
40
( callback ?: ( node : HTMLElement , param : any ) => void ) => ( param : any ) => {
Original file line number Diff line number Diff line change @@ -12,3 +12,14 @@ export function getReactVersion() {
12
12
patch : + parts [ 2 ] ,
13
13
} ;
14
14
}
15
+
16
+ export function getChildRef (
17
+ element ?: React . ReactElement | ( ( ...args : any [ ] ) => React . ReactNode ) | null ,
18
+ ) {
19
+ if ( ! element || typeof element === 'function' ) {
20
+ return null ;
21
+ }
22
+ const { major } = getReactVersion ( ) ;
23
+ const childRef = major >= 19 ? element . props . ref : ( element as any ) . ref ;
24
+ return childRef ;
25
+ }
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest' ;
2
+ import { getChildRef } from '../src/utils' ;
3
+
4
+ describe ( 'utils' , ( ) => {
5
+ describe ( 'getChildRef' , ( ) => {
6
+ it ( 'should return null if ref is null' , ( ) => {
7
+ expect ( getChildRef ( null ) ) . to . equal ( null ) ;
8
+ } ) ;
9
+
10
+ it ( 'should return null if ref is undefined' , ( ) => {
11
+ expect ( getChildRef ( undefined ) ) . to . equal ( null ) ;
12
+ } ) ;
13
+
14
+ it ( 'should return null if ref is a function' , ( ) => {
15
+ expect ( getChildRef ( ( ) => null ) ) . to . equal ( null ) ;
16
+ } ) ;
17
+ } ) ;
18
+ } ) ;
You can’t perform that action at this time.
0 commit comments