class Range extends Component {
state = {
value: "0",
step: "500"
};
handleChange = event => {
let eValue = event.target.value;
let step = this.stepRange(eValue);
this.setState({ value: eValue, step: step });
};
stepRange = eValue => {
if (eValue > 1000) {
return 5000;
}
if (eValue > 5000) {
return 10000;
}
return 500;
};
render() {
return (
<div>
<input
type="range"
min="0"
max="10000"
value={this.state.value}
onChange={this.handleChange}
step={this.state.step}
className={modRange.inputRange}
/>
<p>{this.state.value}</p>
</div>
);
}
}
<Range steps={[ 500, 1000, 5000, 10000 ]} />
class Range extends React.Component {
state = {
value: this.props.steps[0],
}
onChange = ({ target: { value } }) => {
this.setState({
value: this.props.steps.reduce((closest, n) =>
Math.abs(closest - value) < Math.abs(n - value) ? closest : n
),
});
}
render() {
const { steps } = this.props;
return (
<div>
<input
type="range"
min={steps[0]}
max={steps[steps.length - 1]}
value={this.state.value}
onChange={this.onChange}
/>
<p>{this.state.value}</p>
</div>
);
}
}