@material-ui/lab
Version:
Laboratory for new Material-UI modules.
175 lines (163 loc) • 5.46 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import Fade from '@material-ui/core/Fade';
import { styled } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import { useUtils } from '../internal/pickers/hooks/useUtils';
import FadeTransitionGroup from './PickersFadeTransitionGroup';
// tslint:disable-next-line no-relative-import-in-test
import ArrowDropDownIcon from '../internal/svg-icons/ArrowDropDown';
import PickersArrowSwitcher from '../internal/pickers/PickersArrowSwitcher';
import { usePreviousMonthDisabled, useNextMonthDisabled } from '../internal/pickers/hooks/date-helpers-hooks';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const PickersCalendarHeaderRoot = styled('div', {
skipSx: true
})({
display: 'flex',
alignItems: 'center',
marginTop: 16,
marginBottom: 8,
paddingLeft: 24,
paddingRight: 12,
// prevent jumping in safari
maxHeight: 30,
minHeight: 30
});
const PickersCalendarHeaderLabel = styled('div', {
skipSx: true
})(({
theme
}) => _extends({
display: 'flex',
maxHeight: 30,
overflow: 'hidden',
alignItems: 'center',
cursor: 'pointer',
marginRight: 'auto'
}, theme.typography.body1, {
fontWeight: theme.typography.fontWeightMedium
}));
const PickersCalendarHeaderLabelItem = styled('div', {
skipSx: true
})({
marginRight: 6
});
const PickersCalendarHeaderSwitchViewButton = styled(IconButton, {
skipSx: true
})({
marginRight: 'auto'
});
const PickersCalendarHeaderSwitchView = styled(ArrowDropDownIcon, {
skipSx: true
})(({
theme,
styleProps
}) => _extends({
willChange: 'transform',
transition: theme.transitions.create('transform'),
transform: 'rotate(0deg)'
}, styleProps.openView === 'year' && {
transform: 'rotate(180deg)'
}));
function getSwitchingViewAriaText(view) {
return view === 'year' ? 'year view is open, switch to calendar view' : 'calendar view is open, switch to year view';
}
/**
* @ignore - do not document.
*/
function PickersCalendarHeader(props) {
const {
components = {},
componentsProps = {},
currentMonth: month,
disableFuture,
disablePast,
getViewSwitchingButtonText = getSwitchingViewAriaText,
leftArrowButtonText = 'Previous month',
maxDate,
minDate,
onMonthChange,
onViewChange,
openView: currentView,
reduceAnimations,
rightArrowButtonText = 'Next month',
views
} = props;
const utils = useUtils();
const switchViewButtonProps = componentsProps.switchViewButton || {};
const selectNextMonth = () => onMonthChange(utils.getNextMonth(month), 'left');
const selectPreviousMonth = () => onMonthChange(utils.getPreviousMonth(month), 'right');
const isNextMonthDisabled = useNextMonthDisabled(month, {
disableFuture,
maxDate
});
const isPreviousMonthDisabled = usePreviousMonthDisabled(month, {
disablePast,
minDate
});
const handleToggleView = () => {
if (views.length === 1 || !onViewChange) {
return;
}
if (views.length === 2) {
onViewChange(views.find(view => view !== currentView) || views[0]);
} else {
// switching only between first 2
const nextIndexToOpen = views.indexOf(currentView) !== 0 ? 0 : 1;
onViewChange(views[nextIndexToOpen]);
}
}; // No need to display more information
if (views.length === 1 && views[0] === 'year') {
return null;
}
const styleProps = props;
return /*#__PURE__*/_jsxs(PickersCalendarHeaderRoot, {
styleProps: styleProps,
children: [/*#__PURE__*/_jsxs(PickersCalendarHeaderLabel, {
role: "presentation",
onClick: handleToggleView,
styleProps: styleProps,
children: [/*#__PURE__*/_jsx(FadeTransitionGroup, {
reduceAnimations: reduceAnimations,
transKey: utils.format(month, 'month'),
children: /*#__PURE__*/_jsx(PickersCalendarHeaderLabelItem, {
"aria-live": "polite",
styleProps: styleProps,
children: utils.format(month, 'month')
})
}), /*#__PURE__*/_jsx(FadeTransitionGroup, {
reduceAnimations: reduceAnimations,
transKey: utils.format(month, 'year'),
children: /*#__PURE__*/_jsx(PickersCalendarHeaderLabelItem, {
"aria-live": "polite",
styleProps: styleProps,
children: utils.format(month, 'year')
})
}), views.length > 1 && /*#__PURE__*/_jsx(PickersCalendarHeaderSwitchViewButton, _extends({
size: "small",
as: components.SwitchViewButton,
"aria-label": getViewSwitchingButtonText(currentView)
}, switchViewButtonProps, {
styleProps: _extends({}, styleProps, switchViewButtonProps),
children: /*#__PURE__*/_jsx(PickersCalendarHeaderSwitchView, {
as: components.SwitchViewIcon,
styleProps: styleProps
})
}))]
}), /*#__PURE__*/_jsx(Fade, {
in: currentView === 'day',
children: /*#__PURE__*/_jsx(PickersArrowSwitcher, {
leftArrowButtonText: leftArrowButtonText,
rightArrowButtonText: rightArrowButtonText,
components: components,
componentsProps: componentsProps,
onLeftClick: selectPreviousMonth,
onRightClick: selectNextMonth,
isLeftDisabled: isPreviousMonthDisabled,
isRightDisabled: isNextMonthDisabled
})
})]
});
}
export default PickersCalendarHeader;