2023-09-24 12:31:06 +00:00
|
|
|
import { ReactElement } from 'react';
|
2021-11-21 10:49:18 +00:00
|
|
|
import RcSlider from 'rc-slider';
|
2023-09-24 12:31:06 +00:00
|
|
|
import { HandleProps } from 'rc-slider/lib/Handles/Handle';
|
2021-11-21 10:49:18 +00:00
|
|
|
|
2023-04-21 04:52:05 +00:00
|
|
|
import { SliderTooltip } from '@@/Tip/SliderTooltip';
|
|
|
|
|
2021-11-21 10:49:18 +00:00
|
|
|
import styles from './Slider.module.css';
|
2023-09-24 12:31:06 +00:00
|
|
|
|
2021-11-21 10:49:18 +00:00
|
|
|
import 'rc-slider/assets/index.css';
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
min: number;
|
|
|
|
max: number;
|
|
|
|
step: number;
|
|
|
|
value: number;
|
2023-04-21 04:52:05 +00:00
|
|
|
onChange: (value: number | number[]) => void;
|
2023-09-24 12:31:06 +00:00
|
|
|
dataCy?: string;
|
2022-11-04 01:12:53 +00:00
|
|
|
// true if you want to always show the tooltip
|
|
|
|
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 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}>
|
2023-04-21 04:52:05 +00:00
|
|
|
<RcSlider
|
|
|
|
handleRender={sliderTooltip}
|
2021-11-21 10:49:18 +00:00
|
|
|
min={min}
|
|
|
|
max={max}
|
|
|
|
marks={marks}
|
2023-04-21 04:52:05 +00:00
|
|
|
step={step}
|
2022-11-04 01:12:53 +00:00
|
|
|
data-cy={dataCy}
|
2023-04-21 04:52:05 +00:00
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
2021-11-21 10:49:18 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function translateMinValue(value: number) {
|
|
|
|
if (value === 0) {
|
|
|
|
return 'unlimited';
|
|
|
|
}
|
|
|
|
return value.toString();
|
|
|
|
}
|
2023-09-24 12:31:06 +00:00
|
|
|
|
|
|
|
function sliderTooltip(
|
|
|
|
node: ReactElement<HandleProps>,
|
|
|
|
handleProps: { value: number }
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<SliderTooltip
|
|
|
|
value={translateMinValue(handleProps.value)}
|
|
|
|
child={node}
|
|
|
|
delay={0}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|