useItemRef.js 787 B

123456789101112131415161718192021
  1. import * as React from 'react';
  2. import { composeRef, getNodeRef } from "rc-util/es/ref";
  3. import { FormContext } from '../context';
  4. export default function useItemRef() {
  5. const {
  6. itemRef
  7. } = React.useContext(FormContext);
  8. const cacheRef = React.useRef({});
  9. function getRef(name, children) {
  10. // Outer caller already check the `supportRef`
  11. const childrenRef = children && typeof children === 'object' && getNodeRef(children);
  12. const nameStr = name.join('_');
  13. if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
  14. cacheRef.current.name = nameStr;
  15. cacheRef.current.originRef = childrenRef;
  16. cacheRef.current.ref = composeRef(itemRef(name), childrenRef);
  17. }
  18. return cacheRef.current.ref;
  19. }
  20. return getRef;
  21. }