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
    this.setState({
      [field]: value,
    });
  },
B
Benjy Cui 已提交
37 38 39 40 41 42
  onStartChange(value) {
    this.onChange('startValue', value);
  },
  onEndChange(value) {
    this.onChange('endValue', value);
  },
Y
yiminghe 已提交
43
  render() {
44 45 46
    return (
      <div>
        <DatePicker disabledDate={this.disabledStartDate}
47 48
          value={this.state.startValue}
          placeholder="开始日期"
B
Benjy Cui 已提交
49
          onChange={this.onStartChange} />
50
        <DatePicker disabledDate={this.disabledEndDate}
51 52
          value={this.state.endValue}
          placeholder="结束日期"
B
Benjy Cui 已提交
53
          onChange={this.onEndChange} />
54 55
      </div>
    );
Y
yiminghe 已提交
56 57 58 59
  }
});

ReactDOM.render(
A
afc163 已提交
60
  <DateRange />
61
, mountNode);
Y
yiminghe 已提交
62
````