Compare commits

...
8 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
Benjy Cui 0329df0c5a bump 3.1.4 2015-12-07 14:40:57 +08:00
Boris Serdiuk 7fa8dfdd03 Merge pull request #55 from react-component/fix-align
fix: value should be aligned if `min` `max` changed
2015-12-07 09:23:51 +03:00
Benjy Cui 555f5a0660 fix: value should be aligned if min max changed 2015-12-06 09:21:17 +08:00
6 changed files with 160 additions and 31 deletions
+6
View File
@@ -106,6 +106,12 @@ ReactDOM.render(<Rcslider />, container);
<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>
</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>
<td>defaultValue</td>
<td>number or [number, number]</td>
+44 -4
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less');
const React = require('react');
@@ -16,22 +17,57 @@ const CustomizedRange = React.createClass({
value: [20, 40],
};
},
onChange: function(value) {
onSliderChange: function(value) {
log(value);
this.setState({
value: value,
});
},
render: function() {
return <Slider range value={this.state.value} onChange={this.onChange} />;
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) {
this.setState({
min: +e.target.value || 0,
});
},
onMaxChange: function(e) {
this.setState({
max: +e.target.value || 100,
});
},
render: function() {
return (
<div>
<label>Min: </label>
<input type="number" value={this.state.min} onChange={this.onMinChange} />
<br />
<label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br />
<Slider range defaultValue={[20, 50]} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div>
);
},
});
ReactDOM.render(
<div>
<div style={style}>
<p>Basic Range</p>
<Slider range defaultValue={[0, 20]} onChange={log} />
<p>Basic Range`allowCross=false`</p>
<Slider range allowCross={false} defaultValue={[0, 20]} onChange={log} />
</div>
<div style={style}>
<p>Basic Range`step=20` </p>
@@ -49,5 +85,9 @@ ReactDOM.render(
<p>Customized Range</p>
<CustomizedRange />
</div>
<div style={style}>
<p>Range with dynamic `max` `min`</p>
<DynamicBounds />
</div>
</div>
, document.getElementById('__react-content'));
+42 -2
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less');
const React = require('react');
@@ -21,14 +22,49 @@ const CustomizedSlider = React.createClass({
value: 50,
};
},
onChange: function(value) {
onSliderChange: function(value) {
log(value);
this.setState({
value: value,
});
},
render: function() {
return <Slider value={this.state.value} onChange={this.onChange} />;
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) {
this.setState({
min: +e.target.value || 0,
});
},
onMaxChange: function(e) {
this.setState({
max: +e.target.value || 100,
});
},
render: function() {
return (
<div>
<label>Min: </label>
<input type="number" value={this.state.min} onChange={this.onMinChange} />
<br />
<label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br />
<Slider defaultValue={50} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div>
);
},
});
@@ -58,5 +94,9 @@ ReactDOM.render(
<p>Customized Slider</p>
<CustomizedSlider />
</div>
<div style={style}>
<p>Slider with dynamic `min` `max`</p>
<DynamicBounds />
</div>
</div>
, document.getElementById('__react-content'));
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "3.1.3",
"version": "3.2.0",
"description": "slider ui component for react",
"keywords": [
"react",
@@ -51,6 +51,7 @@
],
"dependencies": {
"classnames": "^2.2.1",
"object-assign": "^4.0.1",
"rc-tooltip": "3.x",
"rc-util": "3.x"
}
+65 -23
View File
@@ -1,6 +1,7 @@
import React from 'react';
import {Dom as DomUtils} from 'rc-util';
import classNames from 'classnames';
import objectAssign from 'object-assign';
import Track from './Track';
import Handle from './Handle';
import Dots from './Dots';
@@ -66,34 +67,48 @@ class Slider extends React.Component {
}
componentWillReceiveProps(nextProps) {
if (!('value' in nextProps || 'min' in nextProps || 'max' in nextProps)) return;
const {lowerBound, upperBound} = this.state;
if (nextProps.range) {
const value = nextProps.value;
if (value) {
this.setState({
upperBound: value[1],
lowerBound: value[0],
});
}
} else if ('value' in nextProps) {
const value = nextProps.value || [lowerBound, upperBound];
const nextUpperBound = this.trimAlignValue(value[1], nextProps);
const nextLowerBound = this.trimAlignValue(value[0], nextProps);
if (nextLowerBound === lowerBound && nextUpperBound === upperBound) return;
this.setState({
upperBound: nextProps.value,
upperBound: nextUpperBound,
lowerBound: nextLowerBound,
});
if (this.isValueOutOfBounds(upperBound, nextProps) ||
this.isValueOutOfBounds(lowerBound, nextProps)) {
this.props.onChange([nextLowerBound, nextUpperBound]);
}
} else {
const value = 'value' in nextProps ? nextProps.value : upperBound;
const nextValue = this.trimAlignValue(value, nextProps);
if (nextValue === upperBound && lowerBound === nextProps.min) return;
this.setState({
upperBound: nextValue,
lowerBound: nextProps.min,
});
if (this.isValueOutOfBounds(upperBound, nextProps)) {
this.props.onChange(nextValue);
}
}
}
onChange(handle, value) {
onChange(state) {
const props = this.props;
const isNotControlled = !('value' in props);
if (isNotControlled) {
this.setState({[handle]: value});
this.setState(state);
} else if (state.handle) {
this.setState({handle: state.handle});
}
const state = this.state;
const data = {
upperBound: state.upperBound,
lowerBound: state.lowerBound,
};
data[handle] = value;
const data = objectAssign({}, this.state, state);
const changedValue = props.range ? [data.lowerBound, data.upperBound] : data.upperBound;
props.onChange(changedValue);
}
@@ -125,7 +140,26 @@ class Slider extends React.Component {
const oldValue = state[state.handle];
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) {
@@ -180,7 +214,9 @@ class Slider extends React.Component {
const oldValue = state[valueNeedChanging];
if (value === oldValue) return;
this.onChange(valueNeedChanging, value);
this.onChange({
[valueNeedChanging]: value,
});
}
getValue() {
@@ -214,10 +250,14 @@ class Slider extends React.Component {
return precision;
}
trimAlignValue(v) {
isValueOutOfBounds(value, props) {
return value < props.min || value > props.max;
}
trimAlignValue(v, nextProps) {
const state = this.state || {};
const {handle, lowerBound, upperBound} = state;
const {marks, step, min, max} = this.props;
const {marks, step, min, max, allowCross} = objectAssign({}, this.props, nextProps || {});
let val = v;
if (val <= min) {
@@ -226,10 +266,10 @@ class Slider extends React.Component {
if (val >= max) {
val = max;
}
if (handle === 'upperBound' && val <= lowerBound) {
if (!allowCross && handle === 'upperBound' && val <= lowerBound) {
val = lowerBound;
}
if (handle === 'lowerBound' && val >= upperBound) {
if (!allowCross && handle === 'lowerBound' && val >= upperBound) {
val = upperBound;
}
@@ -363,6 +403,7 @@ Slider.propTypes = {
tipFormatter: React.PropTypes.func,
dots: React.PropTypes.bool,
range: React.PropTypes.bool,
allowCross: React.PropTypes.bool,
};
Slider.defaultProps = {
@@ -380,6 +421,7 @@ Slider.defaultProps = {
disabled: false,
dots: false,
range: false,
allowCross: true,
};
export default Slider;
+1 -1
View File
@@ -106,7 +106,7 @@ describe('rc-slider', function() {
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-mark-text')[1].innerHTML).to.be('30');
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-mark-text')[2].innerHTML).to.be('100');
const range = ReactDOM.render(<Slider range value={30} marks={marks} />, div);
const range = ReactDOM.render(<Slider range value={[0, 30]} marks={marks} />, div);
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(range, 'rc-slider-mark-text').length).to.be(3);
});