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

[Slider] Take and use step props #860

Merged
merged 1 commit into from
Jun 16, 2015
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
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