UNPKG

16.2 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 _styledComponents = require("./styled-components.js");
11
12var _dateFnsAdapter = _interopRequireDefault(require("./utils/date-fns-adapter.js"));
13
14var _dateHelpers = _interopRequireDefault(require("./utils/date-helpers.js"));
15
16var _overrides = require("../helpers/overrides.js");
17
18var _index = require("../locale/index.js");
19
20var _focusVisible = require("../utils/focusVisible.js");
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
25
26function _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; }
27
28function _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); }
29
30function _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); }
31
32function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
33
34function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
35
36function _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; }
37
38function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
39
40function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
41
42function _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); } }
43
44function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
45
46function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
47
48function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
49
50function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
51
52function _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); }
53
54function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
55
56function _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; }
57
58var Day =
59/*#__PURE__*/
60function (_React$Component) {
61 _inherits(Day, _React$Component);
62
63 function Day(props) {
64 var _this;
65
66 _classCallCheck(this, Day);
67
68 _this = _possibleConstructorReturn(this, _getPrototypeOf(Day).call(this, props));
69
70 _defineProperty(_assertThisInitialized(_this), "dayElm", void 0);
71
72 _defineProperty(_assertThisInitialized(_this), "state", {
73 isHovered: false,
74 isFocusVisible: false
75 });
76
77 _defineProperty(_assertThisInitialized(_this), "dateHelpers", void 0);
78
79 _defineProperty(_assertThisInitialized(_this), "getDateProp", function () {
80 return _this.props.date === undefined ? _this.dateHelpers.date() : _this.props.date;
81 });
82
83 _defineProperty(_assertThisInitialized(_this), "getMonthProp", function () {
84 return _this.props.month === undefined || _this.props.month === null ? _this.dateHelpers.getMonth(_this.getDateProp()) : _this.props.month;
85 });
86
87 _defineProperty(_assertThisInitialized(_this), "onSelect", function (selectedDate) {
88 var _this$props = _this.props,
89 range = _this$props.range,
90 value = _this$props.value;
91 var date;
92
93 if (Array.isArray(value) && range) {
94 if (!value.length || value.length > 1) {
95 date = [selectedDate];
96 } else if (_this.dateHelpers.isAfter(selectedDate, value[0])) {
97 date = [value[0], selectedDate];
98 } else {
99 date = [selectedDate, value[0]];
100 }
101 } else {
102 date = selectedDate;
103 }
104
105 _this.props.onSelect({
106 date: date
107 });
108 });
109
110 _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (event) {
111 var date = _this.getDateProp();
112
113 var _this$props2 = _this.props,
114 highlighted = _this$props2.highlighted,
115 disabled = _this$props2.disabled;
116
117 if (event.key === 'Enter' && highlighted && !disabled) {
118 event.preventDefault();
119
120 _this.onSelect(date);
121 }
122 });
123
124 _defineProperty(_assertThisInitialized(_this), "onClick", function (event) {
125 var date = _this.getDateProp();
126
127 var disabled = _this.props.disabled;
128
129 if (!disabled) {
130 _this.props.onClick({
131 event: event,
132 date: date
133 });
134
135 _this.onSelect(date);
136 }
137 });
138
139 _defineProperty(_assertThisInitialized(_this), "onFocus", function (event) {
140 if ((0, _focusVisible.isFocusVisible)(event)) {
141 _this.setState({
142 isFocusVisible: true
143 });
144 }
145
146 _this.props.onFocus({
147 event: event,
148 date: _this.getDateProp()
149 });
150 });
151
152 _defineProperty(_assertThisInitialized(_this), "onBlur", function (event) {
153 if (_this.state.isFocusVisible !== false) {
154 _this.setState({
155 isFocusVisible: false
156 });
157 }
158
159 _this.props.onBlur({
160 event: event,
161 date: _this.getDateProp()
162 });
163 });
164
165 _defineProperty(_assertThisInitialized(_this), "onMouseOver", function (event) {
166 _this.setState({
167 isHovered: true
168 });
169
170 _this.props.onMouseOver({
171 event: event,
172 date: _this.getDateProp()
173 });
174 });
175
176 _defineProperty(_assertThisInitialized(_this), "onMouseLeave", function (event) {
177 _this.setState({
178 isHovered: false
179 });
180
181 _this.props.onMouseLeave({
182 event: event,
183 date: _this.getDateProp()
184 });
185 });
186
187 _defineProperty(_assertThisInitialized(_this), "isOutsideMonth", function () {
188 var month = _this.getMonthProp();
189
190 return month !== undefined && month !== _this.dateHelpers.getMonth(_this.getDateProp());
191 });
192
193 _this.dateHelpers = new _dateHelpers.default(props.adapter);
194 return _this;
195 }
196
197 _createClass(Day, [{
198 key: "componentDidMount",
199 value: function componentDidMount() {
200 if (this.dayElm && this.props.focusedCalendar) {
201 if (this.props.highlighted || !this.props.highlightedDate && this.isSelected()) {
202 this.dayElm.focus();
203 }
204 }
205 }
206 }, {
207 key: "componentDidUpdate",
208 value: function componentDidUpdate(prevProps) {
209 if (this.dayElm && this.props.focusedCalendar) {
210 if (this.props.highlighted || !this.props.highlightedDate && this.isSelected()) {
211 this.dayElm.focus();
212 }
213 }
214 }
215 }, {
216 key: "isSelected",
217 value: function isSelected() {
218 var date = this.getDateProp();
219 var value = this.props.value;
220
221 if (Array.isArray(value)) {
222 return this.dateHelpers.isSameDay(date, value[0]) || this.dateHelpers.isSameDay(date, value[1]);
223 } else {
224 return this.dateHelpers.isSameDay(date, value);
225 }
226 } // calculated for range case only
227
228 }, {
229 key: "isPseudoSelected",
230 value: function isPseudoSelected() {
231 var date = this.getDateProp();
232 var value = this.props.value;
233
234 if (Array.isArray(value) && !value[0] && !value[1]) {
235 return false;
236 } // fix flow by passing a specific arg type and remove 'Array.isArray(value)'
237
238
239 if (Array.isArray(value) && value.length > 1) {
240 return this.dateHelpers.isDayInRange(date, value[0], value[1]);
241 }
242 } // calculated for range case only
243
244 }, {
245 key: "isPseudoHighlighted",
246 value: function isPseudoHighlighted() {
247 var date = this.getDateProp();
248 var _this$props3 = this.props,
249 value = _this$props3.value,
250 highlightedDate = _this$props3.highlightedDate;
251
252 if (Array.isArray(value) && !value[0] && !value[1]) {
253 return false;
254 } // fix flow by passing a specific arg type and remove 'Array.isArray(value)'
255
256
257 if (Array.isArray(value) && highlightedDate && value[0] && !value[1]) {
258 if (this.dateHelpers.isAfter(highlightedDate, value[0])) {
259 return this.dateHelpers.isDayInRange(date, value[0], highlightedDate);
260 } else {
261 return this.dateHelpers.isDayInRange(date, highlightedDate, value[0]);
262 }
263 }
264 }
265 }, {
266 key: "getSharedProps",
267 value: function getSharedProps() {
268 var date = this.getDateProp();
269 var _this$props4 = this.props,
270 value = _this$props4.value,
271 highlightedDate = _this$props4.highlightedDate,
272 range = _this$props4.range,
273 highlighted = _this$props4.highlighted;
274 var $isHighlighted = highlighted;
275 var $selected = this.isSelected();
276 var $hasRangeHighlighted = !!(Array.isArray(value) && range && value.length === 1 && highlightedDate && !this.dateHelpers.isSameDay(value[0], highlightedDate));
277 return {
278 $date: date,
279 $disabled: this.props.disabled,
280 $endDate: Array.isArray(value) && this.props.range && $selected && this.dateHelpers.isSameDay(date, value[1]) || false,
281 $hasRangeHighlighted: $hasRangeHighlighted,
282 $hasRangeOnRight: Array.isArray(value) && $hasRangeHighlighted && highlightedDate && value[0] && this.dateHelpers.isAfter(highlightedDate, value[0]),
283 $hasRangeSelected: Array.isArray(value) ? value.length === 2 : false,
284 $highlightedDate: highlightedDate,
285 $isHighlighted: $isHighlighted,
286 $isHovered: this.state.isHovered,
287 $isFocusVisible: this.state.isFocusVisible,
288 $startOfMonth: this.dateHelpers.isStartOfMonth(date),
289 $endOfMonth: this.dateHelpers.isEndOfMonth(date),
290 $outsideMonth: this.isOutsideMonth(),
291 $peekNextMonth: this.props.peekNextMonth,
292 $pseudoHighlighted: this.props.range && !$isHighlighted && !$selected ? this.isPseudoHighlighted() : false,
293 $pseudoSelected: this.props.range && !$selected ? this.isPseudoSelected() : false,
294 $range: this.props.range,
295 $selected: $selected,
296 $startDate: Array.isArray(this.props.value) && this.props.value.length > 1 && this.props.range && $selected ? this.dateHelpers.isSameDay(date, this.props.value[0]) : false
297 };
298 }
299 }, {
300 key: "getAriaLabel",
301 value: function getAriaLabel(sharedProps, localeContext) {
302 var date = this.getDateProp();
303 return "".concat(sharedProps.$selected ? sharedProps.$range ? sharedProps.$endDate ? localeContext.datepicker.selectedEndDateLabel : localeContext.datepicker.selectedStartDateLabel : localeContext.datepicker.selectedLabel : sharedProps.$disabled ? localeContext.datepicker.dateNotAvailableLabel : localeContext.datepicker.chooseLabel, " ").concat(this.dateHelpers.format(date, 'fullOrdinalWeek', this.props.locale), ". ").concat(!sharedProps.$disabled ? localeContext.datepicker.dateAvailableLabel : '');
304 }
305 }, {
306 key: "render",
307 value: function render() {
308 var _this2 = this;
309
310 var date = this.getDateProp();
311 var _this$props5 = this.props,
312 peekNextMonth = _this$props5.peekNextMonth,
313 _this$props5$override = _this$props5.overrides,
314 overrides = _this$props5$override === void 0 ? {} : _this$props5$override;
315 var sharedProps = this.getSharedProps();
316
317 var _getOverrides = (0, _overrides.getOverrides)(overrides.Day, _styledComponents.StyledDay),
318 _getOverrides2 = _slicedToArray(_getOverrides, 2),
319 Day = _getOverrides2[0],
320 dayProps = _getOverrides2[1];
321
322 return !peekNextMonth && sharedProps.$outsideMonth ? React.createElement(Day, _extends({
323 role: "gridcell"
324 }, sharedProps, dayProps, {
325 onFocus: this.onFocus,
326 onBlur: this.onBlur
327 })) : // eslint-disable-next-line jsx-a11y/mouse-events-have-key-events
328 React.createElement(_index.LocaleContext.Consumer, null, function (locale) {
329 return React.createElement(Day, _extends({
330 "aria-label": _this2.getAriaLabel(sharedProps, locale),
331 ref: function ref(dayElm) {
332 _this2.dayElm = dayElm;
333 },
334 role: "gridcell",
335 "aria-roledescription": "button",
336 tabIndex: _this2.props.highlighted || !_this2.props.highlightedDate && _this2.isSelected() ? 0 : -1
337 }, sharedProps, dayProps, {
338 // Adding event handlers after customers overrides in order to
339 // make sure the components functions as expected
340 // We can extract the handlers from props overrides
341 // and call it along with internal handlers by creating an inline handler
342 onFocus: _this2.onFocus,
343 onBlur: _this2.onBlur,
344 onClick: _this2.onClick,
345 onKeyDown: _this2.onKeyDown,
346 onMouseOver: _this2.onMouseOver,
347 onMouseLeave: _this2.onMouseLeave
348 }), _this2.dateHelpers.getDate(date));
349 });
350 }
351 }]);
352
353 return Day;
354}(React.Component);
355
356exports.default = Day;
357
358_defineProperty(Day, "defaultProps", {
359 disabled: false,
360 highlighted: false,
361 range: false,
362 adapter: _dateFnsAdapter.default,
363 onClick: function onClick() {},
364 onSelect: function onSelect() {},
365 onFocus: function onFocus() {},
366 onBlur: function onBlur() {},
367 onMouseOver: function onMouseOver() {},
368 onMouseLeave: function onMouseLeave() {},
369 overrides: {},
370 peekNextMonth: true,
371 value: null
372});
\No newline at end of file