@material-ui/lab
Version:
Material-UI Lab - Incubator for Material-UI React components.
173 lines (169 loc) • 7.35 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import * as React from 'react';
import clsx from 'clsx';
import Typography from '@material-ui/core/Typography';
import { createStyles, withStyles, useTheme } from '@material-ui/core/styles';
import PickersDay from '../PickersDay/PickersDay';
import { useUtils, useNow } from '../internal/pickers/hooks/useUtils';
import { DAY_SIZE, DAY_MARGIN } from '../internal/pickers/constants/dimensions';
import { useGlobalKeyDown, keycode } from '../internal/pickers/hooks/useKeyDown';
import SlideTransition from './PickersSlideTransition';
var weeksContainerHeight = (DAY_SIZE + DAY_MARGIN * 4) * 6;
export var styles = function styles(theme) {
return createStyles({
root: {
minHeight: weeksContainerHeight
},
loadingContainer: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
minHeight: weeksContainerHeight
},
weekContainer: {
overflow: 'hidden'
},
week: {
margin: "".concat(DAY_MARGIN, "px 0"),
display: 'flex',
justifyContent: 'center'
},
iconButton: {
zIndex: 1,
backgroundColor: theme.palette.background.paper
},
previousMonthButton: {
marginRight: 12
},
daysHeader: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
},
weekDayLabel: {
width: 36,
height: 40,
margin: '0 2px',
textAlign: 'center',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: theme.palette.text.secondary
}
});
};
var _ref = /*#__PURE__*/React.createElement("span", null, "...");
/**
* @ignore - do not document.
*/
function PickersCalendar(props) {
var _useGlobalKeyDown;
var allowKeyboardControl = props.allowKeyboardControl,
allowSameDateSelection = props.allowSameDateSelection,
changeFocusedDay = props.onFocusedDayChange,
classes = props.classes,
className = props.className,
currentMonth = props.currentMonth,
date = props.date,
disableHighlightToday = props.disableHighlightToday,
focusedDay = props.focusedDay,
isDateDisabled = props.isDateDisabled,
isMonthSwitchingAnimating = props.isMonthSwitchingAnimating,
loading = props.loading,
onChange = props.onChange,
onMonthSwitchingAnimationEnd = props.onMonthSwitchingAnimationEnd,
reduceAnimations = props.reduceAnimations,
renderDay = props.renderDay,
_props$renderLoading = props.renderLoading,
renderLoading = _props$renderLoading === void 0 ? function () {
return _ref;
} : _props$renderLoading,
showDaysOutsideCurrentMonth = props.showDaysOutsideCurrentMonth,
slideDirection = props.slideDirection,
TransitionProps = props.TransitionProps;
var now = useNow();
var utils = useUtils();
var theme = useTheme();
var handleDaySelect = React.useCallback(function (day) {
var isFinish = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'finish';
// TODO possibly buggy line figure out and add tests
var finalDate = Array.isArray(date) ? day : utils.mergeDateAndTime(day, date || now);
onChange(finalDate, isFinish);
}, [date, now, onChange, utils]);
var initialDate = Array.isArray(date) ? date[0] : date;
var nowFocusedDay = focusedDay || initialDate || now;
useGlobalKeyDown(Boolean(allowKeyboardControl), (_useGlobalKeyDown = {}, _defineProperty(_useGlobalKeyDown, keycode.ArrowUp, function () {
return changeFocusedDay(utils.addDays(nowFocusedDay, -7));
}), _defineProperty(_useGlobalKeyDown, keycode.ArrowDown, function () {
return changeFocusedDay(utils.addDays(nowFocusedDay, 7));
}), _defineProperty(_useGlobalKeyDown, keycode.ArrowLeft, function () {
return changeFocusedDay(utils.addDays(nowFocusedDay, theme.direction === 'ltr' ? -1 : 1));
}), _defineProperty(_useGlobalKeyDown, keycode.ArrowRight, function () {
return changeFocusedDay(utils.addDays(nowFocusedDay, theme.direction === 'ltr' ? 1 : -1));
}), _defineProperty(_useGlobalKeyDown, keycode.Home, function () {
return changeFocusedDay(utils.startOfWeek(nowFocusedDay));
}), _defineProperty(_useGlobalKeyDown, keycode.End, function () {
return changeFocusedDay(utils.endOfWeek(nowFocusedDay));
}), _defineProperty(_useGlobalKeyDown, keycode.PageUp, function () {
return changeFocusedDay(utils.getNextMonth(nowFocusedDay));
}), _defineProperty(_useGlobalKeyDown, keycode.PageDown, function () {
return changeFocusedDay(utils.getPreviousMonth(nowFocusedDay));
}), _useGlobalKeyDown));
var currentMonthNumber = utils.getMonth(currentMonth);
var selectedDates = (Array.isArray(date) ? date : [date]).filter(Boolean).map(function (selectedDateItem) {
return selectedDateItem && utils.startOfDay(selectedDateItem);
});
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
className: classes.daysHeader
}, utils.getWeekdays().map(function (day, i) {
return /*#__PURE__*/React.createElement(Typography, {
"aria-hidden": true,
key: day + i.toString(),
variant: "caption",
className: classes.weekDayLabel
}, day.charAt(0).toUpperCase());
})), loading ? /*#__PURE__*/React.createElement("div", {
className: classes.loadingContainer
}, renderLoading()) : /*#__PURE__*/React.createElement(SlideTransition, _extends({
transKey: currentMonthNumber,
onExited: onMonthSwitchingAnimationEnd,
reduceAnimations: reduceAnimations,
slideDirection: slideDirection,
className: clsx(classes.root, className)
}, TransitionProps), /*#__PURE__*/React.createElement("div", {
role: "grid",
className: classes.weekContainer
}, utils.getWeekArray(currentMonth).map(function (week) {
return /*#__PURE__*/React.createElement("div", {
role: "row",
key: "week-".concat(week[0]),
className: classes.week
}, week.map(function (day) {
var dayProps = {
key: day === null || day === void 0 ? void 0 : day.toString(),
day: day,
role: 'cell',
isAnimating: isMonthSwitchingAnimating,
disabled: isDateDisabled(day),
allowKeyboardControl: allowKeyboardControl,
allowSameDateSelection: allowSameDateSelection,
focused: allowKeyboardControl && Boolean(focusedDay) && utils.isSameDay(day, nowFocusedDay),
today: utils.isSameDay(day, now),
outsideCurrentMonth: utils.getMonth(day) !== currentMonthNumber,
selected: selectedDates.some(function (selectedDate) {
return selectedDate && utils.isSameDay(selectedDate, day);
}),
disableHighlightToday: disableHighlightToday,
showDaysOutsideCurrentMonth: showDaysOutsideCurrentMonth,
focusable: allowKeyboardControl && Boolean(nowFocusedDay) && utils.toJsDate(nowFocusedDay).getDate() === utils.toJsDate(day).getDate(),
onDayFocus: changeFocusedDay,
onDaySelect: handleDaySelect
};
return renderDay ? renderDay(day, selectedDates, dayProps) : /*#__PURE__*/React.createElement(PickersDay, dayProps);
}));
}))));
}
export default withStyles(styles, {
name: 'MuiPickersCalendar'
})(PickersCalendar);