File size: 4,200 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// @flow

import * as React from "react";
import FormSelect from "./FormSelect.react";
import FormInputGroup from "./FormInputGroup.react";

type Props = {|
  +children?: React.Node,
  +className?: string,
  +defaultDate: Date,
  +minYear: number,
  +maxYear: number,
  +format: string,
  +monthLabels: Array<string>,
  +onChange?: Date => void,
|};

type State = {|
  currentDate: Date,
|};

type DateComponents = {
  yyyy: React.Node,
  dd: React.Node,
  mm: React.Node,
};

type ChangeTypes = "mm" | "yyyy" | "dd";

class FormDatePicker extends React.PureComponent<Props, State> {
  static defaultProps = {
    monthLabels: [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December",
    ],
    minYear: 1897,
    maxYear: new Date().getFullYear(),
    format: "mm/dd/yyyy",
    defaultDate: new Date(),
  };

  state = {
    currentDate: this.props.defaultDate,
  };

  // Handle date changes
  _handleOnChange = (type: ChangeTypes, value: number): void => {
    const { currentDate } = this.state;
    const { onChange } = this.props;
    const newDate: Date = new Date(currentDate);

    // Change month
    if (type === "mm") {
      newDate.setMonth(value);
    }

    // Change day
    if (type === "dd") {
      newDate.setDate(value);
    }

    if (type === "yyyy") {
      newDate.setFullYear(value);
    }

    this.setState({ currentDate: newDate }, () => {
      onChange && onChange(this.state.currentDate);
    });
  };

  // Creates an array with numeric values from start to end
  _range = (start: number, end: number): Array<number> =>
    Array.from({ length: end + 1 - start }, (v, k) => k + start);

  // Renders the months select
  _renderMonths = (): React.Node => {
    const { currentDate } = this.state;
    const { monthLabels } = this.props;

    const onChangeMonths = (e: SyntheticInputEvent<EventTarget>): void =>
      this._handleOnChange("mm", Number(e.target.value));

    return (
      <FormSelect onChange={onChangeMonths}>
        {monthLabels.map((name, index) => (
          <option value={index} selected={currentDate.getUTCMonth() === index}>
            {name}
          </option>
        ))}
      </FormSelect>
    );
  };

  // Renders the days select
  _renderDays = (): React.Node => {
    const { currentDate } = this.state;
    const currentMonthDays = new Date(
      currentDate.getUTCFullYear(),
      currentDate.getUTCMonth() + 1,
      0
    ).getDate();
    const daysRange = this._range(1, currentMonthDays);
    const currentDay = currentDate.getUTCDate();

    const onChangeDays = (e: SyntheticInputEvent<EventTarget>) =>
      this._handleOnChange("dd", Number(e.target.value));

    return (
      <FormSelect onChange={onChangeDays}>
        {daysRange.map(day => (
          <option value={day} selected={currentDay === day}>
            {day}
          </option>
        ))}
      </FormSelect>
    );
  };

  // renderes the years select
  _renderYears = (): React.Node => {
    const { minYear, maxYear } = this.props;
    const { currentDate } = this.state;
    const yearsRange = this._range(minYear, maxYear).reverse();
    const currentYear = currentDate.getUTCFullYear();

    const onChangeYears = (e: SyntheticInputEvent<EventTarget>) =>
      this._handleOnChange("yyyy", Number(e.target.value));

    return (
      <FormSelect onChange={onChangeYears}>
        {yearsRange.map(year => (
          <option value={year} selected={currentYear === year}>
            {year}
          </option>
        ))}
      </FormSelect>
    );
  };

  render(): React.Node {
    const { format, className } = this.props;
    const formatSplit = format.split("/");
    const dateComponents: DateComponents = {
      mm: this._renderMonths(),
      dd: this._renderDays(),
      yyyy: this._renderYears(),
    };

    return (
      <div className={className}>
        <FormInputGroup>
          {formatSplit.map((type: string): React.Node => dateComponents[type])}
        </FormInputGroup>
      </div>
    );
  }
}

FormDatePicker.displayName = "Form.DatePicker";

export default FormDatePicker;