UNPKG

21.1 kBJavaScriptView Raw
1import React__default, { useCallback, createElement, cloneElement, Fragment, Component, useEffect } from 'react';
2import { node, bool, func } from 'prop-types';
3import { u as useUtils } from './useUtils-cfb96ac9.js';
4import clsx from 'clsx';
5import _extends from '@babel/runtime/helpers/esm/extends';
6import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
7import Typography from '@material-ui/core/Typography';
8import { makeStyles, useTheme, withStyles } from '@material-ui/core/styles';
9import { a as arrayIncludes, r as runKeyHandler, V as VariantContext } from './Wrapper-241966d7.js';
10import IconButton from '@material-ui/core/IconButton';
11import SvgIcon from '@material-ui/core/SvgIcon';
12import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
13import _createClass from '@babel/runtime/helpers/esm/createClass';
14import _possibleConstructorReturn from '@babel/runtime/helpers/esm/possibleConstructorReturn';
15import _getPrototypeOf from '@babel/runtime/helpers/esm/getPrototypeOf';
16import _inherits from '@babel/runtime/helpers/esm/inherits';
17import Day from './Day.js';
18import { TransitionGroup, CSSTransition } from 'react-transition-group';
19import CircularProgress from '@material-ui/core/CircularProgress';
20
21var findClosestEnabledDate = function findClosestEnabledDate(_ref) {
22 var date = _ref.date,
23 utils = _ref.utils,
24 minDate = _ref.minDate,
25 maxDate = _ref.maxDate,
26 disableFuture = _ref.disableFuture,
27 disablePast = _ref.disablePast,
28 shouldDisableDate = _ref.shouldDisableDate;
29 var today = utils.startOfDay(utils.date());
30
31 if (disablePast && utils.isBefore(minDate, today)) {
32 minDate = today;
33 }
34
35 if (disableFuture && utils.isAfter(maxDate, today)) {
36 maxDate = today;
37 }
38
39 var forward = date;
40 var backward = date;
41
42 if (utils.isBefore(date, minDate)) {
43 forward = utils.date(minDate);
44 backward = null;
45 }
46
47 if (utils.isAfter(date, maxDate)) {
48 if (backward) {
49 backward = utils.date(maxDate);
50 }
51
52 forward = null;
53 }
54
55 while (forward || backward) {
56 if (forward && utils.isAfter(forward, maxDate)) {
57 forward = null;
58 }
59
60 if (backward && utils.isBefore(backward, minDate)) {
61 backward = null;
62 }
63
64 if (forward) {
65 if (!shouldDisableDate(forward)) {
66 return forward;
67 }
68
69 forward = utils.addDays(forward, 1);
70 }
71
72 if (backward) {
73 if (!shouldDisableDate(backward)) {
74 return backward;
75 }
76
77 backward = utils.addDays(backward, -1);
78 }
79 } // fallback to today if no enabled days
80
81
82 return utils.date();
83};
84var isYearOnlyView = function isYearOnlyView(views) {
85 return views.length === 1 && views[0] === 'year';
86};
87var isYearAndMonthViews = function isYearAndMonthViews(views) {
88 return views.length === 2 && arrayIncludes(views, 'month') && arrayIncludes(views, 'year');
89};
90var getFormatByViews = function getFormatByViews(views, utils) {
91 if (isYearOnlyView(views)) {
92 return utils.yearFormat;
93 }
94
95 if (isYearAndMonthViews(views)) {
96 return utils.yearMonthFormat;
97 }
98
99 return utils.dateFormat;
100};
101
102var DayWrapper = function DayWrapper(_ref) {
103 var children = _ref.children,
104 value = _ref.value,
105 disabled = _ref.disabled,
106 onSelect = _ref.onSelect,
107 dayInCurrentMonth = _ref.dayInCurrentMonth,
108 other = _objectWithoutProperties(_ref, ["children", "value", "disabled", "onSelect", "dayInCurrentMonth"]);
109
110 var handleClick = useCallback(function () {
111 return onSelect(value);
112 }, [onSelect, value]);
113 return createElement("div", _extends({
114 role: "presentation",
115 onClick: dayInCurrentMonth && !disabled ? handleClick : undefined,
116 onKeyPress: dayInCurrentMonth && !disabled ? handleClick : undefined
117 }, other), children);
118};
119
120var animationDuration = 350;
121var useStyles = makeStyles(function (theme) {
122 var slideTransition = theme.transitions.create('transform', {
123 duration: animationDuration,
124 easing: 'cubic-bezier(0.35, 0.8, 0.4, 1)'
125 });
126 return {
127 transitionContainer: {
128 display: 'block',
129 position: 'relative',
130 '& > *': {
131 position: 'absolute',
132 top: 0,
133 right: 0,
134 left: 0
135 }
136 },
137 'slideEnter-left': {
138 willChange: 'transform',
139 transform: 'translate(100%)'
140 },
141 'slideEnter-right': {
142 willChange: 'transform',
143 transform: 'translate(-100%)'
144 },
145 slideEnterActive: {
146 transform: 'translate(0%)',
147 transition: slideTransition
148 },
149 slideExit: {
150 transform: 'translate(0%)'
151 },
152 'slideExitActiveLeft-left': {
153 willChange: 'transform',
154 transform: 'translate(-200%)',
155 transition: slideTransition
156 },
157 'slideExitActiveLeft-right': {
158 willChange: 'transform',
159 transform: 'translate(200%)',
160 transition: slideTransition
161 }
162 };
163}, {
164 name: 'MuiPickersSlideTransition'
165});
166
167var SlideTransition = function SlideTransition(_ref) {
168 var children = _ref.children,
169 transKey = _ref.transKey,
170 slideDirection = _ref.slideDirection,
171 _ref$className = _ref.className,
172 className = _ref$className === void 0 ? null : _ref$className;
173 var classes = useStyles();
174 var transitionClasses = {
175 exit: classes.slideExit,
176 enterActive: classes.slideEnterActive,
177 // @ts-ignore
178 enter: classes['slideEnter-' + slideDirection],
179 // @ts-ignore
180 exitActive: classes['slideExitActiveLeft-' + slideDirection]
181 };
182 return createElement(TransitionGroup, {
183 className: clsx(classes.transitionContainer, className),
184 childFactory: function childFactory(element) {
185 return cloneElement(element, {
186 classNames: transitionClasses
187 });
188 }
189 }, createElement(CSSTransition, {
190 mountOnEnter: true,
191 unmountOnExit: true,
192 key: transKey + slideDirection,
193 timeout: animationDuration,
194 classNames: transitionClasses,
195 children: children
196 }));
197};
198
199var ArrowLeftIcon = function ArrowLeftIcon(props) {
200 return React__default.createElement(SvgIcon, props, React__default.createElement("path", {
201 d: "M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"
202 }), React__default.createElement("path", {
203 fill: "none",
204 d: "M0 0h24v24H0V0z"
205 }));
206};
207
208var ArrowRightIcon = function ArrowRightIcon(props) {
209 return React__default.createElement(SvgIcon, props, React__default.createElement("path", {
210 d: "M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"
211 }), React__default.createElement("path", {
212 fill: "none",
213 d: "M0 0h24v24H0V0z"
214 }));
215};
216
217var useStyles$1 = makeStyles(function (theme) {
218 return {
219 switchHeader: {
220 display: 'flex',
221 justifyContent: 'space-between',
222 alignItems: 'center',
223 marginTop: theme.spacing(0.5),
224 marginBottom: theme.spacing(1)
225 },
226 transitionContainer: {
227 width: '100%',
228 overflow: 'hidden',
229 height: 23
230 },
231 iconButton: {
232 zIndex: 1,
233 backgroundColor: theme.palette.background.paper
234 },
235 daysHeader: {
236 display: 'flex',
237 justifyContent: 'center',
238 alignItems: 'center',
239 maxHeight: 16
240 },
241 dayLabel: {
242 width: 36,
243 margin: '0 2px',
244 textAlign: 'center',
245 color: theme.palette.text.hint
246 }
247 };
248}, {
249 name: 'MuiPickersCalendarHeader'
250});
251var CalendarHeader = function CalendarHeader(_ref) {
252 var currentMonth = _ref.currentMonth,
253 onMonthChange = _ref.onMonthChange,
254 leftArrowIcon = _ref.leftArrowIcon,
255 rightArrowIcon = _ref.rightArrowIcon,
256 leftArrowButtonProps = _ref.leftArrowButtonProps,
257 rightArrowButtonProps = _ref.rightArrowButtonProps,
258 disablePrevMonth = _ref.disablePrevMonth,
259 disableNextMonth = _ref.disableNextMonth,
260 slideDirection = _ref.slideDirection;
261 var utils = useUtils();
262 var classes = useStyles$1();
263 var theme = useTheme();
264 var rtl = theme.direction === 'rtl';
265
266 var selectNextMonth = function selectNextMonth() {
267 return onMonthChange(utils.getNextMonth(currentMonth), 'left');
268 };
269
270 var selectPreviousMonth = function selectPreviousMonth() {
271 return onMonthChange(utils.getPreviousMonth(currentMonth), 'right');
272 };
273
274 return createElement("div", null, createElement("div", {
275 className: classes.switchHeader
276 }, createElement(IconButton, _extends({}, leftArrowButtonProps, {
277 disabled: disablePrevMonth,
278 onClick: selectPreviousMonth,
279 className: classes.iconButton
280 }), rtl ? rightArrowIcon : leftArrowIcon), createElement(SlideTransition, {
281 slideDirection: slideDirection,
282 transKey: currentMonth.toString(),
283 className: classes.transitionContainer
284 }, createElement(Typography, {
285 align: "center",
286 variant: "body1"
287 }, utils.getCalendarHeaderText(currentMonth))), createElement(IconButton, _extends({}, rightArrowButtonProps, {
288 disabled: disableNextMonth,
289 onClick: selectNextMonth,
290 className: classes.iconButton
291 }), rtl ? leftArrowIcon : rightArrowIcon)), createElement("div", {
292 className: classes.daysHeader
293 }, utils.getWeekdays().map(function (day, index) {
294 return createElement(Typography, {
295 key: index // eslint-disable-line react/no-array-index-key
296 ,
297 variant: "caption",
298 className: classes.dayLabel
299 }, day);
300 })));
301};
302CalendarHeader.displayName = 'CalendarHeader';
303process.env.NODE_ENV !== "production" ? CalendarHeader.propTypes = {
304 leftArrowIcon: node,
305 rightArrowIcon: node,
306 disablePrevMonth: bool,
307 disableNextMonth: bool
308} : void 0;
309CalendarHeader.defaultProps = {
310 leftArrowIcon: createElement(ArrowLeftIcon, null),
311 rightArrowIcon: createElement(ArrowRightIcon, null),
312 disablePrevMonth: false,
313 disableNextMonth: false
314};
315
316var withUtils = function withUtils() {
317 return function (Component) {
318 var WithUtils = function WithUtils(props) {
319 var utils = useUtils();
320 return createElement(Component, _extends({
321 utils: utils
322 }, props));
323 };
324
325 WithUtils.displayName = "WithUtils(".concat(Component.displayName || Component.name, ")");
326 return WithUtils;
327 };
328};
329
330var KeyDownListener = function KeyDownListener(_ref) {
331 var onKeyDown = _ref.onKeyDown;
332 useEffect(function () {
333 window.addEventListener('keydown', onKeyDown);
334 return function () {
335 window.removeEventListener('keydown', onKeyDown);
336 };
337 }, [onKeyDown]);
338 return null;
339};
340
341var Calendar =
342/*#__PURE__*/
343function (_React$Component) {
344 _inherits(Calendar, _React$Component);
345
346 function Calendar() {
347 var _getPrototypeOf2;
348
349 var _this;
350
351 _classCallCheck(this, Calendar);
352
353 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
354 args[_key] = arguments[_key];
355 }
356
357 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Calendar)).call.apply(_getPrototypeOf2, [this].concat(args)));
358 _this.state = {
359 slideDirection: 'left',
360 currentMonth: _this.props.utils.startOfMonth(_this.props.date),
361 loadingQueue: 0
362 };
363
364 _this.pushToLoadingQueue = function () {
365 var loadingQueue = _this.state.loadingQueue + 1;
366
367 _this.setState({
368 loadingQueue: loadingQueue
369 });
370 };
371
372 _this.popFromLoadingQueue = function () {
373 var loadingQueue = _this.state.loadingQueue;
374 loadingQueue = loadingQueue <= 0 ? 0 : loadingQueue - 1;
375
376 _this.setState({
377 loadingQueue: loadingQueue
378 });
379 };
380
381 _this.handleChangeMonth = function (newMonth, slideDirection) {
382 _this.setState({
383 currentMonth: newMonth,
384 slideDirection: slideDirection
385 });
386
387 if (_this.props.onMonthChange) {
388 var returnVal = _this.props.onMonthChange(newMonth);
389
390 if (returnVal) {
391 _this.pushToLoadingQueue();
392
393 returnVal.then(function () {
394 _this.popFromLoadingQueue();
395 });
396 }
397 }
398 };
399
400 _this.validateMinMaxDate = function (day) {
401 var _this$props = _this.props,
402 minDate = _this$props.minDate,
403 maxDate = _this$props.maxDate,
404 utils = _this$props.utils,
405 disableFuture = _this$props.disableFuture,
406 disablePast = _this$props.disablePast;
407 var now = utils.date();
408 return Boolean(disableFuture && utils.isAfterDay(day, now) || disablePast && utils.isBeforeDay(day, now) || minDate && utils.isBeforeDay(day, utils.date(minDate)) || maxDate && utils.isAfterDay(day, utils.date(maxDate)));
409 };
410
411 _this.shouldDisablePrevMonth = function () {
412 var _this$props2 = _this.props,
413 utils = _this$props2.utils,
414 disablePast = _this$props2.disablePast,
415 minDate = _this$props2.minDate;
416 var now = utils.date();
417 var firstEnabledMonth = utils.startOfMonth(disablePast && utils.isAfter(now, utils.date(minDate)) ? now : utils.date(minDate));
418 return !utils.isBefore(firstEnabledMonth, _this.state.currentMonth);
419 };
420
421 _this.shouldDisableNextMonth = function () {
422 var _this$props3 = _this.props,
423 utils = _this$props3.utils,
424 disableFuture = _this$props3.disableFuture,
425 maxDate = _this$props3.maxDate;
426 var now = utils.date();
427 var lastEnabledMonth = utils.startOfMonth(disableFuture && utils.isBefore(now, utils.date(maxDate)) ? now : utils.date(maxDate));
428 return !utils.isAfter(lastEnabledMonth, _this.state.currentMonth);
429 };
430
431 _this.shouldDisableDate = function (day) {
432 var shouldDisableDate = _this.props.shouldDisableDate;
433 return _this.validateMinMaxDate(day) || Boolean(shouldDisableDate && shouldDisableDate(day));
434 };
435
436 _this.handleDaySelect = function (day) {
437 var isFinish = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
438 var _this$props4 = _this.props,
439 date = _this$props4.date,
440 utils = _this$props4.utils;
441
442 _this.props.onChange(utils.mergeDateAndTime(day, date), isFinish);
443 };
444
445 _this.moveToDay = function (day) {
446 var utils = _this.props.utils;
447
448 if (day && !_this.shouldDisableDate(day)) {
449 if (utils.getMonth(day) !== utils.getMonth(_this.state.currentMonth)) {
450 _this.handleChangeMonth(utils.startOfMonth(day), 'left');
451 }
452
453 _this.handleDaySelect(day, false);
454 }
455 };
456
457 _this.handleKeyDown = function (event) {
458 var _this$props5 = _this.props,
459 theme = _this$props5.theme,
460 date = _this$props5.date,
461 utils = _this$props5.utils;
462 runKeyHandler(event, {
463 ArrowUp: function ArrowUp() {
464 return _this.moveToDay(utils.addDays(date, -7));
465 },
466 ArrowDown: function ArrowDown() {
467 return _this.moveToDay(utils.addDays(date, 7));
468 },
469 ArrowLeft: function ArrowLeft() {
470 return _this.moveToDay(utils.addDays(date, theme.direction === 'ltr' ? -1 : 1));
471 },
472 ArrowRight: function ArrowRight() {
473 return _this.moveToDay(utils.addDays(date, theme.direction === 'ltr' ? 1 : -1));
474 }
475 });
476 };
477
478 _this.renderWeeks = function () {
479 var _this$props6 = _this.props,
480 utils = _this$props6.utils,
481 classes = _this$props6.classes;
482 var weeks = utils.getWeekArray(_this.state.currentMonth);
483 return weeks.map(function (week) {
484 return createElement("div", {
485 key: "week-".concat(week[0].toString()),
486 className: classes.week
487 }, _this.renderDays(week));
488 });
489 };
490
491 _this.renderDays = function (week) {
492 var _this$props7 = _this.props,
493 date = _this$props7.date,
494 renderDay = _this$props7.renderDay,
495 utils = _this$props7.utils;
496 var now = utils.date();
497 var selectedDate = utils.startOfDay(date);
498 var currentMonthNumber = utils.getMonth(_this.state.currentMonth);
499 return week.map(function (day) {
500 var disabled = _this.shouldDisableDate(day);
501
502 var isDayInCurrentMonth = utils.getMonth(day) === currentMonthNumber;
503 var dayComponent = createElement(Day, {
504 disabled: disabled,
505 current: utils.isSameDay(day, now),
506 hidden: !isDayInCurrentMonth,
507 selected: utils.isSameDay(selectedDate, day)
508 }, utils.getDayText(day));
509
510 if (renderDay) {
511 dayComponent = renderDay(day, selectedDate, isDayInCurrentMonth, dayComponent);
512 }
513
514 return createElement(DayWrapper, {
515 value: day,
516 key: day.toString(),
517 disabled: disabled,
518 dayInCurrentMonth: isDayInCurrentMonth,
519 onSelect: _this.handleDaySelect
520 }, dayComponent);
521 });
522 };
523
524 return _this;
525 }
526
527 _createClass(Calendar, [{
528 key: "componentDidMount",
529 value: function componentDidMount() {
530 var _this$props8 = this.props,
531 date = _this$props8.date,
532 minDate = _this$props8.minDate,
533 maxDate = _this$props8.maxDate,
534 utils = _this$props8.utils,
535 disablePast = _this$props8.disablePast,
536 disableFuture = _this$props8.disableFuture;
537
538 if (this.shouldDisableDate(date)) {
539 var closestEnabledDate = findClosestEnabledDate({
540 date: date,
541 utils: utils,
542 minDate: utils.date(minDate),
543 maxDate: utils.date(maxDate),
544 disablePast: Boolean(disablePast),
545 disableFuture: Boolean(disableFuture),
546 shouldDisableDate: this.shouldDisableDate
547 });
548 this.handleDaySelect(closestEnabledDate, false);
549 }
550 }
551 }, {
552 key: "render",
553 value: function render() {
554 var _this$state = this.state,
555 currentMonth = _this$state.currentMonth,
556 slideDirection = _this$state.slideDirection;
557 var _this$props9 = this.props,
558 classes = _this$props9.classes,
559 allowKeyboardControl = _this$props9.allowKeyboardControl,
560 leftArrowButtonProps = _this$props9.leftArrowButtonProps,
561 leftArrowIcon = _this$props9.leftArrowIcon,
562 rightArrowButtonProps = _this$props9.rightArrowButtonProps,
563 rightArrowIcon = _this$props9.rightArrowIcon,
564 loadingIndicator = _this$props9.loadingIndicator;
565 var loadingElement = loadingIndicator ? loadingIndicator : createElement(CircularProgress, null);
566 return createElement(Fragment, null, allowKeyboardControl && this.context !== 'static' && createElement(KeyDownListener, {
567 onKeyDown: this.handleKeyDown
568 }), createElement(CalendarHeader, {
569 currentMonth: currentMonth,
570 slideDirection: slideDirection,
571 onMonthChange: this.handleChangeMonth,
572 leftArrowIcon: leftArrowIcon,
573 leftArrowButtonProps: leftArrowButtonProps,
574 rightArrowIcon: rightArrowIcon,
575 rightArrowButtonProps: rightArrowButtonProps,
576 disablePrevMonth: this.shouldDisablePrevMonth(),
577 disableNextMonth: this.shouldDisableNextMonth()
578 }), createElement(SlideTransition, {
579 slideDirection: slideDirection,
580 transKey: currentMonth.toString(),
581 className: classes.transitionContainer
582 }, createElement(Fragment, null, this.state.loadingQueue > 0 && createElement("div", {
583 className: classes.progressContainer
584 }, loadingElement) || createElement("div", null, this.renderWeeks()))));
585 }
586 }], [{
587 key: "getDerivedStateFromProps",
588 value: function getDerivedStateFromProps(nextProps, state) {
589 var utils = nextProps.utils,
590 nextDate = nextProps.date;
591
592 if (!utils.isEqual(nextDate, state.lastDate)) {
593 var nextMonth = utils.getMonth(nextDate);
594 var lastDate = state.lastDate || nextDate;
595 var lastMonth = utils.getMonth(lastDate);
596 return {
597 lastDate: nextDate,
598 currentMonth: nextProps.utils.startOfMonth(nextDate),
599 // prettier-ignore
600 slideDirection: nextMonth === lastMonth ? state.slideDirection : utils.isAfterDay(nextDate, lastDate) ? 'left' : 'right'
601 };
602 }
603
604 return null;
605 }
606 }]);
607
608 return Calendar;
609}(Component);
610Calendar.contextType = VariantContext;
611process.env.NODE_ENV !== "production" ? Calendar.propTypes = {
612 renderDay: func,
613 shouldDisableDate: func,
614 allowKeyboardControl: bool
615} : void 0;
616Calendar.defaultProps = {
617 minDate: new Date('1900-01-01'),
618 maxDate: new Date('2100-01-01'),
619 disablePast: false,
620 disableFuture: false,
621 allowKeyboardControl: true
622};
623var styles = function styles(theme) {
624 return {
625 transitionContainer: {
626 minHeight: 36 * 6,
627 marginTop: theme.spacing(1.5)
628 },
629 progressContainer: {
630 width: '100%',
631 height: '100%',
632 display: 'flex',
633 justifyContent: 'center',
634 alignItems: 'center'
635 },
636 week: {
637 display: 'flex',
638 justifyContent: 'center'
639 }
640 };
641};
642var Calendar$1 = withStyles(styles, {
643 name: 'MuiPickersCalendar',
644 withTheme: true
645})(withUtils()(Calendar));
646
647export { Calendar as C, Calendar$1 as a, isYearAndMonthViews as b, getFormatByViews as g, isYearOnlyView as i, styles as s };
648//# sourceMappingURL=Calendar-11ae61f6.js.map