/
RangeSlider.js
120 lines (102 loc) · 3.22 KB
/
RangeSlider.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
import React, { Component } from 'react';
import { arrayOf, number, shape, string } from 'prop-types';
import classNames from 'classnames';
import { withDimensions } from '../../util/contextHelpers';
import Handle from './Handle';
import Track from './Track';
import css from './RangeSlider.css';
class RangeSliderComponent extends Component {
constructor(props) {
super(props);
const { min, max, handles } = props;
handles.forEach((h, index) => {
if (h < min || h > max || (index < handles.length - 1 && h > handles[index + 1])) {
throw new Error(
'RangeSlider error: handles need to be given in ascending order and they need to be within min and max values'
);
}
});
this.state = { activeHandle: 0 };
this.toPosition = this.toPosition.bind(this);
this.toValue = this.toValue.bind(this);
this.changeActive = this.changeActive.bind(this);
this.onChange = this.onChange.bind(this);
}
toPosition(value) {
const { dimensions, min, max } = this.props;
const width = dimensions.width;
const valueOffset = value - min;
const scale = max - min;
return Math.round((valueOffset / scale) * width);
}
toValue(position) {
const { dimensions, min, max, step } = this.props;
const width = dimensions.width;
const scale = max - min;
const value = Math.round((position / width) * scale) + min;
return Math.ceil(value / step) * step;
}
changeActive(index) {
this.setState({ activeHandle: index });
}
onChange(position, handleIndex) {
this.props.onChange(Object.assign([...this.props.handles], { [handleIndex]: position }));
}
render() {
const { handles, min, max } = this.props;
return (
<Track handles={handles} valueToPosition={this.toPosition}>
{handles.map((h, index) => {
const classes = classNames({ [css.activeHandle]: this.state.activeHandle === index });
return (
<Handle
key={index}
className={classes}
value={h}
min={index === 0 ? min : handles[index - 1]}
max={index === handles.length - 1 ? max : handles[index + 1]}
valueToPosition={this.toPosition}
positionToValue={this.toValue}
changeActive={() => this.changeActive(index)}
onChange={value => this.onChange(value, index)}
/>
);
})}
</Track>
);
}
}
RangeSliderComponent.defaultProps = {
min: 0,
max: 10000000,
step: 1,
};
RangeSliderComponent.propTypes = {
handles: arrayOf(number),
min: number,
max: number,
step: number,
dimensions: shape({
height: number.isRequired,
width: number.isRequired,
}).isRequired,
};
const RangeSliderComponentWithDimensions = withDimensions(RangeSliderComponent);
const RangeSlider = props => {
const { rootClassName, className, ...rest } = props;
const classes = classNames(rootClassName || css.root, className);
return (
<div className={classes}>
<RangeSliderComponentWithDimensions {...rest} />
</div>
);
};
RangeSlider.defaultProps = {
rootClassName: null,
className: null,
};
RangeSlider.propTypes = {
rootClassName: string,
className: string,
};
export default RangeSlider;