useItemRef.js 1008 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = useItemRef;
  7. var React = _interopRequireWildcard(require("react"));
  8. var _ref = require("rc-util/lib/ref");
  9. var _context = require("../context");
  10. function useItemRef() {
  11. const {
  12. itemRef
  13. } = React.useContext(_context.FormContext);
  14. const cacheRef = React.useRef({});
  15. function getRef(name, children) {
  16. // Outer caller already check the `supportRef`
  17. const childrenRef = children && typeof children === 'object' && (0, _ref.getNodeRef)(children);
  18. const nameStr = name.join('_');
  19. if (cacheRef.current.name !== nameStr || cacheRef.current.originRef !== childrenRef) {
  20. cacheRef.current.name = nameStr;
  21. cacheRef.current.originRef = childrenRef;
  22. cacheRef.current.ref = (0, _ref.composeRef)(itemRef(name), childrenRef);
  23. }
  24. return cacheRef.current.ref;
  25. }
  26. return getRef;
  27. }