2024-04-08 14:04:08 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import { useRef } from 'react';
|
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';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
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
|
|
|
|
2024-07-18 19:19:02 +08:00
|
|
|
export type SliderTooltipProps = TooltipProps & {
|
|
|
|
draggingDelete?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const SliderTooltip = React.forwardRef<SliderRef, SliderTooltipProps>((props, ref) => {
|
|
|
|
const { open, draggingDelete } = props;
|
2020-11-17 12:01:02 +08:00
|
|
|
const innerRef = useRef<any>(null);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
2024-07-18 19:19:02 +08:00
|
|
|
const mergedOpen = open && !draggingDelete;
|
|
|
|
|
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(() => {
|
2024-07-18 19:19:02 +08:00
|
|
|
if (mergedOpen) {
|
2020-01-06 20:09:12 +08:00
|
|
|
keepAlign();
|
|
|
|
} else {
|
|
|
|
cancelKeepAlign();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cancelKeepAlign;
|
2024-07-18 19:19:02 +08:00
|
|
|
}, [mergedOpen, props.title]);
|
2020-01-06 20:09:12 +08:00
|
|
|
|
2024-07-18 19:19:02 +08:00
|
|
|
return <Tooltip ref={composeRef(innerRef, ref)} {...props} open={mergedOpen} />;
|
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;
|