start-end.md 1.3 KB
Newer Older
A
afc163 已提交
1
# 日期范围一
Y
yiminghe 已提交
2 3 4

- order: 7

A
afc163 已提交
5
可以设置 `disabledDate` 方法,来约束开始和结束日期。
Y
yiminghe 已提交
6 7 8 9

---

````jsx
10
import { DatePicker } from 'antd';
Y
yiminghe 已提交
11

A
afc163 已提交
12
const DateRange = React.createClass({
Y
yiminghe 已提交
13 14 15 16 17 18 19 20 21 22 23 24
  getInitialState() {
    return {
      startValue: null,
      endValue: null
    };
  },
  disabledStartDate(startValue) {
    if (!startValue || !this.state.endValue) {
      return false;
    }
    return startValue.getTime() >= this.state.endValue.getTime();
  },
A
afc163 已提交
25 26 27 28 29 30
  disabledEndDate(endValue) {
    if (!endValue || !this.state.startValue) {
      return false;
    }
    return endValue.getTime() <= this.state.startValue.getTime();
  },
Y
yiminghe 已提交
31
  onChange(field, value) {
32
    console.log(field, 'change', value);
Y
yiminghe 已提交
33 34 35 36 37
    this.setState({
      [field]: value,
    });
  },
  render() {
A
afc163 已提交
38
    return <div>
39
      <DatePicker disabledDate={this.disabledStartDate}
A
afc163 已提交
40 41
        value={this.state.startValue}
        placeholder="开始日期"
42
        onChange={this.onChange.bind(this, 'startValue')} />
43
      <DatePicker disabledDate={this.disabledEndDate}
A
afc163 已提交
44 45
        value={this.state.endValue}
        placeholder="结束日期"
46
        onChange={this.onChange.bind(this, 'endValue')} />
Y
yiminghe 已提交
47 48 49 50 51
    </div>;
  }
});

ReactDOM.render(
A
afc163 已提交
52
  <DateRange />
53
, document.getElementById('components-date-picker-demo-start-end'));
Y
yiminghe 已提交
54
````