Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update slider range when passing new props #10

Merged
merged 2 commits into from
Apr 9, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ class Nouislider extends React.Component {
}

shouldComponentUpdate(nextProps) {
const { start, disabled } = this.props;
return !isEqual(nextProps.start, start) || nextProps.disabled !== disabled;
const { start, disabled, range } = this.props;
return (
!isEqual(nextProps.start, start) ||
nextProps.disabled !== disabled ||
nextProps.range !== range
Copy link
Owner

@mmarkelov mmarkelov Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range is object, so it will be always true - and this is not what I actually want.
You can use isEqual from utils, but you should also make some changes to support objects

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mmarkelov, sorry for the mistake. I fixed it in the last commit. I used a simple function to compare objects. Maybe in the future it will be interesting to use a more robust method like _.isEqual from lodash.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexmasnou I should think about it. I don't want to include this kind of dependencies and want to make library as simple as possible

);
}

componentDidUpdate() {
const { start, disabled } = this.props;
const { start, disabled, range } = this.props;
this.slider.set(start);
this.toggleDisable(disabled);
this.updateRange(range);
}

componentWillUnmount() {
Expand Down Expand Up @@ -59,6 +64,11 @@ class Nouislider extends React.Component {
}
};

updateRange = range => {
const sliderHTML = this.sliderContainer.current;
sliderHTML.noUiSlider.updateOptions({ range });
};

createSlider() {
const { onUpdate, onChange, onSlide, onStart, onEnd, onSet } = this.props;
const slider = nouislider.create(this.sliderContainer.current, {
Expand Down