-
Notifications
You must be signed in to change notification settings - Fork 3
/
Slider.jsx
195 lines (191 loc) · 4.9 KB
/
Slider.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Box, Slider as MuiSlider, Typography } from "@mui/material";
import PropTypes from "prop-types";
import React from "react";
/**
* Discrete slider component
*/
export default function Slider({
color = "primary",
disabled = false,
labelStyle = {},
labelPosition = "bottom",
labels = [],
max = 10,
min = 1,
onChange = () => {},
onChangeCommitted = () => {},
orientation = "horizontal",
showLabels = true,
step = 1,
title,
value,
valueLabelDisplay = "auto"
}) {
// get ticks labels if they are not pre defined
const range = (start, stop, step) =>
Array.from(
{ length: (stop - start) / step + 1 },
(_, i) => start + i * step
);
// count decimal places in a number
function countDecimals(value) {
return value % 1 ? value.toString().split(".")[1].length : 0;
}
// show/hide labels
let marks = [];
let stepSize = step;
if (showLabels) {
if (!labels.length && step !== 0) {
range(min, max, stepSize).map(item => {
const thisMark = {
label: String(item.toFixed(countDecimals(step))),
value: item
};
marks.push(thisMark);
return marks;
});
} else {
marks = labels;
stepSize = null;
}
} else {
marks = false;
}
// set label position based on orientation
let style = {};
if (orientation === "horizontal") {
style = {
position: "absolute",
top: labelPosition === "top" ? "-19px" : "30px"
};
} else {
style = {
left: labelPosition === "left" ? "-11px" : "32px",
position: "absolute"
};
}
// return components
return (
<Box sx={{ height: "100%" }} display="flex" flexDirection="column">
<Typography>{title}</Typography>
<Box
sx={{
height: "100%",
paddingBottom: orientation === "vertical" ? "15px" : "0px",
paddingLeft: orientation === "horizontal" ? "10px" : "0px",
paddingRight: orientation === "horizontal" ? "10px" : "0px",
paddingTop: orientation === "vertical" ? "5px" : "0px"
}}
>
<MuiSlider
sx={{
"& .MuiSlider-markLabel": {
...labelStyle,
...style
},
'& input[type="range"]': {
WebkitAppearance: `slider-${orientation}`
},
color: { color },
marginLeft:
orientation === "vertical" && labelPosition === "left"
? "20px"
: "0px",
marginTop:
orientation === "horizontal" && labelPosition === "top"
? "20px"
: "5px"
}}
disabled={disabled}
marks={marks}
max={max}
min={min}
onChange={onChange}
onChangeCommitted={onChangeCommitted}
orientation={orientation}
step={stepSize}
value={typeof value !== "undefined" ? value : null}
valueLabelDisplay={valueLabelDisplay}
/>
</Box>
</Box>
);
}
Slider.propTypes = {
/**
* The color of the component
*/
color: PropTypes.string,
/**
* If true, the label, input and helper text should be displayed in a disabled state.
*/
disabled: PropTypes.bool,
/**
* Tick label position respective to slide
*/
labelPosition: PropTypes.oneOf(["bottom", "top", "left", "right"]),
/**
* Custom style to apply to the labels
*/
labelStyle: PropTypes.object,
/**
* Indicates predeterminated values to which the user can move the slider.
* It should contain objects with "value" and optional "label" keys.
*/
labels: PropTypes.array,
/**
* The maximum allowed value of the slider
*/
max: PropTypes.number,
/**
* The minimum allowed value of the slider
*/
min: PropTypes.number,
/**
* Callback fired when the value is changed.
*
* **Signature**
* ```
* function(event: object, value: number) => void
* ```
* _event_: The event source of the callback. You can pull out the new value by accessing `event.target.value` (any).
* _value_: The new value.
*/
onChange: PropTypes.func,
/**
* Callback fired when the value is changed.
*
* **Signature**
* ```
* function(event: object, value: number) => void
* ```
* _event_: The event source of the callback.
* _value_: The new value.
*/
onChangeCommitted: PropTypes.func,
/**
* Slider orientation
*/
orientation: PropTypes.oneOf(["horizontal", "vertical"]),
/**
* If true, tick labels should be displayed
*/
showLabels: PropTypes.bool,
/**
* The step of slider
*/
step: PropTypes.number,
/**
* Slider title
*/
title: PropTypes.string,
/**
* The input value
*/
value: PropTypes.number,
/**
* If auto the value label will display when the thumb is hovered;
* if on, will diplay persistently and if off will never display
*/
valueLabelDisplay: PropTypes.oneOf(["auto", "off", "on"])
};