mirror of
https://github.com/wassname/slider.git
synced 2026-09-09 11:35:22 +08:00
Merge pull request #55 from react-component/fix-align
fix: value should be aligned if `min` `max` changed
This commit is contained in:
+22
-2
@@ -16,14 +16,34 @@ const CustomizedRange = React.createClass({
|
||||
value: [20, 40],
|
||||
};
|
||||
},
|
||||
onChange: function(value) {
|
||||
onSliderChange: function(value) {
|
||||
log(value);
|
||||
this.setState({
|
||||
value: value,
|
||||
});
|
||||
},
|
||||
onMinChange: function(e) {
|
||||
this.setState({
|
||||
min: +e.target.value || 0,
|
||||
});
|
||||
},
|
||||
onMaxChange: function(e) {
|
||||
this.setState({
|
||||
max: +e.target.value || 100,
|
||||
});
|
||||
},
|
||||
render: function() {
|
||||
return <Slider range value={this.state.value} onChange={this.onChange} />;
|
||||
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 value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
+24
-2
@@ -19,16 +19,38 @@ const CustomizedSlider = React.createClass({
|
||||
getInitialState: function() {
|
||||
return {
|
||||
value: 50,
|
||||
min: 0,
|
||||
max: 100,
|
||||
};
|
||||
},
|
||||
onChange: function(value) {
|
||||
onSliderChange: function(value) {
|
||||
log(value);
|
||||
this.setState({
|
||||
value: value,
|
||||
});
|
||||
},
|
||||
onMinChange: function(e) {
|
||||
this.setState({
|
||||
min: +e.target.value || 0,
|
||||
});
|
||||
},
|
||||
onMaxChange: function(e) {
|
||||
this.setState({
|
||||
max: +e.target.value || 100,
|
||||
});
|
||||
},
|
||||
render: function() {
|
||||
return <Slider value={this.state.value} onChange={this.onChange} />;
|
||||
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 value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
|
||||
</div>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"classnames": "^2.2.1",
|
||||
"object-assign": "^4.0.1",
|
||||
"rc-tooltip": "3.x",
|
||||
"rc-util": "3.x"
|
||||
}
|
||||
|
||||
+31
-10
@@ -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,18 +67,34 @@ 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 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 nextValue = this.trimAlignValue(nextProps.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,10 +231,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} = objectAssign({}, this.props, nextProps || {});
|
||||
|
||||
let val = v;
|
||||
if (val <= min) {
|
||||
|
||||
+1
-1
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user