我正在尝试使用React JS创建一个具有多个值的输入范围。
我有类似的东西:
class price extends React.Component {
constructor(props){
super(props);
this.state = {
minValue: 0,
maxValue: 20000,
step: 1000,
firstValue: null,
secondValue: null
};
this.handleChange = this.handleChange.bind(this);
}
componentWillMount(){
this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue});
}
handleChange(name, event){
//We set the state value depending on input that is clicked
if(name === "second"){
let newValue = parseInt(this.state.firstValue) + parseInt(this.state.step);
//The second value can't be lower than the first value
if(parseInt(this.state.secondValue) > parseInt(newValue)){
this.setState({secondValue: event.target.value});
}
}else{
//The first value can't be greater than the second value
if(parseInt(this.state.firstValue) < parseInt(this.state.secondValue)){
this.setState({firstValue: event.target.value});
}
}
}
render(){
const language = this.props.language;
return (
<div>
<div className="priceTitle">{language.price}</div>
<div className="rangeValues">Range : {this.state.firstValue} - {this.state.secondValue}</div>
<section className="range-slider">
<input type="range" value={this.state.firstValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "first")}/>
<input type="range" value={this.state.secondValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "second")}/>
<div className="minValue">{this.state.minValue}</div>
<div className="maxValue">{this.state.maxValue}</div>
</section>
</div>
);
}
}
我想禁用第二个值低于第一个值,第一个值大于第二个值。我尝试在handleChange函数中执行此操作但没有成功。
在我的示例中,第二个值不能低,但如果第一个值是例如12000而第二个值是13000,则状态不能再更新。我不能再改变价格了。
有什么建议吗?
答案 0 :(得分:3)
您必须将州值与目标值进行比较。
您的方法应如下所示
handleChange(name, event){
let value = event.target.value;
if(name === "second"){
if(parseInt(this.state.firstValue) < parseInt(value)){
this.setState({secondValue:value});
}
}
else{
if(parseInt(value) < parseInt(this.state.secondValue)){
this.setState({firstValue: value});
}
}
}
如果您想让它们相等,只需使用<=
代替<
。
class Price extends React.Component {
constructor(props){
super(props);
this.state = {
minValue: 0,
maxValue: 20000,
step: 1000,
firstValue: null,
secondValue: null
};
this.handleChange = this.handleChange.bind(this);
}
componentWillMount(){
this.setState({firstValue: this.state.minValue, secondValue: this.state.maxValue});
}
handleChange(name, event){
let value = event.target.value;
if(name === "second"){
if(parseInt(this.state.firstValue) < parseInt(value)){
this.setState({secondValue:value});
}
}
else{
if(parseInt(value) < parseInt(this.state.secondValue)){
this.setState({firstValue: value});
}
}
}
render(){
const language = this.props.language;
return (
<div>
<div className="priceTitle">15</div>
<div className="rangeValues">Range : {this.state.firstValue} - {this.state.secondValue}</div>
<section className="range-slider">
<input type="range" value={this.state.firstValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "first")}/>
<input type="range" value={this.state.secondValue} min={this.state.minValue} max={this.state.maxValue} step={this.state.step} onChange={this.handleChange.bind(this, "second")}/>
<div className="minValue">{this.state.minValue}</div>
<div className="maxValue">{this.state.maxValue}</div>
</section>
</div>
);
}
}
ReactDOM.render(
<Price language="World" />,
document.getElementById('container')
);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container">
</div>
&#13;