ant-design/components/date-picker/demo/start-end.md
Benjy Cui 4026221d45 deps: hello moment (#2887)
* deps: upgrade TimePicker

* deps: upgrade DatePicker

* deps: upgrade Calendar

* fix: moment should work with LocaleProvider

* feat: update API of TimePicker

* feat: update Calendar's APIs

* feat: update DatePicker's APIs

* doc: update demo

* revert: add dateString and timeString and so on

* feat: add Calendar[defaultValue]

* feat: add defaultPickerValue

* docs: update docs about date picker

* feat: set moment locale to zh-cn automatically
2016-09-09 13:55:21 +08:00

1.9 KiB

order title
7
zh-CN en-US
日期范围一 Date range, case 1

zh-CN

可以设置 disabledDate 方法,来约束开始和结束日期。

en-US

You can use the disabledDate property to limit the start and end dates.

import { DatePicker } from 'antd';

const DateRange = React.createClass({
  getInitialState() {
    return {
      startValue: null,
      endValue: null,
      endOpen: false,
    };
  },
  disabledStartDate(startValue) {
    if (!startValue || !this.state.endValue) {
      return false;
    }
    return startValue.valueOf() > this.state.endValue.valueOf();
  },
  disabledEndDate(endValue) {
    if (!endValue || !this.state.startValue) {
      return false;
    }
    return endValue.valueOf() <= this.state.startValue.valueOf();
  },
  onChange(field, value) {
    this.setState({
      [field]: value,
    });
  },
  onStartChange(value) {
    this.onChange('startValue', value);
  },
  onEndChange(value) {
    this.onChange('endValue', value);
  },
  handleStartToggle({ open }) {
    if (!open) {
      this.setState({ endOpen: true });
    }
  },
  handleEndToggle({ open }) {
    this.setState({ endOpen: open });
  },
  render() {
    return (
      <div>
        <DatePicker
          disabledDate={this.disabledStartDate}
          showTime
          format="YYYY-MM-DD HH:mm:ss"
          value={this.state.startValue}
          placeholder="开始日期"
          onChange={this.onStartChange}
          toggleOpen={this.handleStartToggle}
        />
        <DatePicker
          disabledDate={this.disabledEndDate}
          showTime
          format="YYYY-MM-DD HH:mm:ss"
          value={this.state.endValue}
          placeholder="结束日期"
          onChange={this.onEndChange}
          open={this.state.endOpen}
          toggleOpen={this.handleEndToggle}
        />
      </div>
    );
  },
});

ReactDOM.render(
  <DateRange />
, mountNode);