-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathRangeSlider.component.tsx
63 lines (59 loc) · 1.63 KB
/
RangeSlider.component.tsx
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
import { ChangeEvent } from 'react';
interface IRangeSliderProps {
id: string;
label: string;
min: number;
max: number;
value: number;
onChange: (value: number) => void;
startValue?: number;
formatValue?: (value: number) => string;
}
/**
* A reusable range slider component with labels
* @function RangeSlider
* @param {string} id - Unique identifier for the slider
* @param {string} label - Accessible label for the slider
* @param {number} min - Minimum value of the range
* @param {number} max - Maximum value of the range
* @param {number} value - Current value of the slider
* @param {function} onChange - Handler for when the slider value changes
* @param {number} startValue - Optional starting value to display (defaults to min)
* @param {function} formatValue - Optional function to format the displayed values
* @returns {JSX.Element} - Rendered component
*/
const RangeSlider = ({
id,
label,
min,
max,
value,
onChange,
startValue = min,
formatValue = (val: number) => val.toString(),
}: IRangeSliderProps) => {
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
onChange(parseInt(e.target.value));
};
return (
<div>
<label htmlFor={id} className="sr-only">
{label}
</label>
<input
id={id}
type="range"
min={min}
max={max}
value={value}
onChange={handleChange}
className="w-full cursor-pointer"
/>
<div className="flex justify-between mt-2">
<span>{formatValue(startValue)}</span>
<span>{formatValue(value)}</span>
</div>
</div>
);
};
export default RangeSlider;