Skip to content

Instantly share code, notes, and snippets.

@CGamesPlay
Created February 26, 2020 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGamesPlay/ef5464a0b43ed02bf3a9b62954944f93 to your computer and use it in GitHub Desktop.
Save CGamesPlay/ef5464a0b43ed02bf3a9b62954944f93 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
import nouislider from "nouislider";
import "nouislider/distribute/nouislider.min.css";
import "./Slider.css";
class Slider extends Component {
constructor(props) {
super(props);
this.root = React.createRef();
}
componentDidMount() {
this._createSlider(this.root.current);
}
componentDidUpdate(prevProps) {
if (
prevProps.min !== this.props.min ||
prevProps.max !== this.props.max ||
prevProps.start !== this.props.start ||
prevProps.end !== this.props.end
) {
this._destroySlider();
this._createSlider();
}
}
render() {
return <div ref={this.root}></div>;
}
_createSlider() {
nouislider.create(this.root.current, {
start: [this.props.start, this.props.end],
connect: true,
range: {
min: this.props.min,
max: this.props.max,
},
});
const parseChanges = changes =>
changes.map(ch => Math.round(parseFloat(ch)));
this.root.current.noUiSlider.on("change", changes => {
if (this.props.onChange) {
this.props.onChange(...parseChanges(changes));
}
});
this.root.current.noUiSlider.on("update", changes => {
if (this.props.onSlide) {
this.props.onSlide(...parseChanges(changes));
}
});
}
_destroySlider() {
this.root.current.noUiSlider.destroy();
}
}
export default Slider;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment