ant-design-vue/components/_util/createRef.ts

32 lines
619 B
TypeScript
Raw Normal View History

2020-10-07 14:49:01 +00:00
export interface RefObject extends Function {
2020-10-03 07:54:52 +00:00
current?: any;
}
2020-10-17 04:14:13 +00:00
function createRef(): any {
2020-10-03 07:54:52 +00:00
const func: RefObject = (node: any) => {
func.current = node;
};
return func;
}
2020-10-07 14:49:01 +00:00
export function fillRef<T>(ref, node: T) {
if (typeof ref === 'function') {
ref(node);
} else if (typeof ref === 'object' && ref && 'current' in ref) {
(ref as any).current = node;
}
}
/**
* Merge refs into one ref function to support ref passing.
*/
export function composeRef<T>(...refs: any[]) {
return (node: T) => {
refs.forEach(ref => {
fillRef(ref, node);
});
};
}
2020-10-03 07:54:52 +00:00
export default createRef;