This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathRangeSlider.react.js
308 lines (276 loc) · 8.87 KB
/
RangeSlider.react.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {assoc, omit, pickBy} from 'ramda';
import {Range, createSliderWithTooltip} from 'rc-slider';
import computeSliderStyle from '../utils/computeSliderStyle';
/**
* A double slider with two handles.
* Used for specifying a range of numerical values.
*/
export default class RangeSlider extends Component {
constructor(props) {
super(props);
this.propsToState = this.propsToState.bind(this);
this.DashSlider = props.tooltip
? createSliderWithTooltip(Range)
: Range;
this._computeStyle = computeSliderStyle();
}
propsToState(newProps) {
this.setState({value: newProps.value});
}
componentWillReceiveProps(newProps) {
if (newProps.tooltip !== this.props.tooltip) {
this.DashSlider = newProps.tooltip
? createSliderWithTooltip(Range)
: Range;
}
this.propsToState(newProps);
}
componentWillMount() {
this.propsToState(this.props);
}
render() {
const {
className,
id,
loading_state,
setProps,
tooltip,
updatemode,
vertical,
verticalHeight,
} = this.props;
const value = this.state.value;
let tipProps;
if (tooltip && tooltip.always_visible) {
/**
* clone `tooltip` but with renamed key `always_visible` -> `visible`
* the rc-tooltip API uses `visible`, but `always_visible is more semantic
* assigns the new (renamed) key to the old key and deletes the old key
*/
tipProps = assoc('visible', tooltip.always_visible, tooltip);
delete tipProps.always_visible;
} else {
tipProps = tooltip;
}
const truncatedMarks =
this.props.marks &&
pickBy(
(k, mark) => mark >= this.props.min && mark <= this.props.max,
this.props.marks
);
return (
<div
id={id}
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
className={className}
style={this._computeStyle(vertical, verticalHeight, tooltip)}
>
<this.DashSlider
onChange={value => {
if (updatemode === 'drag') {
setProps({value});
} else {
this.setState({value});
}
}}
onAfterChange={value => {
if (updatemode === 'mouseup') {
setProps({value});
}
}}
tipProps={tipProps}
value={value}
marks={truncatedMarks}
{...omit(
[
'className',
'value',
'setProps',
'marks',
'updatemode',
'verticalHeight',
],
this.props
)}
/>
</div>
);
}
}
RangeSlider.propTypes = {
/**
* The ID of this component, used to identify dash components
* in callbacks. The ID needs to be unique across all of the
* components in an app.
*/
id: PropTypes.string,
/**
* Marks on the slider.
* The key determines the position (a number),
* and the value determines what will show.
* If you want to set the style of a specific mark point,
* the value should be an object which
* contains style and label properties.
*/
marks: PropTypes.objectOf(
PropTypes.oneOfType([
PropTypes.string,
PropTypes.exact({
label: PropTypes.string,
style: PropTypes.object,
}),
])
),
/**
* The value of the input
*/
value: PropTypes.arrayOf(PropTypes.number),
/**
* allowCross could be set as true to allow those handles to cross.
*/
allowCross: PropTypes.bool,
/**
* Additional CSS class for the root DOM node
*/
className: PropTypes.string,
/**
* Determine how many ranges to render, and multiple handles
* will be rendered (number + 1).
*/
count: PropTypes.number,
/**
* If true, the handles can't be moved.
*/
disabled: PropTypes.bool,
/**
* When the step value is greater than 1,
* you can set the dots to true if you want to
* render the slider with dots.
*/
dots: PropTypes.bool,
/**
* If the value is true, it means a continuous
* value is included. Otherwise, it is an independent value.
*/
included: PropTypes.bool,
/**
* Minimum allowed value of the slider
*/
min: PropTypes.number,
/**
* Maximum allowed value of the slider
*/
max: PropTypes.number,
/**
* pushable could be set as true to allow pushing of
* surrounding handles when moving an handle.
* When set to a number, the number will be the
* minimum ensured distance between handles.
*/
pushable: PropTypes.oneOfType([PropTypes.bool, PropTypes.number]),
/**
* Configuration for tooltips describing the current slider values
*/
tooltip: PropTypes.exact({
/**
* Determines whether tooltips should always be visible
* (as opposed to the default, visible on hover)
*/
always_visible: PropTypes.bool,
/**
* Determines the placement of tooltips
* See https://github.com/react-component/tooltip#api
* top/bottom{*} sets the _origin_ of the tooltip, so e.g. `topLeft`
* will in reality appear to be on the top right of the handle
*/
placement: PropTypes.oneOf([
'left',
'right',
'top',
'bottom',
'topLeft',
'topRight',
'bottomLeft',
'bottomRight',
]),
}),
/**
* Value by which increments or decrements are made
*/
step: PropTypes.number,
/**
* If true, the slider will be vertical
*/
vertical: PropTypes.bool,
/**
* The height, in px, of the slider if it is vertical.
*/
verticalHeight: PropTypes.number,
/**
* Determines when the component should update
* its value. If `mouseup`, then the slider
* will only trigger its value when the user has
* finished dragging the slider. If `drag`, then
* the slider will update its value continuously
* as it is being dragged.
* Only use `drag` if your updates are fast.
*/
updatemode: PropTypes.oneOf(['mouseup', 'drag']),
/**
* Dash-assigned callback that gets fired when the value changes.
*/
setProps: PropTypes.func,
/**
* Object that holds the loading state object coming from dash-renderer
*/
loading_state: PropTypes.shape({
/**
* Determines if the component is loading or not
*/
is_loading: PropTypes.bool,
/**
* Holds which property is loading
*/
prop_name: PropTypes.string,
/**
* Holds the name of the component that is loading
*/
component_name: PropTypes.string,
}),
/**
* Used to allow user interactions in this component to be persisted when
* the component - or the page - is refreshed. If `persisted` is truthy and
* hasn't changed from its previous value, a `value` that the user has
* changed while using the app will keep that change, as long as
* the new `value` also matches what was given originally.
* Used in conjunction with `persistence_type`.
*/
persistence: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]),
/**
* Properties whose user interactions will persist after refreshing the
* component or the page. Since only `value` is allowed this prop can
* normally be ignored.
*/
persisted_props: PropTypes.arrayOf(PropTypes.oneOf(['value'])),
/**
* Where persisted user changes will be stored:
* memory: only kept in memory, reset on page refresh.
* local: window.localStorage, data is kept after the browser quit.
* session: window.sessionStorage, data is cleared once the browser quit.
*/
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
};
RangeSlider.defaultProps = {
updatemode: 'mouseup',
persisted_props: ['value'],
persistence_type: 'local',
verticalHeight: 400,
};