mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-16 22:18:28 +02:00

* refactor: update ref logic * chore: clean up * chore: coverage adjust * test: coverage * chore: back of code
31 lines
987 B
TypeScript
31 lines
987 B
TypeScript
import * as React from 'react';
|
|
import { composeRef, getNodeRef } from 'rc-util/lib/ref';
|
|
|
|
import { FormContext } from '../context';
|
|
import type { 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) {
|
|
// Outer caller already check the `supportRef`
|
|
const childrenRef: React.Ref<React.ReactElement> =
|
|
children && typeof children === 'object' && 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;
|
|
}
|