Skip to content

Commit

Permalink
Merge pull request #1587 from KapJI/slider-perf
Browse files Browse the repository at this point in the history
[Slider] tiny performance improvement, code refactoring.
  • Loading branch information
shaurya947 committed Sep 29, 2015
2 parents fc0713e + 4ec0543 commit 4dcdae9
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions src/slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,36 +346,17 @@ const Slider = React.createClass({

_alignValue(val) {
let { step, min } = this.props;

let valModStep = (val - min) % step;
let alignValue = val - valModStep;

if (Math.abs(valModStep) * 2 >= step) {
alignValue += (valModStep > 0) ? step : (-step);
}

let alignValue = Math.round((val - min) / step) * step + min;
return parseFloat(alignValue.toFixed(5));
},

_constrain() {
let { min, max, step } = this.props;
let steps = (max - min) / step;
return (pos) => {
let pixelMax = React.findDOMNode(this.refs.track).clientWidth;
let pixelStep = pixelMax / ((max - min) / step);

let cursor = min;
let i;
for (i = 0; i < (max - min) / step; i++) {
let distance = (pos.left - cursor);
let nextDistance = (cursor + pixelStep) - pos.left;
if (Math.abs(distance) > Math.abs(nextDistance)) {
cursor += pixelStep;
}
else {
break;
}
}

let pixelStep = pixelMax / steps;
let cursor = Math.round(pos.left / pixelStep) * pixelStep;
return {
left: cursor,
};
Expand Down Expand Up @@ -442,9 +423,6 @@ const Slider = React.createClass({
_dragX(e, pos) {
let max = React.findDOMNode(this.refs.track).clientWidth;
if (pos < 0) pos = 0; else if (pos > max) pos = max;
if (pos === this.props.min) {
return this._updateWithChangeEvent(e, 0);
}
this._updateWithChangeEvent(e, pos / max);
},

Expand Down

0 comments on commit 4dcdae9

Please sign in to comment.