Skip to content

Commit

Permalink
Merge pull request #860 from crenwick/sliderStep
Browse files Browse the repository at this point in the history
[Slider] Take and use step props
  • Loading branch information
Hai Nguyen committed Jun 16, 2015
2 parents 239ad0c + cf6cf8d commit c34504a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 10 additions & 4 deletions docs/src/app/components/pages/components/sliders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SlidersPage extends React.Component {
'// Default\n' +
'<Slider name="slider1" />\n\n' +
'// With starting value\n' +
'<Slider name="slider2" defaultValue={0.5} />\n' +
'<Slider name="slider2" defaultValue={0.5} step={0.10} />\n' +
'<Slider name="slider3" defaultValue={1} />\n\n' +
'// Disabled with fixed value\n' +
'<Slider name="slider1" disabled={true} />\n' +
Expand All @@ -40,6 +40,12 @@ class SlidersPage extends React.Component {
desc: 'The minimum value the slider can slide to on a scale from ' +
'0 to 1 inclusive.'
},
{
name: 'step',
type: 'number',
header: 'default: 0.01',
desc: 'The granularity the slider can step through values.'
},
{
name: 'style',
type: 'object',
Expand All @@ -57,7 +63,7 @@ class SlidersPage extends React.Component {
header: 'optional',
desc: 'Callback function that is fired when the user changes the ' +
'slider\'s value.'
}
}
]
},
];
Expand All @@ -68,8 +74,8 @@ class SlidersPage extends React.Component {
code={code}
componentInfo={componentInfo}>

<Slider name="slider1"/>
<Slider onMouseDown={this.handleMouseDown} name="slider2" value={0.5} />
<Slider name="slider1" />
<Slider name="slider2" value={0.5} step={0.10} />
<Slider name="slider3" value={1}/>
<Slider name="slider1" disabled={true} />
<Slider name="slider2" disabled={true} value={0.5} />
Expand Down
42 changes: 40 additions & 2 deletions src/slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var Slider = React.createClass({
required: true,
disabled: false,
defaultValue: 0,
step: 0.01,
min: 0,
max: 1,
dragging: false
Expand Down Expand Up @@ -244,6 +245,7 @@ var Slider = React.createClass({
<Draggable axis="x" bound="point"
cancel={this.props.disabled ? '*' : null}
start={{x: (percent * 100) + '%'}}
constrain={this._constrain()}
onStart={this._onDragStart}
onStop={this._onDragStop}
onDrag={this._onDragUpdate}
Expand Down Expand Up @@ -285,14 +287,50 @@ var Slider = React.createClass({
},

setPercent: function (percent) {
var value = this._percentToValue(percent);
var value = this._alignValue(this._percentToValue(percent));
this.setState({value: value, percent: percent});
},

clearValue: function() {
this.setValue(0);
},

_alignValue: function (val) {
var { step, min } = this.props;

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

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

return parseFloat(alignValue.toFixed(5));
},

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

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

_onFocus: function (e) {
this.setState({focused: true});
if (this.props.onFocus) this.props.onFocus(e);
Expand Down Expand Up @@ -349,7 +387,7 @@ var Slider = React.createClass({
_updateWithChangeEvent: function(e, percent) {
if (this.state.percent === percent) return;
this.setPercent(percent);
var value = this._percentToValue(percent);
var value = this._alignValue(this._percentToValue(percent));
if (this.props.onChange) this.props.onChange(e, value);
},

Expand Down

0 comments on commit c34504a

Please sign in to comment.