ant-design/components/datepicker/demo/start-end.md
2015-11-03 13:52:41 +08:00

55 lines
1.3 KiB
Markdown

# 开始结束时间约束
- order: 7
设置 `disabledDate` 方法,来约束开始和结束时间的选择。
---
````jsx
import { Datepicker } from 'antd';
const DateRange = React.createClass({
getInitialState() {
return {
startValue: null,
endValue: null
};
},
disabledStartDate(startValue) {
if (!startValue || !this.state.endValue) {
return false;
}
return startValue.getTime() >= this.state.endValue.getTime();
},
disabledEndDate(endValue) {
if (!endValue || !this.state.startValue) {
return false;
}
return endValue.getTime() <= this.state.startValue.getTime();
},
onChange(field, value) {
console.log(field,'change',value);
this.setState({
[field]: value,
});
},
render() {
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')} />
</div>;
}
});
ReactDOM.render(
<DateRange />
, document.getElementById('components-datepicker-demo-start-end'));
````