2020-01-06 20:09:12 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import Tooltip, { TooltipProps } from '../tooltip';
|
|
|
|
|
2020-05-15 10:47:03 +08:00
|
|
|
function useCombinedRefs(
|
|
|
|
...refs: Array<React.MutableRefObject<unknown> | ((instance: unknown) => void) | null>
|
|
|
|
) {
|
|
|
|
const targetRef = React.useRef();
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
refs.forEach(ref => {
|
|
|
|
if (!ref) return;
|
|
|
|
|
|
|
|
if (typeof ref === 'function') {
|
|
|
|
ref(targetRef.current);
|
|
|
|
} else {
|
|
|
|
ref.current = targetRef.current;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, [refs]);
|
|
|
|
|
|
|
|
return targetRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SliderTooltip = React.forwardRef<unknown, TooltipProps>((props, ref) => {
|
2020-01-06 20:09:12 +08:00
|
|
|
const { visible } = props;
|
2020-05-15 10:47:03 +08:00
|
|
|
const innerRef = React.useRef<any>(null);
|
|
|
|
const tooltipRef = useCombinedRefs(ref, innerRef);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
|
|
|
const rafRef = React.useRef<number | null>(null);
|
|
|
|
|
|
|
|
function cancelKeepAlign() {
|
|
|
|
window.cancelAnimationFrame(rafRef.current!);
|
|
|
|
rafRef.current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function keepAlign() {
|
|
|
|
if (rafRef.current !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rafRef.current = window.requestAnimationFrame(() => {
|
2020-05-15 10:47:03 +08:00
|
|
|
(tooltipRef.current as any).forcePopupAlign();
|
2020-01-06 20:09:12 +08:00
|
|
|
|
|
|
|
rafRef.current = null;
|
|
|
|
keepAlign();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (visible) {
|
|
|
|
keepAlign();
|
|
|
|
} else {
|
|
|
|
cancelKeepAlign();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cancelKeepAlign;
|
|
|
|
}, [visible]);
|
|
|
|
|
|
|
|
return <Tooltip ref={tooltipRef} {...props} />;
|
2020-05-15 10:47:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
export default SliderTooltip;
|