UNPKG

30.5 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _index = require("../form-control/index.js");
11
12var _index2 = require("../locale/index.js");
13
14var _index3 = require("../select/index.js");
15
16var _calendarHeader = _interopRequireDefault(require("./calendar-header.js"));
17
18var _month = _interopRequireDefault(require("./month.js"));
19
20var _timepicker = _interopRequireDefault(require("../timepicker/timepicker.js"));
21
22var _styledComponents = require("./styled-components.js");
23
24var _dateFnsAdapter = _interopRequireDefault(require("./utils/date-fns-adapter.js"));
25
26var _dateHelpers = _interopRequireDefault(require("./utils/date-helpers.js"));
27
28var _overrides = require("../helpers/overrides.js");
29
30var _constants = require("./constants.js");
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
35
36function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
37
38function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
39
40function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
41
42function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
43
44function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
45
46function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
47
48function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
49
50function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
51
52function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
53
54function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
55
56function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
57
58function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
59
60function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
61
62function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
63
64function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
65
66function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
67
68function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
69
70function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
71
72function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
73
74function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
75
76function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
77
78function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
79
80var Calendar =
81/*#__PURE__*/
82function (_React$Component) {
83 _inherits(Calendar, _React$Component);
84
85 function Calendar(props) {
86 var _this;
87
88 _classCallCheck(this, Calendar);
89
90 _this = _possibleConstructorReturn(this, _getPrototypeOf(Calendar).call(this, props));
91
92 _defineProperty(_assertThisInitialized(_this), "dateHelpers", void 0);
93
94 _defineProperty(_assertThisInitialized(_this), "calendar", void 0);
95
96 _defineProperty(_assertThisInitialized(_this), "getDateInView", function () {
97 var _this$props = _this.props,
98 highlightedDate = _this$props.highlightedDate,
99 value = _this$props.value;
100
101 var minDate = _this.dateHelpers.getEffectiveMinDate(_this.props);
102
103 var maxDate = _this.dateHelpers.getEffectiveMaxDate(_this.props);
104
105 var current = _this.dateHelpers.date();
106
107 var initialDate = _this.getSingleDate(value) || highlightedDate;
108
109 if (initialDate) {
110 return initialDate;
111 } else {
112 if (minDate && _this.dateHelpers.isBefore(current, minDate)) {
113 return minDate;
114 } else if (maxDate && _this.dateHelpers.isAfter(current, maxDate)) {
115 return maxDate;
116 }
117 }
118
119 return current;
120 });
121
122 _defineProperty(_assertThisInitialized(_this), "handleMonthChange", function (date) {
123 _this.setHighlightedDate(_this.dateHelpers.getStartOfMonth(date));
124
125 if (_this.props.onMonthChange) {
126 _this.props.onMonthChange({
127 date: date
128 });
129 }
130 });
131
132 _defineProperty(_assertThisInitialized(_this), "handleYearChange", function (date) {
133 _this.setHighlightedDate(date);
134
135 if (_this.props.onYearChange) {
136 _this.props.onYearChange({
137 date: date
138 });
139 }
140 });
141
142 _defineProperty(_assertThisInitialized(_this), "changeMonth", function (_ref) {
143 var date = _ref.date;
144
145 _this.setState({
146 date: date
147 }, function () {
148 return _this.handleMonthChange(_this.state.date);
149 });
150 });
151
152 _defineProperty(_assertThisInitialized(_this), "changeYear", function (_ref2) {
153 var date = _ref2.date;
154
155 _this.setState({
156 date: date
157 }, function () {
158 return _this.handleYearChange(_this.state.date);
159 });
160 });
161
162 _defineProperty(_assertThisInitialized(_this), "renderCalendarHeader", function () {
163 var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.state.date;
164 var order = arguments.length > 1 ? arguments[1] : undefined;
165 return React.createElement(_calendarHeader.default, _extends({}, _this.props, {
166 key: "month-header-".concat(order),
167 date: date,
168 order: order,
169 onMonthChange: _this.changeMonth,
170 onYearChange: _this.changeYear
171 }));
172 });
173
174 _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
175 switch (event.key) {
176 case 'ArrowUp':
177 case 'ArrowDown':
178 case 'ArrowLeft':
179 case 'ArrowRight':
180 case 'Home':
181 case 'End':
182 case 'PageUp':
183 case 'PageDown':
184 _this.handleArrowKey(event.key);
185
186 event.preventDefault();
187 event.stopPropagation();
188 break;
189 }
190 });
191
192 _defineProperty(_assertThisInitialized(_this), "handleArrowKey", function (key) {
193 var oldDate = _this.state.highlightedDate;
194 var highlightedDate = oldDate;
195
196 var currentDate = _this.dateHelpers.date();
197
198 switch (key) {
199 case 'ArrowLeft':
200 // adding `new Date()` as the last option to satisfy Flow
201 highlightedDate = _this.dateHelpers.subDays(highlightedDate ? highlightedDate : currentDate, 1);
202 break;
203
204 case 'ArrowRight':
205 highlightedDate = _this.dateHelpers.addDays( // adding `new Date()` as the last option to satisfy Flow
206 highlightedDate ? highlightedDate : currentDate, 1);
207 break;
208
209 case 'ArrowUp':
210 highlightedDate = _this.dateHelpers.subWeeks( // adding `new Date()` as the last option to satisfy Flow
211 highlightedDate ? highlightedDate : currentDate, 1);
212 break;
213
214 case 'ArrowDown':
215 highlightedDate = _this.dateHelpers.addWeeks( // adding `new Date()` as the last option to satisfy Flow
216 highlightedDate ? highlightedDate : currentDate, 1);
217 break;
218
219 case 'Home':
220 highlightedDate = _this.dateHelpers.getStartOfWeek( // adding `new Date()` as the last option to satisfy Flow
221 highlightedDate ? highlightedDate : currentDate);
222 break;
223
224 case 'End':
225 highlightedDate = _this.dateHelpers.getEndOfWeek( // adding `new Date()` as the last option to satisfy Flow
226 highlightedDate ? highlightedDate : currentDate);
227 break;
228
229 case 'PageUp':
230 highlightedDate = _this.dateHelpers.subMonths( // adding `new Date()` as the last option to satisfy Flow
231 highlightedDate ? highlightedDate : currentDate, 1);
232 break;
233
234 case 'PageDown':
235 highlightedDate = _this.dateHelpers.addMonths( // adding `new Date()` as the last option to satisfy Flow
236 highlightedDate ? highlightedDate : currentDate, 1);
237 break;
238 }
239
240 _this.setState({
241 highlightedDate: highlightedDate,
242 date: highlightedDate
243 });
244 });
245
246 _defineProperty(_assertThisInitialized(_this), "focusCalendar", function () {
247 if (!_this.state.focused) {
248 _this.setState({
249 focused: true
250 });
251 }
252 });
253
254 _defineProperty(_assertThisInitialized(_this), "blurCalendar", function () {
255 if (typeof document !== 'undefined') {
256 var activeElm = document.activeElement;
257
258 if (_this.calendar && !_this.calendar.contains(activeElm)) {
259 _this.setState({
260 focused: false
261 });
262 }
263 }
264 });
265
266 _defineProperty(_assertThisInitialized(_this), "handleTabbing", function (event) {
267 if (typeof document !== 'undefined') {
268 if (event.keyCode === 9) {
269 var activeElm = document.activeElement; // need to look for any tabindex >= 0 and ideally for not disabled
270 // focusable by default elements like input, button, etc.
271
272 var focusable = _this.state.rootElement ? _this.state.rootElement.querySelectorAll('[tabindex="0"]') : null;
273 var length = focusable ? focusable.length : 0;
274
275 if (event.shiftKey) {
276 if (focusable && activeElm === focusable[0]) {
277 event.preventDefault();
278 focusable[length - 1].focus();
279 }
280 } else {
281 if (focusable && activeElm === focusable[length - 1]) {
282 event.preventDefault();
283 focusable[0].focus();
284 }
285 }
286 }
287 }
288 });
289
290 _defineProperty(_assertThisInitialized(_this), "onDayFocus", function (data) {
291 var date = data.date;
292
293 _this.setState({
294 highlightedDate: date
295 });
296
297 _this.focusCalendar();
298
299 _this.props.onDayFocus && _this.props.onDayFocus(data);
300 });
301
302 _defineProperty(_assertThisInitialized(_this), "onDayMouseOver", function (data) {
303 var date = data.date;
304
305 _this.setState({
306 highlightedDate: date
307 });
308
309 _this.props.onDayMouseOver && _this.props.onDayMouseOver(data);
310 });
311
312 _defineProperty(_assertThisInitialized(_this), "onDayMouseLeave", function (data) {
313 var date = data.date;
314
315 _this.setHighlightedDate(date);
316
317 _this.props.onDayMouseLeave && _this.props.onDayMouseLeave(data);
318 });
319
320 _defineProperty(_assertThisInitialized(_this), "handleDateChange", function (data) {
321 var _this$props$onChange = _this.props.onChange,
322 onChange = _this$props$onChange === void 0 ? function (params) {} : _this$props$onChange;
323 var updatedDate = data.date; // We'll need to update the date in time values of internal state
324
325 var newTimeState = _toConsumableArray(_this.state.time); // Apply the currently selected time values (saved in state) to the updated date
326
327
328 if (Array.isArray(data.date)) {
329 updatedDate = data.date.map(function (date, index) {
330 newTimeState[index] = _this.dateHelpers.applyDateToTime(newTimeState[index], date);
331 return newTimeState[index];
332 });
333 } else if (!Array.isArray(_this.props.value) && data.date) {
334 newTimeState[0] = _this.dateHelpers.applyDateToTime(newTimeState[0], data.date);
335 updatedDate = newTimeState[0];
336 } // Update the date in time values of internal state
337
338
339 _this.setState({
340 time: newTimeState
341 });
342
343 onChange({
344 date: updatedDate
345 });
346 });
347
348 _defineProperty(_assertThisInitialized(_this), "handleTimeChange", function (time, index) {
349 var _this$props$onChange2 = _this.props.onChange,
350 onChange = _this$props$onChange2 === void 0 ? function (params) {} : _this$props$onChange2; // Save/update the time value in internal state
351
352 var newTimeState = _toConsumableArray(_this.state.time);
353
354 newTimeState[index] = _this.dateHelpers.applyTimeToDate(newTimeState[index], time);
355
356 _this.setState({
357 time: newTimeState
358 }); // Time change calls calendar's onChange handler
359 // with the date value set to the date with updated time
360
361
362 if (Array.isArray(_this.props.value)) {
363 var dates = _this.props.value.map(function (date, i) {
364 if (index === i) {
365 return _this.dateHelpers.applyTimeToDate(date, time);
366 }
367
368 return date;
369 });
370
371 onChange({
372 date: dates
373 });
374 } else {
375 var date = _this.dateHelpers.applyTimeToDate(_this.props.value, time);
376
377 onChange({
378 date: date
379 });
380 }
381 });
382
383 _defineProperty(_assertThisInitialized(_this), "renderMonths", function (translations) {
384 var _this$props$overrides = _this.props.overrides,
385 overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides;
386 var monthList = [];
387
388 var _getOverrides = (0, _overrides.getOverrides)(overrides.CalendarContainer, _styledComponents.StyledCalendarContainer),
389 _getOverrides2 = _slicedToArray(_getOverrides, 2),
390 CalendarContainer = _getOverrides2[0],
391 calendarContainerProps = _getOverrides2[1];
392
393 var _getOverrides3 = (0, _overrides.getOverrides)(overrides.MonthContainer, _styledComponents.StyledMonthContainer),
394 _getOverrides4 = _slicedToArray(_getOverrides3, 2),
395 MonthContainer = _getOverrides4[0],
396 monthContainerProps = _getOverrides4[1];
397
398 for (var i = 0; i < (_this.props.monthsShown || 1); ++i) {
399 var monthSubComponents = [];
400
401 var monthDate = _this.dateHelpers.addMonths(_this.state.date, i);
402
403 var monthKey = "month-".concat(i);
404 monthSubComponents.push(_this.renderCalendarHeader(monthDate, i));
405 monthSubComponents.push(React.createElement(CalendarContainer, _extends({
406 key: monthKey,
407 ref: function ref(calendar) {
408 _this.calendar = calendar;
409 },
410 role: "grid",
411 "aria-roledescription": translations.ariaRoleDescCalMonth,
412 "aria-multiselectable": _this.props.range || null,
413 onKeyDown: _this.onKeyDown
414 }, calendarContainerProps), React.createElement(_month.default, {
415 adapter: _this.props.adapter,
416 date: monthDate,
417 excludeDates: _this.props.excludeDates,
418 filterDate: _this.props.filterDate,
419 highlightedDate: _this.state.highlightedDate,
420 includeDates: _this.props.includeDates,
421 focusedCalendar: _this.state.focused,
422 range: _this.props.range,
423 locale: _this.props.locale,
424 maxDate: _this.props.maxDate,
425 minDate: _this.props.minDate,
426 month: _this.dateHelpers.getMonth(_this.state.date),
427 onDayBlur: _this.blurCalendar,
428 onDayFocus: _this.onDayFocus,
429 onDayClick: _this.props.onDayClick,
430 onDayMouseOver: _this.onDayMouseOver,
431 onDayMouseLeave: _this.onDayMouseLeave,
432 onChange: _this.handleDateChange,
433 overrides: overrides,
434 value: _this.props.value,
435 peekNextMonth: _this.props.peekNextMonth
436 })));
437 monthList.push(React.createElement("div", {
438 key: "month-component-".concat(i)
439 }, monthSubComponents));
440 }
441
442 return React.createElement(MonthContainer, monthContainerProps, monthList);
443 });
444
445 _defineProperty(_assertThisInitialized(_this), "renderTimeSelect", function (value, onChange, label) {
446 var _this$props$overrides2 = _this.props.overrides,
447 overrides = _this$props$overrides2 === void 0 ? {} : _this$props$overrides2;
448
449 var _getOverrides5 = (0, _overrides.getOverrides)(overrides.TimeSelectContainer, _styledComponents.StyledSelectorContainer),
450 _getOverrides6 = _slicedToArray(_getOverrides5, 2),
451 TimeSelectContainer = _getOverrides6[0],
452 timeSelectContainerProps = _getOverrides6[1];
453
454 var _getOverrides7 = (0, _overrides.getOverrides)(overrides.TimeSelectFormControl, _index.FormControl),
455 _getOverrides8 = _slicedToArray(_getOverrides7, 2),
456 TimeSelectFormControl = _getOverrides8[0],
457 timeSelectFormControlProps = _getOverrides8[1];
458
459 var _getOverrides9 = (0, _overrides.getOverrides)(overrides.TimeSelect, _timepicker.default),
460 _getOverrides10 = _slicedToArray(_getOverrides9, 2),
461 TimeSelect = _getOverrides10[0],
462 timeSelectProps = _getOverrides10[1];
463
464 return React.createElement(TimeSelectContainer, timeSelectContainerProps, React.createElement(TimeSelectFormControl, _extends({
465 label: label
466 }, timeSelectFormControlProps), React.createElement(TimeSelect, _extends({
467 value: value ? _this.dateHelpers.date(value) : value,
468 onChange: onChange,
469 nullable: true
470 }, timeSelectProps))));
471 });
472
473 _defineProperty(_assertThisInitialized(_this), "renderQuickSelect", function () {
474 var _this$props$overrides3 = _this.props.overrides,
475 overrides = _this$props$overrides3 === void 0 ? {} : _this$props$overrides3;
476
477 var _getOverrides11 = (0, _overrides.getOverrides)(overrides.QuickSelectContainer, _styledComponents.StyledSelectorContainer),
478 _getOverrides12 = _slicedToArray(_getOverrides11, 2),
479 QuickSelectContainer = _getOverrides12[0],
480 quickSelectContainerProps = _getOverrides12[1];
481
482 var _getOverrides13 = (0, _overrides.getOverrides)(overrides.QuickSelectFormControl, _index.FormControl),
483 _getOverrides14 = _slicedToArray(_getOverrides13, 2),
484 QuickSelectFormControl = _getOverrides14[0],
485 quickSelectFormControlProps = _getOverrides14[1];
486
487 var _getOverrides15 = (0, _overrides.getOverrides)( //
488 overrides.QuickSelect, _index3.Select),
489 _getOverrides16 = _slicedToArray(_getOverrides15, 2),
490 QuickSelect = _getOverrides16[0],
491 _getOverrides16$ = _getOverrides16[1],
492 quickSelectOverrides = _getOverrides16$.overrides,
493 restQuickSelectProps = _objectWithoutProperties(_getOverrides16$, ["overrides"]);
494
495 if (!_this.props.quickSelect) {
496 return null;
497 }
498
499 var NOW = _this.dateHelpers.set(_this.dateHelpers.date(), {
500 hours: 12,
501 minutes: 0,
502 seconds: 0
503 });
504
505 return React.createElement(_index2.LocaleContext.Consumer, null, function (locale) {
506 return React.createElement(QuickSelectContainer, quickSelectContainerProps, React.createElement(QuickSelectFormControl, _extends({
507 label: locale.datepicker.quickSelectLabel
508 }, quickSelectFormControlProps), React.createElement(QuickSelect, _extends({
509 "aria-label": locale.datepicker.quickSelectAriaLabel,
510 labelKey: "id",
511 onChange: function onChange(params) {
512 if (!params.option) {
513 _this.setState({
514 quickSelectId: null
515 });
516
517 _this.props.onChange && _this.props.onChange({
518 date: []
519 });
520 } else {
521 _this.setState({
522 quickSelectId: params.option.id
523 });
524
525 if (_this.props.onChange) {
526 if (_this.props.range) {
527 _this.props.onChange({
528 date: [params.option.beginDate, params.option.endDate || NOW]
529 });
530 } else {
531 _this.props.onChange({
532 date: params.option.beginDate
533 });
534 }
535 }
536 }
537 },
538 options: _this.props.quickSelectOptions || [{
539 id: locale.datepicker.pastWeek,
540 beginDate: _this.dateHelpers.subWeeks(NOW, 1)
541 }, {
542 id: locale.datepicker.pastMonth,
543 beginDate: _this.dateHelpers.subMonths(NOW, 1)
544 }, {
545 id: locale.datepicker.pastThreeMonths,
546 beginDate: _this.dateHelpers.subMonths(NOW, 3)
547 }, {
548 id: locale.datepicker.pastSixMonths,
549 beginDate: _this.dateHelpers.subMonths(NOW, 6)
550 }, {
551 id: locale.datepicker.pastYear,
552 beginDate: _this.dateHelpers.subYears(NOW, 1)
553 }, {
554 id: locale.datepicker.pastTwoYears,
555 beginDate: _this.dateHelpers.subYears(NOW, 2)
556 }],
557 placeholder: locale.datepicker.quickSelectPlaceholder,
558 value: _this.state.quickSelectId && [{
559 id: _this.state.quickSelectId
560 }],
561 overrides: (0, _overrides.mergeOverrides)({
562 Dropdown: {
563 style: {
564 textAlign: 'start'
565 }
566 }
567 }, quickSelectOverrides)
568 }, restQuickSelectProps))));
569 });
570 });
571
572 var _this$props2 = _this.props,
573 _highlightedDate = _this$props2.highlightedDate,
574 _value = _this$props2.value,
575 adapter = _this$props2.adapter;
576 _this.dateHelpers = new _dateHelpers.default(adapter);
577
578 var dateInView = _this.getDateInView();
579
580 var _time = [];
581
582 if (Array.isArray(_value)) {
583 _time = _toConsumableArray(_value);
584 } else if (_value) {
585 _time = [_value];
586 }
587
588 _this.state = {
589 highlightedDate: _this.getSingleDate(_value) || (_highlightedDate && _this.dateHelpers.isSameMonth(dateInView, _highlightedDate) ? _highlightedDate : _this.dateHelpers.date()),
590 focused: false,
591 date: dateInView,
592 quickSelectId: null,
593 rootElement: null,
594 time: _time
595 };
596 return _this;
597 }
598
599 _createClass(Calendar, [{
600 key: "componentDidMount",
601 value: function componentDidMount() {
602 if (this.props.autoFocusCalendar) {
603 this.focusCalendar();
604 }
605 }
606 }, {
607 key: "componentDidUpdate",
608 value: function componentDidUpdate(prevProps) {
609 if (this.props.highlightedDate && !this.dateHelpers.isSameDay(this.props.highlightedDate, prevProps.highlightedDate)) {
610 this.setState({
611 date: this.props.highlightedDate
612 });
613 }
614
615 if (this.props.autoFocusCalendar && this.props.autoFocusCalendar !== prevProps.autoFocusCalendar) {
616 this.focusCalendar();
617 }
618
619 if (prevProps.value !== this.props.value) {
620 var nextDate = this.getDateInView();
621
622 if (!this.isInView(nextDate)) {
623 this.setState({
624 date: nextDate
625 });
626 }
627 }
628 }
629 }, {
630 key: "isInView",
631 value: function isInView(date) {
632 // we calculate the month delta between the date arg and the date in the state.
633 var currentDate = this.state.date; // First we get the year delta
634
635 var yearDelta = this.dateHelpers.getYear(date) - this.dateHelpers.getYear(currentDate); // then we convert it to months. Then we simply add the date-without-year month delta back in.
636
637 var monthDelta = yearDelta * 12 + this.dateHelpers.getMonth(date) - this.dateHelpers.getMonth(currentDate); // we just check that the delta is between the range given by "this month" (i.e. 0) and "the last month" (i.e. monthsShown)
638
639 return monthDelta >= 0 && monthDelta < (this.props.monthsShown || 1);
640 }
641 }, {
642 key: "getSingleDate",
643 value: function getSingleDate(value) {
644 // need to check this.props.range but flow would complain
645 // at the return value in the else clause
646 if (Array.isArray(value)) {
647 return value[0] || null;
648 }
649
650 return value;
651 }
652 }, {
653 key: "setHighlightedDate",
654 value: function setHighlightedDate(date) {
655 var value = this.props.value;
656 var selected = this.getSingleDate(value);
657 var nextState;
658
659 if (selected && this.dateHelpers.isSameMonth(selected, date) && this.dateHelpers.isSameYear(selected, date)) {
660 nextState = {
661 highlightedDate: selected
662 };
663 } else {
664 nextState = {
665 highlightedDate: date
666 };
667 }
668
669 this.setState(nextState);
670 }
671 }, {
672 key: "render",
673 value: function render() {
674 var _this2 = this;
675
676 var _this$props$overrides4 = this.props.overrides,
677 overrides = _this$props$overrides4 === void 0 ? {} : _this$props$overrides4;
678
679 var _getOverrides17 = (0, _overrides.getOverrides)(overrides.Root, _styledComponents.StyledRoot),
680 _getOverrides18 = _slicedToArray(_getOverrides17, 2),
681 Root = _getOverrides18[0],
682 rootProps = _getOverrides18[1];
683
684 var _concat = [].concat(this.props.value),
685 _concat2 = _slicedToArray(_concat, 2),
686 startDate = _concat2[0],
687 endDate = _concat2[1];
688
689 return React.createElement(_index2.LocaleContext.Consumer, null, function (locale) {
690 return React.createElement(Root, _extends({
691 "data-baseweb": "calendar",
692 ref: function ref(root) {
693 if (root && root instanceof HTMLElement && !_this2.state.rootElement) {
694 _this2.setState({
695 rootElement: root
696 });
697 }
698 },
699 "aria-label": locale.datepicker.ariaLabelCalendar,
700 onKeyDown: _this2.props.trapTabbing ? _this2.handleTabbing : null
701 }, rootProps), _this2.renderMonths({
702 ariaRoleDescCalMonth: locale.datepicker.ariaRoleDescriptionCalendarMonth
703 }), _this2.props.timeSelectStart && _this2.renderTimeSelect(startDate, function (time) {
704 return _this2.handleTimeChange(time, 0);
705 }, locale.datepicker.timeSelectStartLabel), _this2.props.timeSelectEnd && _this2.props.range && _this2.renderTimeSelect(endDate, function (time) {
706 return _this2.handleTimeChange(time, 1);
707 }, locale.datepicker.timeSelectEndLabel), _this2.renderQuickSelect());
708 });
709 }
710 }]);
711
712 return Calendar;
713}(React.Component);
714
715exports.default = Calendar;
716
717_defineProperty(Calendar, "defaultProps", {
718 autoFocusCalendar: false,
719 excludeDates: null,
720 filterDate: null,
721 highlightedDate: null,
722 includeDates: null,
723 range: false,
724 locale: null,
725 maxDate: null,
726 minDate: null,
727 onDayClick: function onDayClick() {},
728 onDayFocus: function onDayFocus() {},
729 onDayMouseOver: function onDayMouseOver() {},
730 onDayMouseLeave: function onDayMouseLeave() {},
731 onMonthChange: function onMonthChange() {},
732 onYearChange: function onYearChange() {},
733 onChange: function onChange() {},
734 orientation: _constants.ORIENTATION.vertical,
735 overrides: {},
736 peekNextMonth: false,
737 adapter: _dateFnsAdapter.default,
738 value: null,
739 trapTabbing: false
740});
\No newline at end of file