Compare commits

...
5 Commits
Author SHA1 Message Date
Benjy Cui 978d877323 bump 3.2.0 2015-12-16 09:30:46 +08:00
Boris Serdiuk a1ae3a8374 Merge pull request #60 from react-component/fix-rerender
fix: crash when rerender #57
2015-12-15 13:53:31 +03:00
Benjy Cui 15f568283c fix: crash when rerender #57 2015-12-15 11:32:41 +08:00
Benjy Cui e85d325542 Merge pull request #59 from react-component/feat-allowCross
feat: add `allowCross` property #56
2015-12-15 09:06:09 +08:00
Benjy Cui 395fa6c201 feat: add allowCross property #56 2015-12-12 08:45:28 +08:00
5 changed files with 87 additions and 22 deletions
+6
View File
@@ -106,6 +106,12 @@ ReactDOM.render(<Rcslider />, container);
<td>false</td> <td>false</td>
<td>Determines the type of slider. If range is `true`, two handles will be rendered in order to select a range.</td> <td>Determines the type of slider. If range is `true`, two handles will be rendered in order to select a range.</td>
</tr> </tr>
<tr>
<td>allowCross</td>
<td>boolean</td>
<td>true</td>
<td>When `range` is `true`, `allowCross` could be set as `true` to allow those two handles cross.</td>
</tr>
<tr> <tr>
<td>defaultValue</td> <td>defaultValue</td>
<td>number or [number, number]</td> <td>number or [number, number]</td>
+23 -3
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less'); require('rc-slider/assets/index.less');
const React = require('react'); const React = require('react');
@@ -22,6 +23,21 @@ const CustomizedRange = React.createClass({
value: value, value: value,
}); });
}, },
render: function() {
return <Slider range value={this.state.value} onChange={this.onSliderChange} />;
},
});
const DynamicBounds = React.createClass({
getInitialState() {
return {
min: 0,
max: 100,
};
},
onSliderChange: function(value) {
log(value);
},
onMinChange: function(e) { onMinChange: function(e) {
this.setState({ this.setState({
min: +e.target.value || 0, min: +e.target.value || 0,
@@ -41,7 +57,7 @@ const CustomizedRange = React.createClass({
<label>Max: </label> <label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} /> <input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br /> <br /><br />
<Slider range value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} /> <Slider range defaultValue={[20, 50]} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div> </div>
); );
}, },
@@ -50,8 +66,8 @@ const CustomizedRange = React.createClass({
ReactDOM.render( ReactDOM.render(
<div> <div>
<div style={style}> <div style={style}>
<p>Basic Range</p> <p>Basic Range`allowCross=false`</p>
<Slider range defaultValue={[0, 20]} onChange={log} /> <Slider range allowCross={false} defaultValue={[0, 20]} onChange={log} />
</div> </div>
<div style={style}> <div style={style}>
<p>Basic Range`step=20` </p> <p>Basic Range`step=20` </p>
@@ -69,5 +85,9 @@ ReactDOM.render(
<p>Customized Range</p> <p>Customized Range</p>
<CustomizedRange /> <CustomizedRange />
</div> </div>
<div style={style}>
<p>Range with dynamic `max` `min`</p>
<DynamicBounds />
</div>
</div> </div>
, document.getElementById('__react-content')); , document.getElementById('__react-content'));
+21 -3
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less'); require('rc-slider/assets/index.less');
const React = require('react'); const React = require('react');
@@ -19,8 +20,6 @@ const CustomizedSlider = React.createClass({
getInitialState: function() { getInitialState: function() {
return { return {
value: 50, value: 50,
min: 0,
max: 100,
}; };
}, },
onSliderChange: function(value) { onSliderChange: function(value) {
@@ -29,6 +28,21 @@ const CustomizedSlider = React.createClass({
value: value, value: value,
}); });
}, },
render: function() {
return <Slider value={this.state.value} onChange={this.onSliderChange} />;
},
});
const DynamicBounds = React.createClass({
getInitialState: function() {
return {
min: 0,
max: 100,
};
},
onSliderChange: function(value) {
log(value);
},
onMinChange: function(e) { onMinChange: function(e) {
this.setState({ this.setState({
min: +e.target.value || 0, min: +e.target.value || 0,
@@ -48,7 +62,7 @@ const CustomizedSlider = React.createClass({
<label>Max: </label> <label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} /> <input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br /> <br /><br />
<Slider value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} /> <Slider defaultValue={50} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div> </div>
); );
}, },
@@ -80,5 +94,9 @@ ReactDOM.render(
<p>Customized Slider</p> <p>Customized Slider</p>
<CustomizedSlider /> <CustomizedSlider />
</div> </div>
<div style={style}>
<p>Slider with dynamic `min` `max`</p>
<DynamicBounds />
</div>
</div> </div>
, document.getElementById('__react-content')); , document.getElementById('__react-content'));
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "rc-slider", "name": "rc-slider",
"version": "3.1.4", "version": "3.2.0",
"description": "slider ui component for react", "description": "slider ui component for react",
"keywords": [ "keywords": [
"react", "react",
+36 -15
View File
@@ -71,7 +71,7 @@ class Slider extends React.Component {
const {lowerBound, upperBound} = this.state; const {lowerBound, upperBound} = this.state;
if (nextProps.range) { if (nextProps.range) {
const value = nextProps.value; const value = nextProps.value || [lowerBound, upperBound];
const nextUpperBound = this.trimAlignValue(value[1], nextProps); const nextUpperBound = this.trimAlignValue(value[1], nextProps);
const nextLowerBound = this.trimAlignValue(value[0], nextProps); const nextLowerBound = this.trimAlignValue(value[0], nextProps);
if (nextLowerBound === lowerBound && nextUpperBound === upperBound) return; if (nextLowerBound === lowerBound && nextUpperBound === upperBound) return;
@@ -85,7 +85,8 @@ class Slider extends React.Component {
this.props.onChange([nextLowerBound, nextUpperBound]); this.props.onChange([nextLowerBound, nextUpperBound]);
} }
} else { } else {
const nextValue = this.trimAlignValue(nextProps.value, nextProps); const value = 'value' in nextProps ? nextProps.value : upperBound;
const nextValue = this.trimAlignValue(value, nextProps);
if (nextValue === upperBound && lowerBound === nextProps.min) return; if (nextValue === upperBound && lowerBound === nextProps.min) return;
this.setState({ this.setState({
@@ -98,19 +99,16 @@ class Slider extends React.Component {
} }
} }
onChange(handle, value) { onChange(state) {
const props = this.props; const props = this.props;
const isNotControlled = !('value' in props); const isNotControlled = !('value' in props);
if (isNotControlled) { if (isNotControlled) {
this.setState({[handle]: value}); this.setState(state);
} else if (state.handle) {
this.setState({handle: state.handle});
} }
const state = this.state; const data = objectAssign({}, this.state, state);
const data = {
upperBound: state.upperBound,
lowerBound: state.lowerBound,
};
data[handle] = value;
const changedValue = props.range ? [data.lowerBound, data.upperBound] : data.upperBound; const changedValue = props.range ? [data.lowerBound, data.upperBound] : data.upperBound;
props.onChange(changedValue); props.onChange(changedValue);
} }
@@ -142,7 +140,26 @@ class Slider extends React.Component {
const oldValue = state[state.handle]; const oldValue = state[state.handle];
if (value === oldValue) return; if (value === oldValue) return;
this.onChange(state.handle, value); if (props.allowCross && value < state.lowerBound && state.handle === 'upperBound') {
this.onChange({
handle: 'lowerBound',
lowerBound: value,
upperBound: this.state.lowerBound,
});
return;
}
if (props.allowCross && value > state.upperBound && state.handle === 'lowerBound') {
this.onChange({
handle: 'upperBound',
upperBound: value,
lowerBound: this.state.upperBound,
});
return;
}
this.onChange({
[state.handle]: value,
});
} }
onTouchStart(e) { onTouchStart(e) {
@@ -197,7 +214,9 @@ class Slider extends React.Component {
const oldValue = state[valueNeedChanging]; const oldValue = state[valueNeedChanging];
if (value === oldValue) return; if (value === oldValue) return;
this.onChange(valueNeedChanging, value); this.onChange({
[valueNeedChanging]: value,
});
} }
getValue() { getValue() {
@@ -238,7 +257,7 @@ class Slider extends React.Component {
trimAlignValue(v, nextProps) { trimAlignValue(v, nextProps) {
const state = this.state || {}; const state = this.state || {};
const {handle, lowerBound, upperBound} = state; const {handle, lowerBound, upperBound} = state;
const {marks, step, min, max} = objectAssign({}, this.props, nextProps || {}); const {marks, step, min, max, allowCross} = objectAssign({}, this.props, nextProps || {});
let val = v; let val = v;
if (val <= min) { if (val <= min) {
@@ -247,10 +266,10 @@ class Slider extends React.Component {
if (val >= max) { if (val >= max) {
val = max; val = max;
} }
if (handle === 'upperBound' && val <= lowerBound) { if (!allowCross && handle === 'upperBound' && val <= lowerBound) {
val = lowerBound; val = lowerBound;
} }
if (handle === 'lowerBound' && val >= upperBound) { if (!allowCross && handle === 'lowerBound' && val >= upperBound) {
val = upperBound; val = upperBound;
} }
@@ -384,6 +403,7 @@ Slider.propTypes = {
tipFormatter: React.PropTypes.func, tipFormatter: React.PropTypes.func,
dots: React.PropTypes.bool, dots: React.PropTypes.bool,
range: React.PropTypes.bool, range: React.PropTypes.bool,
allowCross: React.PropTypes.bool,
}; };
Slider.defaultProps = { Slider.defaultProps = {
@@ -401,6 +421,7 @@ Slider.defaultProps = {
disabled: false, disabled: false,
dots: false, dots: false,
range: false, range: false,
allowCross: true,
}; };
export default Slider; export default Slider;