amis-rpc-design/node_modules/antd/es/form/hooks/useItemRef.js
2023-10-07 19:42:30 +08:00

20 lines
716 B
JavaScript

import { composeRef } from "rc-util/es/ref";
import * as React from 'react';
import { FormContext } from '../context';
export default function useItemRef() {
const {
itemRef
} = React.useContext(FormContext);
const cacheRef = React.useRef({});
function getRef(name, children) {
const childrenRef = 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;
}