@@ -16,25 +16,30 @@ export function useChildrenWithRefs(
16
16
cloneEl ?: (
17
17
element : ReactElement < any , JSXElementConstructor < any > | string > ,
18
18
props : { ref : ( el : HTMLElement ) => void } ,
19
+ index : number ,
19
20
) => ReactElement ,
20
21
) : [ children : ReactNode , refs : ( HTMLElement | undefined ) [ ] ] {
21
22
return useMemo ( ( ) => {
22
23
const refs : ( HTMLElement | undefined ) [ ] = [ ] ;
23
- const clone = cloneEl || cloneElement ;
24
+ const clone = cloneEl || ( ( element , props ) => cloneElement ( element , props ) ) ;
24
25
const newChildren = Children . map ( children , ( child , index ) => {
25
26
if ( ! isValidElement ( child ) ) return child ;
26
- return clone ( child , {
27
- ref : ( el : HTMLElement ) => {
28
- refs [ index ] = el ;
29
- // ref 现在是直接放在 ReactElement 上的,props 虽然也有,但取的是 undefined
30
- const originRef = ( child as any ) . ref ;
31
- // const originRef = child.props.ref;
32
- if ( ! originRef ) return ;
33
- // 连通原来的 ref
34
- if ( typeof originRef === 'function' ) originRef ( el ) ;
35
- else originRef . current = el ;
27
+ return clone (
28
+ child ,
29
+ {
30
+ ref : ( el : HTMLElement ) => {
31
+ refs [ index ] = el ;
32
+ // ref 现在是直接放在 ReactElement 上的,props 虽然也有,但取的是 undefined
33
+ const originRef = ( child as any ) . ref ;
34
+ // const originRef = child.props.ref;
35
+ if ( ! originRef ) return ;
36
+ // 连通原来的 ref
37
+ if ( typeof originRef === 'function' ) originRef ( el ) ;
38
+ else originRef . current = el ;
39
+ } ,
36
40
} ,
37
- } ) ;
41
+ index ,
42
+ ) ;
38
43
} ) ;
39
44
return [ newChildren , refs ] ;
40
45
} , [ children ] ) ;
0 commit comments