2023-06-19 17:07:09 +08:00
|
|
|
import type { SliderRef } from 'rc-slider/lib/Slider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import raf from 'rc-util/lib/raf';
|
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useRef } from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { TooltipProps } from '../tooltip';
|
|
|
|
import Tooltip from '../tooltip';
|
2020-05-15 10:47:03 +08:00
|
|
|
|
2023-06-19 17:07:09 +08:00
|
|
|
const SliderTooltip = React.forwardRef<SliderRef, TooltipProps>((props, ref) => {
|
2022-08-26 17:17:13 +08:00
|
|
|
const { open } = props;
|
2020-11-17 12:01:02 +08:00
|
|
|
const innerRef = useRef<any>(null);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
2020-11-17 12:01:02 +08:00
|
|
|
const rafRef = useRef<number | null>(null);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
|
|
|
function cancelKeepAlign() {
|
2020-11-20 12:11:50 +08:00
|
|
|
raf.cancel(rafRef.current!);
|
2020-01-06 20:09:12 +08:00
|
|
|
rafRef.current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function keepAlign() {
|
2020-11-20 12:11:50 +08:00
|
|
|
rafRef.current = raf(() => {
|
2023-03-30 14:45:15 +08:00
|
|
|
innerRef.current?.forceAlign();
|
2020-01-06 20:09:12 +08:00
|
|
|
rafRef.current = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-08-26 17:17:13 +08:00
|
|
|
if (open) {
|
2020-01-06 20:09:12 +08:00
|
|
|
keepAlign();
|
|
|
|
} else {
|
|
|
|
cancelKeepAlign();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cancelKeepAlign;
|
2022-08-26 17:17:13 +08:00
|
|
|
}, [open, props.title]);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
2020-11-17 12:01:02 +08:00
|
|
|
return <Tooltip ref={composeRef(innerRef, ref)} {...props} />;
|
2020-05-15 10:47:03 +08:00
|
|
|
});
|
|
|
|
|
2023-06-19 17:07:09 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
SliderTooltip.displayName = 'SliderTooltip';
|
|
|
|
}
|
|
|
|
|
2020-05-15 10:47:03 +08:00
|
|
|
export default SliderTooltip;
|