Created
February 26, 2020 08:31
-
-
Save CGamesPlay/ef5464a0b43ed02bf3a9b62954944f93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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