diff --git a/examples/marks.js b/examples/marks.js index 7268846..a487bc0 100644 --- a/examples/marks.js +++ b/examples/marks.js @@ -8,13 +8,15 @@ var Slider = require('rc-slider'); var style = {width: 400, margin: 50}; var marks = ['A','B','C','D', 'E', 'F']; -var log = console.log.bind(console); +var log = function(value) { + console.log(value); +}; ReactDOM.render(

Slider with marks, `included=true`

- +

Slider with marks and steps, `included=true`

@@ -36,7 +38,7 @@ ReactDOM.render(

Range with marks and steps

- +
, document.getElementById('__react-content')); diff --git a/examples/range.html b/examples/range.html new file mode 100644 index 0000000..48cdce8 --- /dev/null +++ b/examples/range.html @@ -0,0 +1 @@ +placeholder diff --git a/examples/range.js b/examples/range.js new file mode 100644 index 0000000..6d7c4a5 --- /dev/null +++ b/examples/range.js @@ -0,0 +1,54 @@ +'use strict'; + +require('rc-slider/assets/index.less'); + +var React = require('react'); +var ReactDOM = require('react-dom'); +var Slider = require('rc-slider'); + +var style = {width: 400, margin: 50}; +var log = function(value) { + console.log(value); +}; + +var CustomizedRange = React.createClass({ + getInitialState: function() { + return { + value: [20, 40] + } + }, + onChange: function(value) { + log(value); + this.setState({ + value: value + }); + }, + render: function() { + return ; + } +}); + +ReactDOM.render( +
+
+

Basic Range

+ +
+
+

Basic Range,`step=20`

+ +
+
+

Basic Range,`step=20, dots`

+ +
+
+

Controlled Range

+ +
+
+

Customized Range

+ +
+
+ , document.getElementById('__react-content')); diff --git a/examples/simple.html b/examples/slider.html similarity index 100% rename from examples/simple.html rename to examples/slider.html diff --git a/examples/simple.js b/examples/slider.js similarity index 58% rename from examples/simple.js rename to examples/slider.js index bd75880..8273936 100644 --- a/examples/simple.js +++ b/examples/slider.js @@ -7,13 +7,32 @@ var ReactDOM = require('react-dom'); var Slider = require('rc-slider'); var style = {width: 400, margin: 50}; -var log = console.log.bind(console); +var log = function(value) { + console.log(value); +}; function percentFormatter(v) { return v + ' %'; } +var CustomizedSlider = React.createClass({ + getInitialState: function() { + return { + value: 50 + } + }, + onChange: function(value) { + log(value); + this.setState({ + value: value + }); + }, + render: function() { + return ; + } +}); + ReactDOM.render(
@@ -22,27 +41,23 @@ ReactDOM.render(

Basic Slider,`step=20`

- +

Basic Slider,`step=20, dots`

- +

Basic Slider with `tipFormatter`

-

Basic Range

- +

Controlled Slider

+
-

Basic Range,`step=20`

- -
-
-

Basic Range,`step=20, dots`

- +

Customized Slider

+
, document.getElementById('__react-content')); diff --git a/package.json b/package.json index ba72c90..6ea1296 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-slider", - "version": "2.3.2", + "version": "2.4.0", "description": "slider ui component for react", "keywords": [ "react", diff --git a/src/Slider.jsx b/src/Slider.jsx index 56f5e24..4dde98a 100644 --- a/src/Slider.jsx +++ b/src/Slider.jsx @@ -120,10 +120,13 @@ class Slider extends React.Component { const oldValue = state[state.handle]; if (value === oldValue) return; + // If it is not controlled component if (!('value' in props) && !('index' in props)) { this.setState({[state.handle]: value}, () => { this.triggerEvents('onChange', this.getValue()); }); + } else { + this.triggerEvents('onChange', this.getChangedValue(state.handle, value)); } } @@ -144,7 +147,7 @@ class Slider extends React.Component { } onStart(position) { - this.triggerEvents('onBeforeChange'); + this.triggerEvents('onBeforeChange', this.getValue()); const value = this.calcValueByPos(position); this.startValue = value; @@ -173,10 +176,19 @@ class Slider extends React.Component { this.setState({ handle: valueNeedChanging, recent: valueNeedChanging, - [valueNeedChanging]: value, - }, () => { - this.triggerEvents('onChange', this.getValue()); }); + + const props = this.props; + // If it is not controlled component + if (!('value' in props) && !('index' in props)) { + this.setState({ + [valueNeedChanging]: value, + }, () => { + this.triggerEvents('onChange', this.getValue()); + }); + } else { + this.triggerEvents('onChange', this.getChangedValue(valueNeedChanging, value)); + } } getValue() { @@ -184,6 +196,16 @@ class Slider extends React.Component { return this.props.range ? [lowerBound, upperBound] : upperBound; } + getChangedValue(valueNeedChanging, value) { + const state = this.state; + const data = { + upperBound: state.upperBound, + lowerBound: state.lowerBound, + }; + data[valueNeedChanging] = value; + return this.props.range ? [data.lowerBound, data.upperBound] : data.upperBound; + } + getIndex(value) { const {marks, min, max, step} = this.props; diff --git a/tests/index.spec.js b/tests/index.spec.js index 826efbc..07c55ec 100644 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -8,10 +8,6 @@ var Simulate = TestUtils.Simulate; var $ = require('jquery'); require('../assets/index.less'); -if (typeof initMochaPhantomJS === 'function') { - initMochaPhantomJS() -} - describe('rc-slider', function () { this.timeout(5000); var div = document.createElement('div');