-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
day.jsx
314 lines (273 loc) · 8.59 KB
/
day.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import React from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import {
getDay,
getMonth,
getDate,
newDate,
isSameDay,
isDayDisabled,
isDayExcluded,
isDayInRange,
isEqual,
isBefore,
isAfter,
getDayOfWeekCode,
formatDate
} from "./date_utils";
export default class Day extends React.Component {
static propTypes = {
ariaLabelPrefixWhenEnabled: PropTypes.string,
ariaLabelPrefixWhenDisabled: PropTypes.string,
disabledKeyboardNavigation: PropTypes.bool,
day: PropTypes.instanceOf(Date).isRequired,
dayClassName: PropTypes.func,
endDate: PropTypes.instanceOf(Date),
highlightDates: PropTypes.instanceOf(Map),
month: PropTypes.number,
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
preSelection: PropTypes.instanceOf(Date),
selected: PropTypes.object,
selectingDate: PropTypes.instanceOf(Date),
selectsEnd: PropTypes.bool,
selectsStart: PropTypes.bool,
selectsRange: PropTypes.bool,
startDate: PropTypes.instanceOf(Date),
renderDayContents: PropTypes.func,
handleOnKeyDown: PropTypes.func,
containerRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Element) })
])
};
componentDidMount() {
this.handleFocusDay();
}
componentDidUpdate(prevProps) {
this.handleFocusDay(prevProps);
}
dayEl = React.createRef();
handleClick = event => {
if (!this.isDisabled() && this.props.onClick) {
this.props.onClick(event);
}
};
handleMouseEnter = event => {
if (!this.isDisabled() && this.props.onMouseEnter) {
this.props.onMouseEnter(event);
}
};
handleOnKeyDown = event => {
const eventKey = event.key;
if (eventKey === " ") {
event.preventDefault();
event.key = "Enter";
}
this.props.handleOnKeyDown(event);
};
isSameDay = other => isSameDay(this.props.day, other);
isKeyboardSelected = () =>
!this.props.disabledKeyboardNavigation &&
!this.isSameDay(this.props.selected) &&
this.isSameDay(this.props.preSelection);
isDisabled = () => isDayDisabled(this.props.day, this.props);
isExcluded = () => isDayExcluded(this.props.day, this.props);
getHighLightedClass = defaultClassName => {
const { day, highlightDates } = this.props;
if (!highlightDates) {
return false;
}
// Looking for className in the Map of {'day string, 'className'}
const dayStr = formatDate(day, "MM.dd.yyyy");
return highlightDates.get(dayStr);
};
isInRange = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isDayInRange(day, startDate, endDate);
};
isInSelectingRange = () => {
const {
day,
selectsStart,
selectsEnd,
selectsRange,
selectingDate,
startDate,
endDate
} = this.props;
if (
!(selectsStart || selectsEnd || selectsRange) ||
!selectingDate ||
this.isDisabled()
) {
return false;
}
if (
selectsStart &&
endDate &&
(isBefore(selectingDate, endDate) || isEqual(selectingDate, endDate))
) {
return isDayInRange(day, selectingDate, endDate);
}
if (
selectsEnd &&
startDate &&
(isAfter(selectingDate, startDate) || isEqual(selectingDate, startDate))
) {
return isDayInRange(day, startDate, selectingDate);
}
if (
selectsRange &&
startDate &&
!endDate &&
(isAfter(selectingDate, startDate) || isEqual(selectingDate, startDate))
) {
return isDayInRange(day, startDate, selectingDate);
}
return false;
};
isSelectingRangeStart = () => {
if (!this.isInSelectingRange()) {
return false;
}
const { day, selectingDate, startDate, selectsStart } = this.props;
if (selectsStart) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, startDate);
}
};
isSelectingRangeEnd = () => {
if (!this.isInSelectingRange()) {
return false;
}
const { day, selectingDate, endDate, selectsEnd } = this.props;
if (selectsEnd) {
return isSameDay(day, selectingDate);
} else {
return isSameDay(day, endDate);
}
};
isRangeStart = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isSameDay(startDate, day);
};
isRangeEnd = () => {
const { day, startDate, endDate } = this.props;
if (!startDate || !endDate) {
return false;
}
return isSameDay(endDate, day);
};
isWeekend = () => {
const weekday = getDay(this.props.day);
return weekday === 0 || weekday === 6;
};
isOutsideMonth = () => {
return (
this.props.month !== undefined &&
this.props.month !== getMonth(this.props.day)
);
};
getClassNames = date => {
const dayClassName = this.props.dayClassName
? this.props.dayClassName(date)
: undefined;
return classnames(
"react-datepicker__day",
dayClassName,
"react-datepicker__day--" + getDayOfWeekCode(this.props.day),
{
"react-datepicker__day--disabled": this.isDisabled(),
"react-datepicker__day--excluded": this.isExcluded(),
"react-datepicker__day--selected": this.isSameDay(this.props.selected),
"react-datepicker__day--keyboard-selected": this.isKeyboardSelected(),
"react-datepicker__day--range-start": this.isRangeStart(),
"react-datepicker__day--range-end": this.isRangeEnd(),
"react-datepicker__day--in-range": this.isInRange(),
"react-datepicker__day--in-selecting-range": this.isInSelectingRange(),
"react-datepicker__day--selecting-range-start": this.isSelectingRangeStart(),
"react-datepicker__day--selecting-range-end": this.isSelectingRangeEnd(),
"react-datepicker__day--today": this.isSameDay(newDate()),
"react-datepicker__day--weekend": this.isWeekend(),
"react-datepicker__day--outside-month": this.isOutsideMonth()
},
this.getHighLightedClass("react-datepicker__day--highlighted")
);
};
getAriaLabel = () => {
const {
day,
ariaLabelPrefixWhenEnabled = "Choose",
ariaLabelPrefixWhenDisabled = "Not available"
} = this.props;
const prefix =
this.isDisabled() || this.isExcluded()
? ariaLabelPrefixWhenDisabled
: ariaLabelPrefixWhenEnabled;
return `${prefix} ${formatDate(day, "PPPP")}`;
};
getTabIndex = (selected, preSelection) => {
const selectedDay = selected || this.props.selected;
const preSelectionDay = preSelection || this.props.preSelection;
const tabIndex =
this.isKeyboardSelected() ||
(this.isSameDay(selectedDay) && isSameDay(preSelectionDay, selectedDay))
? 0
: -1;
return tabIndex;
};
// various cases when we need to apply focus to the preselected day
// focus the day on mount/update so that keyboard navigation works while cycling through months with up or down keys (not for prev and next month buttons)
// prevent focus for these activeElement cases so we don't pull focus from the input as the calendar opens
handleFocusDay = (prevProps = {}) => {
let shouldFocusDay = false;
// only do this while the input isn't focused
// otherwise, typing/backspacing the date manually may steal focus away from the input
if (
this.getTabIndex() === 0 &&
!prevProps.isInputFocused &&
this.isSameDay(this.props.preSelection)
) {
// there is currently no activeElement
if (!document.activeElement || document.activeElement === document.body) {
shouldFocusDay = true;
}
// the activeElement is in the container, and it is another instance of Day
if (
this.props.containerRef &&
this.props.containerRef.current &&
this.props.containerRef.current.contains(document.activeElement) &&
document.activeElement.classList.contains("react-datepicker__day")
) {
shouldFocusDay = true;
}
}
shouldFocusDay && this.dayEl.current.focus({ preventScroll: true });
};
render = () => (
<div
ref={this.dayEl}
className={this.getClassNames(this.props.day)}
onKeyDown={this.handleOnKeyDown}
onClick={this.handleClick}
onMouseEnter={this.handleMouseEnter}
tabIndex={this.getTabIndex()}
aria-label={this.getAriaLabel()}
role="button"
aria-disabled={this.isDisabled()}
>
{this.props.renderDayContents
? this.props.renderDayContents(getDate(this.props.day), this.props.day)
: getDate(this.props.day)}
</div>
);
}