mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-22 22:48:29 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
30 lines
915 B
TypeScript
30 lines
915 B
TypeScript
import * as React from 'react';
|
|
import { composeRef } 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) {
|
|
const childrenRef: React.Ref<React.ReactElement> =
|
|
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;
|
|
}
|