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
Show file tree
Hide file tree
Changes from all commits
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 ||
!isEqual(nextProps.range, range)
);
}

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
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,27 @@ export const isEqual = (val1, val2) => {
if (Array.isArray(val1) && Array.isArray(val2)) {
return JSON.stringify(val1) === JSON.stringify(val2);
}
if (typeof val1 === "object" && typeof val2 === "object") {
return isEquivalentObject(val1, val2);
}
return false;
};

const isEquivalentObject = (obj1, obj2) => {
var aProps = Object.getOwnPropertyNames(obj1);
var bProps = Object.getOwnPropertyNames(obj2);

if (aProps.length != bProps.length) {
return false;
}

for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];

if (obj1[propName] !== obj2[propName]) {
return false;
}
}

return true;
};
8 changes: 8 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ describe('isEqual function', () => {
expect(isEqual(['a', 3], ['a', 3])).toBe(true);
});

it('Return right result for comparing objects', () => {
expect(isEqual({ min: 3, max: 3 }, { min: 3, max: 3 })).toBe(true);
});

it('Return false for comparing values with different types', () => {
expect(isEqual(3, '3')).toBe(false);
});

it('Return false for comparing objects with different values', () => {
expect(isEqual({ min: 3, max: 3 }, { min: 3, max: 1 })).toBe(false);
});
});