Merge pull request #59 from react-component/feat-allowCross

feat: add `allowCross` property #56
This commit is contained in:
Benjy Cui
2015-12-15 09:06:09 +08:00
3 changed files with 41 additions and 15 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>
+2 -2
View File
@@ -50,8 +50,8 @@ const CustomizedRange = React.createClass({
ReactDOM.render(
<div>
<div style={style}>
<p>Basic Range</p>
<Slider range defaultValue={[0, 20]} onChange={log} />
<p>Basic Range`allowCross`</p>
<Slider range allowCross defaultValue={[0, 20]} onChange={log} />
</div>
<div style={style}>
<p>Basic Range`step=20` </p>
+33 -13
View File
@@ -98,19 +98,16 @@ class Slider extends React.Component {
}
}
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);
}
@@ -142,7 +139,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) {
@@ -197,7 +213,9 @@ class Slider extends React.Component {
const oldValue = state[valueNeedChanging];
if (value === oldValue) return;
this.onChange(valueNeedChanging, value);
this.onChange({
[valueNeedChanging]: value,
});
}
getValue() {
@@ -238,7 +256,7 @@ class Slider extends React.Component {
trimAlignValue(v, nextProps) {
const state = this.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;
if (val <= min) {
@@ -247,10 +265,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;
}
@@ -384,6 +402,7 @@ Slider.propTypes = {
tipFormatter: React.PropTypes.func,
dots: React.PropTypes.bool,
range: React.PropTypes.bool,
allowCross: React.PropTypes.bool,
};
Slider.defaultProps = {
@@ -401,6 +420,7 @@ Slider.defaultProps = {
disabled: false,
dots: false,
range: false,
allowCross: true,
};
export default Slider;