diff --git a/docs/src/app/components/pages/components/sliders.jsx b/docs/src/app/components/pages/components/sliders.jsx index f135d233eb047f..a81eee3117208c 100644 --- a/docs/src/app/components/pages/components/sliders.jsx +++ b/docs/src/app/components/pages/components/sliders.jsx @@ -15,7 +15,7 @@ class SlidersPage extends React.Component { '// Default\n' + '\n\n' + '// With starting value\n' + - '\n' + + '\n' + '\n\n' + '// Disabled with fixed value\n' + '\n' + @@ -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', @@ -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.' - } + } ] }, ]; @@ -68,8 +74,8 @@ class SlidersPage extends React.Component { code={code} componentInfo={componentInfo}> - - + + diff --git a/src/slider.jsx b/src/slider.jsx index e746923a836509..0f82d0a58296c1 100644 --- a/src/slider.jsx +++ b/src/slider.jsx @@ -33,6 +33,7 @@ var Slider = React.createClass({ required: true, disabled: false, defaultValue: 0, + step: 0.01, min: 0, max: 1, dragging: false @@ -244,6 +245,7 @@ var Slider = React.createClass({ = 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); @@ -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); },