diff --git a/examples/marks.js b/examples/marks.js
index 7268846..22e408c 100644
--- a/examples/marks.js
+++ b/examples/marks.js
@@ -14,7 +14,7 @@ ReactDOM.render(
Slider with marks, `included=true`
-
+
Slider with marks and steps, `included=true`
@@ -36,7 +36,7 @@ ReactDOM.render(
Range with marks and steps
-
+
, document.getElementById('__react-content'));
diff --git a/examples/range.js b/examples/range.js
index 0fabeeb..42d5545 100644
--- a/examples/range.js
+++ b/examples/range.js
@@ -9,6 +9,23 @@ var Slider = require('rc-slider');
var style = {width: 400, margin: 50};
var log = console.log.bind(console);
+var CustomizedRange = React.createClass({
+ getInitialState: function() {
+ return {
+ value: [20, 40]
+ }
+ },
+ onChange: function(value) {
+ log(value);
+ this.setState({
+ value: value
+ });
+ },
+ render: function() {
+ return ;
+ }
+});
+
ReactDOM.render(
@@ -24,8 +41,12 @@ ReactDOM.render(
-
Controlled Rangeļ¼`step=20, dots`
+
Controlled Range
+
, document.getElementById('__react-content'));
diff --git a/examples/slider.js b/examples/slider.js
index 1fdd974..beb7656 100644
--- a/examples/slider.js
+++ b/examples/slider.js
@@ -14,6 +14,23 @@ 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(
@@ -36,5 +53,9 @@ ReactDOM.render(
Controlled Slider
+
, document.getElementById('__react-content'));
diff --git a/src/Slider.jsx b/src/Slider.jsx
index 4a35fd3..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));
}
}
@@ -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;