antd
Version:
An enterprise-class UI design language and React components implementation
23 lines • 861 B
JavaScript
import * as React from 'react';
import { composeRef, getNodeRef } from "@rc-component/util/es/ref";
import { isPlainObject } from '../../_util/is';
import { FormContext } from '../context';
const useItemRef = () => {
const {
itemRef
} = React.useContext(FormContext);
const cacheRef = React.useRef({});
const getRef = (name, children) => {
// Outer caller already check the `supportRef`
const childrenRef = children && isPlainObject(children) && getNodeRef(children);
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;
};
export default useItemRef;