forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.jsx
448 lines (400 loc) · 13.4 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
let React = require('react');
let StylePropable = require('./mixins/style-propable');
let Draggable = require('react-draggable2');
let Transitions = require('./styles/transitions');
let FocusRipple = require('./ripples/focus-ripple');
/**
* Verifies min/max range.
* @param {Object} props Properties of the React component.
* @param {String} propName Name of the property to validate.
* @param {String} componentName Name of the component whose property is being validated.
* @returns {Object} Returns an Error if min >= max otherwise null.
*/
let minMaxPropType = (props, propName, componentName) => {
let error = React.PropTypes.number(props, propName, componentName);
if (error !== null) return error;
if (props.min >= props.max) {
let errorMsg = (propName === 'min') ? 'min should be less than max' : 'max should be greater than min';
return new Error(errorMsg);
}
};
/**
* Verifies value is within the min/max range.
* @param {Object} props Properties of the React component.
* @param {String} propName Name of the property to validate.
* @param {String} componentName Name of the component whose property is being validated.
* @returns {Object} Returns an Error if the value is not within the range otherwise null.
*/
let valueInRangePropType = (props, propName, componentName) => {
let error = React.PropTypes.number(props, propName, componentName);
if (error !== null) return error;
let value = props[propName];
if (value < props.min || props.max < value) {
return new Error(propName + ' should be within the range specified by min and max');
}
};
let Slider = React.createClass({
mixins: [StylePropable],
contextTypes: {
muiTheme: React.PropTypes.object,
},
propTypes: {
name: React.PropTypes.string.isRequired,
defaultValue: valueInRangePropType,
description: React.PropTypes.string,
disabled: React.PropTypes.bool,
error: React.PropTypes.string,
max: minMaxPropType,
min: minMaxPropType,
required: React.PropTypes.bool,
step: React.PropTypes.number,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onDragStart: React.PropTypes.func,
onDragStop: React.PropTypes.func,
onFocus: React.PropTypes.func,
value: valueInRangePropType,
},
getDefaultProps() {
return {
defaultValue: 0,
disabled: false,
max: 1,
min: 0,
required: true,
step: 0.01,
};
},
getInitialState() {
let value = this.props.value;
if (value === undefined) {
value = this.props.defaultValue;
}
let percent = (value - this.props.min) / (this.props.max - this.props.min);
if (isNaN(percent)) percent = 0;
return {
active: false,
dragging: false,
focused: false,
hovered: false,
percent: percent,
value: value,
};
},
componentWillReceiveProps(nextProps) {
if (nextProps.value !== undefined) {
this.setValue(nextProps.value);
}
},
getTheme() {
return this.context.muiTheme.component.slider;
},
getStyles() {
let fillGutter = this.getTheme().handleSize / 2;
let disabledGutter = this.getTheme().trackSize + this.getTheme().handleSizeDisabled / 2;
let calcDisabledSpacing = this.props.disabled ? ' - ' + disabledGutter + 'px' : '';
let styles = {
root: {
touchCallout: 'none',
userSelect: 'none',
cursor: 'default',
height: this.getTheme().handleSizeActive,
position: 'relative',
marginTop: 24,
marginBottom: 48,
},
track: {
position: 'absolute',
top: (this.getTheme().handleSizeActive - this.getTheme().trackSize) / 2,
left: 0,
width: '100%',
height: this.getTheme().trackSize,
},
filledAndRemaining: {
position: 'absolute',
top: 0,
height: '100%',
transition: Transitions.easeOut(null, 'margin'),
},
handle: {
boxSizing: 'border-box',
position: 'absolute',
cursor: 'pointer',
pointerEvents: 'inherit',
top: ((this.getTheme().handleSizeActive - this.getTheme().trackSize) / 2) + 'px',
left: '0%',
zIndex: 1,
margin: (this.getTheme().trackSize / 2) + 'px 0 0 0',
width: this.getTheme().handleSize,
height: this.getTheme().handleSize,
backgroundColor: this.getTheme().selectionColor,
backgroundClip: 'padding-box',
border: '0px solid transparent',
borderRadius: '50%',
transform: 'translate(-50%, -50%)',
transition:
Transitions.easeOut('450ms', 'background') + ',' +
Transitions.easeOut('450ms', 'border-color') + ',' +
Transitions.easeOut('450ms', 'width') + ',' +
Transitions.easeOut('450ms', 'height'),
overflow: 'visible',
},
handleWhenDisabled: {
boxSizing: 'content-box',
cursor: 'not-allowed',
backgroundColor: this.getTheme().trackColor,
width: this.getTheme().handleSizeDisabled,
height: this.getTheme().handleSizeDisabled,
border: 'none',
},
handleWhenPercentZero: {
border: this.getTheme().trackSize + 'px solid ' + this.getTheme().handleColorZero,
backgroundColor: this.getTheme().handleFillColor,
boxShadow: 'none',
},
handleWhenPercentZeroAndDisabled: {
cursor: 'not-allowed',
width: this.getTheme().handleSizeDisabled,
height: this.getTheme().handleSizeDisabled,
},
handleWhenPercentZeroAndFocused: {
border: this.getTheme().trackSize + 'px solid ' +
this.getTheme().trackColorSelected,
},
handleWhenActive: {
width: this.getTheme().handleSizeActive,
height: this.getTheme().handleSizeActive,
},
ripple: {
height: this.getTheme().handleSize,
width: this.getTheme().handleSize,
overflow: 'visible',
},
rippleWhenPercentZero: {
top: -this.getTheme().trackSize,
left: -this.getTheme().trackSize,
},
rippleInner: {
height: '300%',
width: '300%',
top: -this.getTheme().handleSize,
left: -this.getTheme().handleSize,
},
};
styles.filled = this.mergeAndPrefix(styles.filledAndRemaining, {
left: 0,
backgroundColor: (this.props.disabled) ?
this.getTheme().trackColor :
this.getTheme().selectionColor,
marginRight: fillGutter,
width: 'calc(' + (this.state.percent * 100) + '%' + calcDisabledSpacing + ')',
});
styles.remaining = this.mergeAndPrefix(styles.filledAndRemaining, {
right: 0,
backgroundColor: this.getTheme().trackColor,
marginLeft: fillGutter,
width: 'calc(' + ((1 - this.state.percent) * 100) + '%' + calcDisabledSpacing + ')',
});
return styles;
},
render() {
let { ...others } = this.props;
let percent = this.state.percent;
if (percent > 1) percent = 1; else if (percent < 0) percent = 0;
let styles = this.getStyles();
let sliderStyles = this.mergeAndPrefix(styles.root, this.props.style);
let handleStyles = percent === 0 ? this.mergeAndPrefix(
styles.handle,
styles.handleWhenPercentZero,
this.state.active && styles.handleWhenActive,
this.state.focused && {outline: 'none'},
(this.state.hovered || this.state.focused) && !this.props.disabled
&& styles.handleWhenPercentZeroAndFocused,
this.props.disabled && styles.handleWhenPercentZeroAndDisabled,
) : this.mergeAndPrefix(
styles.handle,
this.state.active && styles.handleWhenActive,
this.state.focused && {outline: 'none'},
this.props.disabled && styles.handleWhenDisabled
);
let rippleStyle = this.mergeAndPrefix(
styles.ripple,
percent === 0 && styles.rippleWhenPercentZero,
);
let remainingStyles = styles.remaining;
if ((this.state.hovered || this.state.focused) && !this.props.disabled) {
remainingStyles.backgroundColor = this.getTheme().trackColorSelected;
}
let rippleShowCondition = (this.state.hovered || this.state.focused) && !this.state.active;
let rippleColor = this.state.percent === 0 ? this.getTheme().handleColorZero : this.getTheme().rippleColor;
let focusRipple;
if (!this.props.disabled && !this.props.disableFocusRipple) {
focusRipple = (
<FocusRipple
ref="focusRipple"
key="focusRipple"
style={rippleStyle}
innerStyle={styles.rippleInner}
show={rippleShowCondition}
color={rippleColor}/>
);
}
return (
<div {...others } style={this.props.style}>
<span className="mui-input-highlight"></span>
<span className="mui-input-bar"></span>
<span className="mui-input-description">{this.props.description}</span>
<span className="mui-input-error">{this.props.error}</span>
<div style={sliderStyles}
onFocus={this._onFocus}
onBlur={this._onBlur}
onMouseDown={this._onMouseDown}
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
onMouseUp={this._onMouseUp} >
<div ref="track" style={styles.track}>
<div style={styles.filled}></div>
<div style={remainingStyles}></div>
<Draggable axis="x" bound="point"
cancel={this.props.disabled ? '*' : null}
start={{x: (percent * 100) + '%'}}
constrain={this._constrain()}
onStart={this._onDragStart}
onStop={this._onDragStop}
onDrag={this._onDragUpdate}
onMouseDown={this._onMouseDownKnob}>
<div style={handleStyles} tabIndex={0}>
{focusRipple}
</div>
</Draggable>
</div>
</div>
<input ref="input" type="hidden"
name={this.props.name}
value={this.state.value}
required={this.props.required}
min={this.props.min}
max={this.props.max}
step={this.props.step} />
</div>
);
},
getValue() {
return this.state.value;
},
setValue(i) {
// calculate percentage
let percent = (i - this.props.min) / (this.props.max - this.props.min);
if (isNaN(percent)) percent = 0;
// update state
this.setState({
value: i,
percent: percent,
});
},
getPercent() {
return this.state.percent;
},
setPercent(percent) {
let value = this._alignValue(this._percentToValue(percent));
this.setState({value: value, percent: percent});
},
clearValue() {
this.setValue(this.props.min);
},
_alignValue(val) {
let { step, min } = this.props;
let valModStep = (val - min) % step;
let alignValue = val - valModStep;
if (Math.abs(valModStep) * 2 >= step) {
alignValue += (valModStep > 0) ? step : (-step);
}
return parseFloat(alignValue.toFixed(5));
},
_constrain() {
let { min, max, step } = this.props;
return (pos) => {
let pixelMax = React.findDOMNode(this.refs.track).clientWidth;
let pixelStep = pixelMax / ((max - min) / step);
let cursor = min;
let i;
for (i = 0; i < (max - min) / step; i++) {
let distance = (pos.left - cursor);
let nextDistance = (cursor + pixelStep) - pos.left;
if (Math.abs(distance) > Math.abs(nextDistance)) {
cursor += pixelStep;
}
else {
break;
}
}
return {
left: cursor,
};
};
},
_onFocus(e) {
this.setState({focused: true});
if (this.props.onFocus) this.props.onFocus(e);
},
_onBlur(e) {
this.setState({focused: false, active: false});
if (this.props.onBlur) this.props.onBlur(e);
},
_onMouseDown(e) {
if (!this.props.disabled) this._pos = e.clientX;
},
_onMouseEnter() {
this.setState({hovered: true});
},
_onMouseLeave() {
this.setState({hovered: false});
},
_onMouseUp(e) {
if (!this.props.disabled) this.setState({active: false});
if (!this.state.dragging && Math.abs(this._pos - e.clientX) < 5) {
let pos = e.clientX - React.findDOMNode(this).getBoundingClientRect().left;
this._dragX(e, pos);
}
this._pos = undefined;
},
_onMouseDownKnob() {
if (!this.props.disabled) this.setState({active: true});
},
_onDragStart(e, ui) {
this.setState({
dragging: true,
active: true,
});
if (this.props.onDragStart) this.props.onDragStart(e, ui);
},
_onDragStop(e, ui) {
this.setState({
dragging: false,
active: false,
});
if (this.props.onDragStop) this.props.onDragStop(e, ui);
},
_onDragUpdate(e, ui) {
if (!this.state.dragging) return;
if (!this.props.disabled) this._dragX(e, ui.position.left);
},
_dragX(e, pos) {
let max = React.findDOMNode(this.refs.track).clientWidth;
if (pos < 0) pos = 0; else if (pos > max) pos = max;
if (pos === this.props.min) {
return this._updateWithChangeEvent(e, 0);
}
this._updateWithChangeEvent(e, pos / max);
},
_updateWithChangeEvent(e, percent) {
if (this.state.percent === percent) return;
this.setPercent(percent);
let value = this._alignValue(this._percentToValue(percent));
if (this.props.onChange) this.props.onChange(e, value);
},
_percentToValue(percent) {
return percent * (this.props.max - this.props.min) + this.props.min;
},
});
module.exports = Slider;