You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
909 B
29 lines
909 B
4 years ago
|
import * as React from 'react';
|
||
|
import { composeRef } from 'rc-util/lib/ref';
|
||
|
import { FormContext } from '../context';
|
||
|
import { InternalNamePath } from '../interface';
|
||
|
|
||
|
export default function useItemRef() {
|
||
|
const { itemRef } = React.useContext(FormContext);
|
||
|
const cacheRef = React.useRef<{
|
||
|
name?: string;
|
||
|
originRef?: React.Ref<any>;
|
||
|
ref?: React.Ref<any>;
|
||
|
}>({});
|
||
|
|
||
|
function getRef(name: InternalNamePath, children: any) {
|
||
|
const childrenRef: React.Ref<React.ReactElement> =
|
||
|
children && typeof children === 'object' && children.ref;
|
||
|
const nameStr = name.join('_');
|
||
|
if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
|
||
|
cacheRef.current.name = nameStr;
|
||
|
cacheRef.current.originRef = childrenRef;
|
||
|
cacheRef.current.ref = composeRef(itemRef(name), childrenRef);
|
||
|
}
|
||
|
|
||
|
return cacheRef.current.ref;
|
||
|
}
|
||
|
|
||
|
return getRef;
|
||
|
}
|