ant-design/components/slider/SliderTooltip.tsx
偏右 83ddfb807e
test: Enable transition and animation in jest (#26571)
* test:  add test cases to increase coverage

* test: fix snapshot

* fix: test node envioronment

* fix: test node envioronment

* fix test case

* test: refactor menu test cases

* test: update test code style

* test: add more menu test cases

* add comment

* test: update snapshot

* fix: alert test case

* fix: form onLeaveEnd test cov

* test: fix button click wave test cases

* chore: clean up snapshots
2020-09-18 16:53:18 +08:00

58 lines
1.3 KiB
TypeScript

import * as React from 'react';
import Tooltip, { TooltipProps } from '../tooltip';
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) => {
const { visible } = props;
const innerRef = React.useRef<any>(null);
const tooltipRef = useCombinedRefs(ref, innerRef);
const rafRef = React.useRef<number | null>(null);
function cancelKeepAlign() {
window.cancelAnimationFrame(rafRef.current!);
rafRef.current = null;
}
function keepAlign() {
rafRef.current = window.requestAnimationFrame(() => {
(tooltipRef.current as any).forcePopupAlign();
rafRef.current = null;
keepAlign();
});
}
React.useEffect(() => {
if (visible) {
keepAlign();
} else {
cancelKeepAlign();
}
return cancelKeepAlign;
}, [visible]);
return <Tooltip ref={tooltipRef} {...props} />;
});
export default SliderTooltip;