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

32 lines
625 B
TypeScript
Raw Normal View History

2020-10-07 22:49:01 +08:00
export interface RefObject extends Function {
2020-10-03 15:54:52 +08:00
current?: any;
}
function createRef(): RefObject {
const func: RefObject = (node: any) => {
func.current = node;
};
return func;
}
2020-10-07 22:49:01 +08: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 15:54:52 +08:00
export default createRef;