forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
year_dropdown_options.jsx
144 lines (123 loc) · 3.61 KB
/
year_dropdown_options.jsx
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
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { getYear } from "./date_utils";
function generateYears(year, noOfYear, minDate, maxDate) {
var list = [];
for (var i = 0; i < 2 * noOfYear + 1; i++) {
const newYear = year + noOfYear - i;
let isInRange = true;
if (minDate) {
isInRange = getYear(minDate) <= newYear;
}
if (maxDate && isInRange) {
isInRange = getYear(maxDate) >= newYear;
}
if (isInRange) {
list.push(newYear);
}
}
return list;
}
export default class YearDropdownOptions extends React.Component {
static propTypes = {
minDate: PropTypes.instanceOf(Date),
maxDate: PropTypes.instanceOf(Date),
onCancel: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
scrollableYearDropdown: PropTypes.bool,
year: PropTypes.number.isRequired,
yearDropdownItemNumber: PropTypes.number
};
constructor(props) {
super(props);
const { yearDropdownItemNumber, scrollableYearDropdown } = props;
const noOfYear =
yearDropdownItemNumber || (scrollableYearDropdown ? 10 : 5);
this.state = {
yearsList: generateYears(
this.props.year,
noOfYear,
this.props.minDate,
this.props.maxDate
)
};
}
renderOptions = () => {
var selectedYear = this.props.year;
var options = this.state.yearsList.map(year => (
<div
className={
selectedYear === year
? "react-datepicker__year-option react-datepicker__year-option--selected_year"
: "react-datepicker__year-option"
}
key={year}
ref={year}
onClick={this.onChange.bind(this, year)}
>
{selectedYear === year ? (
<span className="react-datepicker__year-option--selected">✓</span>
) : (
""
)}
{year}
</div>
));
const minYear = this.props.minDate ? getYear(this.props.minDate) : null;
const maxYear = this.props.maxDate ? getYear(this.props.maxDate) : null;
if (!maxYear || !this.state.yearsList.find(year => year === maxYear)) {
options.unshift(
<div
className="react-datepicker__year-option"
ref={"upcoming"}
key={"upcoming"}
onClick={this.incrementYears}
>
<a className="react-datepicker__navigation react-datepicker__navigation--years react-datepicker__navigation--years-upcoming" />
</div>
);
}
if (!minYear || !this.state.yearsList.find(year => year === minYear)) {
options.push(
<div
className="react-datepicker__year-option"
ref={"previous"}
key={"previous"}
onClick={this.decrementYears}
>
<a className="react-datepicker__navigation react-datepicker__navigation--years react-datepicker__navigation--years-previous" />
</div>
);
}
return options;
};
onChange = year => {
this.props.onChange(year);
};
handleClickOutside = () => {
this.props.onCancel();
};
shiftYears = amount => {
var years = this.state.yearsList.map(function(year) {
return year + amount;
});
this.setState({
yearsList: years
});
};
incrementYears = () => {
return this.shiftYears(1);
};
decrementYears = () => {
return this.shiftYears(-1);
};
render() {
let dropdownClass = classNames({
"react-datepicker__year-dropdown": true,
"react-datepicker__year-dropdown--scrollable": this.props
.scrollableYearDropdown
});
return <div className={dropdownClass}>{this.renderOptions()}</div>;
}
}