ant-design/components/datepicker/demo/start-end.md

54 lines
1.2 KiB
Markdown
Raw Normal View History

2015-10-22 17:27:53 +08:00
# 开始结束时间约束
- order: 7
设置 `disabledDate` 方法,来约束开始和结束时间的选择。
---
````jsx
var Datepicker = antd.Datepicker;
2015-10-28 18:35:56 +08:00
var 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) {
this.setState({
[field]: value,
});
},
render() {
2015-10-28 18:35:56 +08:00
return <div>
<Datepicker disabledDate={this.disabledStartDate}
value={this.state.startValue}
placeholder="开始日期"
onChange={this.onChange.bind(this,'startValue')} />
<Datepicker disabledDate={this.disabledEndDate}
value={this.state.endValue}
placeholder="结束日期"
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-10-23 11:52:05 +08:00
, document.getElementById('components-datepicker-demo-start-end'));
2015-10-22 17:27:53 +08:00
````