forked from Hacker0x01/react-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyear_dropdown_options.tsx
187 lines (160 loc) · 4.86 KB
/
year_dropdown_options.tsx
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { clsx } from "clsx";
import React, { Component, createRef } from "react";
import { ClickOutsideWrapper } from "./click_outside_wrapper";
import { getYear } from "./date_utils";
function generateYears(
year: number,
noOfYear: number,
minDate?: Date,
maxDate?: Date,
): number[] {
const list: number[] = [];
for (let 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;
}
interface YearDropdownOptionsProps {
minDate?: Date;
maxDate?: Date;
onChange: (year: number) => void;
onCancel: VoidFunction;
scrollableYearDropdown?: boolean;
year: number;
yearDropdownItemNumber?: number;
}
interface YearDropdownOptionsState {
yearsList: number[];
}
export default class YearDropdownOptions extends Component<
YearDropdownOptionsProps,
YearDropdownOptionsState
> {
constructor(props: YearDropdownOptionsProps) {
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,
),
};
this.dropdownRef = createRef<HTMLDivElement>();
}
componentDidMount(): void {
const dropdownCurrent = this.dropdownRef.current;
if (dropdownCurrent) {
// Get array from HTMLCollection
const dropdownCurrentChildren = dropdownCurrent.children
? Array.from(dropdownCurrent.children)
: null;
const selectedYearOptionEl = dropdownCurrentChildren
? dropdownCurrentChildren.find((childEl) => childEl.ariaSelected)
: null;
dropdownCurrent.scrollTop =
selectedYearOptionEl && selectedYearOptionEl instanceof HTMLElement
? selectedYearOptionEl.offsetTop +
(selectedYearOptionEl.clientHeight - dropdownCurrent.clientHeight) /
2
: (dropdownCurrent.scrollHeight - dropdownCurrent.clientHeight) / 2;
}
}
dropdownRef: React.RefObject<HTMLDivElement | null>;
renderOptions = (): React.ReactElement[] => {
const selectedYear = this.props.year;
const 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}
onClick={this.onChange.bind(this, year)}
aria-selected={selectedYear === year ? "true" : undefined}
>
{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"
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"
key={"previous"}
onClick={this.decrementYears}
>
<a className="react-datepicker__navigation react-datepicker__navigation--years react-datepicker__navigation--years-previous" />
</div>,
);
}
return options;
};
onChange = (year: number): void => {
this.props.onChange(year);
};
handleClickOutside = (): void => {
this.props.onCancel();
};
shiftYears = (amount: number): void => {
const years = this.state.yearsList.map(function (year) {
return year + amount;
});
this.setState({
yearsList: years,
});
};
incrementYears = (): void => {
return this.shiftYears(1);
};
decrementYears = (): void => {
return this.shiftYears(-1);
};
render() {
const dropdownClass = clsx({
"react-datepicker__year-dropdown": true,
"react-datepicker__year-dropdown--scrollable":
this.props.scrollableYearDropdown,
});
return (
<ClickOutsideWrapper
className={dropdownClass}
containerRef={this.dropdownRef}
onClickOutside={this.handleClickOutside}
>
{this.renderOptions()}
</ClickOutsideWrapper>
);
}
}