-
-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathTooltipSlider.tsx
More file actions
82 lines (69 loc) · 2.05 KB
/
TooltipSlider.tsx
File metadata and controls
82 lines (69 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import type { SliderProps } from '@rc-component/slider';
import Slider from '@rc-component/slider';
import type { TooltipRef } from '@rc-component/tooltip';
import Tooltip from '@rc-component/tooltip';
import '@rc-component/tooltip/assets/bootstrap.css';
import raf from '@rc-component/util/lib/raf';
import * as React from 'react';
interface HandleTooltipProps {
value: number;
children: React.ReactElement;
visible: boolean;
tipFormatter?: (value: number) => React.ReactNode;
}
const HandleTooltip: React.FC<HandleTooltipProps> = (props) => {
const { value, children, visible, tipFormatter = (val) => `${val} %`, ...restProps } = props;
const tooltipRef = React.useRef<TooltipRef>();
const rafRef = React.useRef<number | null>(null);
function cancelKeepAlign() {
raf.cancel(rafRef.current!);
}
function keepAlign() {
rafRef.current = raf(() => {
tooltipRef.current?.forceAlign();
});
}
React.useEffect(() => {
if (visible) {
keepAlign();
} else {
cancelKeepAlign();
}
return cancelKeepAlign;
}, [value, visible]);
return (
<Tooltip
placement="top"
overlay={tipFormatter(value)}
styles={{ container: { minHeight: 'auto' } }}
ref={tooltipRef}
visible={visible}
{...restProps}
>
{children}
</Tooltip>
);
};
export const handleRender: SliderProps['handleRender'] = (node, props) => (
<HandleTooltip value={props.value} visible={props.dragging}>
{node}
</HandleTooltip>
);
interface TooltipSliderProps extends SliderProps {
tipFormatter?: (value: number) => React.ReactNode;
tipProps?: any;
}
const TooltipSlider: React.FC<TooltipSliderProps> = ({ tipFormatter, tipProps, ...props }) => {
const tipHandleRender: SliderProps['handleRender'] = (node, handleProps) => (
<HandleTooltip
value={handleProps.value}
visible={handleProps.dragging}
tipFormatter={tipFormatter}
{...tipProps}
>
{node}
</HandleTooltip>
);
return <Slider {...props} handleRender={tipHandleRender} />;
};
export default TooltipSlider;