2015-12-25 01:21:03 +08:00
|
|
|
# 日期范围一
|
2015-10-22 17:27:53 +08:00
|
|
|
|
|
|
|
- order: 7
|
|
|
|
|
2015-12-25 01:21:03 +08:00
|
|
|
可以设置 `disabledDate` 方法,来约束开始和结束日期。
|
2015-10-22 17:27:53 +08:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
````jsx
|
2015-12-10 16:12:10 +08:00
|
|
|
import { DatePicker } from 'antd';
|
2015-10-22 17:27:53 +08:00
|
|
|
|
2015-10-28 20:55:49 +08:00
|
|
|
const DateRange = React.createClass({
|
2015-10-22 17:27:53 +08:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
startValue: null,
|
|
|
|
endValue: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
disabledStartDate(startValue) {
|
|
|
|
if (!startValue || !this.state.endValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return startValue.getTime() >= this.state.endValue.getTime();
|
|
|
|
},
|
2015-10-28 18:35:56 +08:00
|
|
|
disabledEndDate(endValue) {
|
|
|
|
if (!endValue || !this.state.startValue) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return endValue.getTime() <= this.state.startValue.getTime();
|
|
|
|
},
|
2015-10-22 17:27:53 +08:00
|
|
|
onChange(field, value) {
|
2015-11-25 17:35:49 +08:00
|
|
|
console.log(field, 'change', value);
|
2015-10-22 17:27:53 +08:00
|
|
|
this.setState({
|
|
|
|
[field]: value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
render() {
|
2015-10-28 18:35:56 +08:00
|
|
|
return <div>
|
2015-12-10 16:12:10 +08:00
|
|
|
<DatePicker disabledDate={this.disabledStartDate}
|
2015-10-28 18:35:56 +08:00
|
|
|
value={this.state.startValue}
|
|
|
|
placeholder="开始日期"
|
2015-11-25 17:35:49 +08:00
|
|
|
onChange={this.onChange.bind(this, 'startValue')} />
|
2015-12-10 16:12:10 +08:00
|
|
|
<DatePicker disabledDate={this.disabledEndDate}
|
2015-10-28 18:35:56 +08:00
|
|
|
value={this.state.endValue}
|
|
|
|
placeholder="结束日期"
|
2015-11-25 17:35:49 +08:00
|
|
|
onChange={this.onChange.bind(this, 'endValue')} />
|
2015-10-22 17:27:53 +08:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(
|
2015-10-28 18:35:56 +08:00
|
|
|
<DateRange />
|
2015-12-10 16:12:10 +08:00
|
|
|
, document.getElementById('components-date-picker-demo-start-end'));
|
2015-10-22 17:27:53 +08:00
|
|
|
````
|