2021-11-21 10:49:18 +00:00
|
|
|
import RcSlider from 'rc-slider';
|
|
|
|
|
|
|
|
import styles from './Slider.module.css';
|
|
|
|
import 'rc-slider/assets/index.css';
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
min: number;
|
|
|
|
max: number;
|
|
|
|
step: number;
|
|
|
|
value: number;
|
|
|
|
onChange: (value: number) => void;
|
2022-11-04 01:12:53 +00:00
|
|
|
// true if you want to always show the tooltip
|
|
|
|
dataCy: string;
|
|
|
|
visibleTooltip?: boolean;
|
2021-11-21 10:49:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 01:12:53 +00:00
|
|
|
export function Slider({
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
step,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
dataCy,
|
|
|
|
visibleTooltip: visible,
|
|
|
|
}: Props) {
|
2021-11-21 10:49:18 +00:00
|
|
|
const SliderWithTooltip = RcSlider.createSliderWithTooltip(RcSlider);
|
2022-11-04 01:12:53 +00:00
|
|
|
// if the tooltip is always visible, hide the marks when tooltip value gets close to the edges
|
2021-11-21 10:49:18 +00:00
|
|
|
const marks = {
|
2022-11-04 01:12:53 +00:00
|
|
|
[min]: visible && value / max < 0.1 ? '' : translateMinValue(min),
|
|
|
|
[max]: visible && value / max > 0.9 ? '' : max.toString(),
|
2021-11-21 10:49:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.root}>
|
|
|
|
<SliderWithTooltip
|
|
|
|
tipFormatter={translateMinValue}
|
|
|
|
min={min}
|
|
|
|
max={max}
|
|
|
|
step={step}
|
|
|
|
marks={marks}
|
|
|
|
defaultValue={value}
|
|
|
|
onAfterChange={onChange}
|
|
|
|
className={styles.slider}
|
2022-11-04 01:12:53 +00:00
|
|
|
tipProps={{ visible }}
|
|
|
|
railStyle={{ height: 8 }}
|
|
|
|
trackStyle={{ height: 8 }}
|
|
|
|
dotStyle={{ visibility: 'hidden' }}
|
|
|
|
data-cy={dataCy}
|
2021-11-21 10:49:18 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateMinValue(value: number) {
|
|
|
|
if (value === 0) {
|
|
|
|
return 'unlimited';
|
|
|
|
}
|
|
|
|
return value.toString();
|
|
|
|
}
|