createPicker.jsx 3.7 KB
Newer Older
B
Benjy Cui 已提交
1 2 3 4 5 6
import React from 'react';
import MonthCalendar from 'rc-calendar/lib/MonthCalendar';
import RcDatePicker from 'rc-calendar/lib/Picker';
import GregorianCalendar from 'gregorian-calendar';
import classNames from 'classnames';

B
Benjy Cui 已提交
7
export default function createPicker(TheCalendar) {
B
Benjy Cui 已提交
8 9 10
  return React.createClass({
    getInitialState() {
      return {
B
Benjy Cui 已提交
11
        value: this.props.parseDateFromValue(this.props.value || this.props.defaultValue)
B
Benjy Cui 已提交
12 13 14 15 16
      };
    },
    componentWillReceiveProps(nextProps) {
      if ('value' in nextProps) {
        this.setState({
B
Benjy Cui 已提交
17
          value: nextProps.parseDateFromValue(nextProps.value)
B
Benjy Cui 已提交
18 19 20 21
        });
      }
    },
    handleChange(value) {
B
Benjy Cui 已提交
22 23
      const props = this.props;
      if (!('value' in props)) {
B
Benjy Cui 已提交
24 25 26
        this.setState({ value });
      }
      const timeValue = value ? new Date(value.getTime()) : null;
B
Benjy Cui 已提交
27
      props.onChange(timeValue, value ? props.getFormatter().format(value) : '');
B
Benjy Cui 已提交
28 29 30
    },
    render() {
      const props = this.props;
B
Benjy Cui 已提交
31
      const locale = props.locale;
B
Benjy Cui 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
      // 以下两行代码
      // 给没有初始值的日期选择框提供本地化信息
      // 否则会以周日开始排
      let defaultCalendarValue = new GregorianCalendar(locale);
      defaultCalendarValue.setTime(Date.now());

      const placeholder = ('placeholder' in props)
        ? props.placeholder : locale.lang.placeholder;

      const disabledTime = props.showTime ? props.disabledTime : null;

      const calendarClassName = classNames({
        ['ant-calendar-time']: props.showTime,
        ['ant-calendar-month']: MonthCalendar === TheCalendar,
      });

      let pickerChangeHandler = {
        onChange: this.handleChange,
      };

      let calendarHandler = {
        onOk: this.handleChange,
      };

      if (props.showTime) {
        pickerChangeHandler.onChange = (value) => {
          // Click clear button
          if (value === null) {
            this.handleChange(value);
          }
        };
      } else {
        calendarHandler = {};
      }

      const calendar = (
        <TheCalendar
          disabledDate={props.disabledDate}
          disabledTime={disabledTime}
          locale={locale.lang}
B
Benjy Cui 已提交
72
          timePicker={props.timePicker}
B
Benjy Cui 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
          defaultValue={defaultCalendarValue}
          dateInputPlaceholder={placeholder}
          prefixCls="ant-calendar"
          className={calendarClassName}
          {...calendarHandler} />
      );

      // default width for showTime
      const pickerStyle = {};
      if (props.showTime) {
        pickerStyle.width = 180;
      }

      return (
B
Benjy Cui 已提交
87
        <span className={props.pickerClass} style={{ ...pickerStyle, ...props.style }}>
B
Benjy Cui 已提交
88 89 90 91 92 93 94 95 96
          <RcDatePicker
            transitionName={props.transitionName}
            disabled={props.disabled}
            calendar={calendar}
            value={this.state.value}
            prefixCls="ant-calendar-picker-container"
            style={props.popupStyle}
            align={props.align}
            getCalendarContainer={props.getCalendarContainer}
B
Benjy Cui 已提交
97 98
            onOpen={props.toggleOpen}
            onClose={props.toggleOpen}
B
Benjy Cui 已提交
99 100 101 102 103 104 105 106
            {...pickerChangeHandler}
          >
            {
              ({ value }) => {
                return (
                  <span>
                    <input
                      disabled={props.disabled}
B
Benjy Cui 已提交
107 108
                      onChange={props.handleInputChange}
                      value={value && props.getFormatter().format(value)}
B
Benjy Cui 已提交
109
                      placeholder={placeholder}
B
Benjy Cui 已提交
110
                      className={props.pickerInputClass} />
B
Benjy Cui 已提交
111 112 113 114 115 116 117 118 119 120 121
                    <span className="ant-calendar-picker-icon" />
                  </span>
                );
              }
            }
          </RcDatePicker>
        </span>
      );
    }
  });
}