-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRangeFilter.jsx
94 lines (86 loc) · 1.84 KB
/
RangeFilter.jsx
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
83
84
85
86
87
88
89
90
91
92
93
94
import * as React from "react";
import { Box, InputLabel, Slider } from "@mui/material";
import PropTypes from "prop-types";
/**
* A range filter allows to filter between two values.
*/
export default function RangeFilter({
label,
min = 0,
max = 100,
value = [min, max],
unit = "%",
onChange
}) {
// state to track the value of the slider without committing it to the controller
const [sliderValue, setSliderValue] = React.useState(value);
React.useEffect(() => {
setSliderValue(value);
}, [value]);
// callback for slider change
const handleChange = (event, newValue) => {
setSliderValue(newValue);
};
// callback for slider change committed
const handleChangeCommitted = (event, newValue) => {
setSliderValue(newValue);
onChange(newValue);
};
// generate marks
const marks = [min, max].map(value => ({
label: unit ? `${value} ${unit}` : value,
value
}));
// return component
return (
<Box px={2} mb={-0.5}>
<InputLabel
sx={{
mb: -1,
ml: -2
}}
shrink
>
{label}
</InputLabel>
<Slider
marks={marks}
min={min}
max={max}
value={sliderValue}
onChange={handleChange}
onChangeCommitted={handleChangeCommitted}
valueLabelDisplay="auto"
/>
</Box>
);
}
// prop types
RangeFilter.propTypes = {
/**
* The label of the filter.
*/
label: PropTypes.string,
/**
* The maximum value.
*/
max: PropTypes.number,
/**
* The minimum value.
*/
min: PropTypes.number,
/**
* Callback function to handle changes
* @param {Array} value - The new value
* @returns {void}
*/
onChange: PropTypes.func,
/**
* The value units.
*/
unit: PropTypes.string,
/**
* The selected value
*/
value: PropTypes.arrayOf(PropTypes.number)
};