Skip to content

Commit

Permalink
Merge pull request #10 from alexmasnou/master
Browse files Browse the repository at this point in the history
Update slider range when passing new props
  • Loading branch information
mmarkelov committed Apr 9, 2019
2 parents e575d72 + 09fffad commit b263aa2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
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);
});
});

0 comments on commit b263aa2

Please sign in to comment.