UNPKG

@material-ui/lab

Version:
337 lines (304 loc) 10.9 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _span; import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { styled, useThemeProps } from '@material-ui/core/styles'; import { unstable_composeClasses as composeClasses, generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled'; import MonthPicker from '../MonthPicker/MonthPicker'; import { useCalendarState } from './useCalendarState'; import { useDefaultDates, useUtils } from '../internal/pickers/hooks/useUtils'; import FadeTransitionGroup from './PickersFadeTransitionGroup'; import PickersCalendar from './PickersCalendar'; import { useViews } from '../internal/pickers/hooks/useViews'; import PickersCalendarHeader from './PickersCalendarHeader'; import YearPicker from '../YearPicker/YearPicker'; import { findClosestEnabledDate } from '../internal/pickers/date-utils'; import PickerView from '../internal/pickers/Picker/PickerView'; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; export function getCalendarPickerUtilityClass(slot) { return generateUtilityClass('MuiCalendarPicker', slot); } export var calendarPickerClasses = generateUtilityClasses('MuiCalendarPicker', ['root', 'viewTransitionContainer']); var useUtilityClasses = function useUtilityClasses(styleProps) { var classes = styleProps.classes; var slots = { root: ['root'], viewTransitionContainer: ['viewTransitionContainer'] }; return composeClasses(slots, getCalendarPickerUtilityClass, classes); }; var CalendarPickerRoot = styled(PickerView, { name: 'MuiCalendarPicker', slot: 'Root', overridesResolver: function overridesResolver(props, styles) { return styles.root; } })({ display: 'flex', flexDirection: 'column' }); var CalendarPickerViewTransitionContainer = styled(FadeTransitionGroup, { name: 'MuiCalendarPicker', slot: 'ViewTransitionContainer', overridesResolver: function overridesResolver(props, styles) { return styles.viewTransitionContainer; } })({ overflowY: 'auto' }); export var defaultReduceAnimations = typeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent); var CalendarPicker = /*#__PURE__*/React.forwardRef(function CalendarPicker(inProps, ref) { var props = useThemeProps({ props: inProps, name: 'MuiCalendarPicker' }); var autoFocus = props.autoFocus, onViewChange = props.onViewChange, date = props.date, _props$disableFuture = props.disableFuture, disableFuture = _props$disableFuture === void 0 ? false : _props$disableFuture, _props$disablePast = props.disablePast, disablePast = _props$disablePast === void 0 ? false : _props$disablePast, defaultCalendarMonth = props.defaultCalendarMonth, _props$loading = props.loading, loading = _props$loading === void 0 ? false : _props$loading, maxDateProp = props.maxDate, minDateProp = props.minDate, onChange = props.onChange, onMonthChange = props.onMonthChange, _props$reduceAnimatio = props.reduceAnimations, reduceAnimations = _props$reduceAnimatio === void 0 ? defaultReduceAnimations : _props$reduceAnimatio, _props$renderLoading = props.renderLoading, renderLoading = _props$renderLoading === void 0 ? function () { return _span || (_span = /*#__PURE__*/_jsx("span", { children: "..." })); } : _props$renderLoading, shouldDisableDate = props.shouldDisableDate, shouldDisableYear = props.shouldDisableYear, view = props.view, _props$views = props.views, views = _props$views === void 0 ? ['year', 'day'] : _props$views, _props$openTo = props.openTo, openTo = _props$openTo === void 0 ? 'day' : _props$openTo, className = props.className, other = _objectWithoutProperties(props, ["autoFocus", "onViewChange", "date", "disableFuture", "disablePast", "defaultCalendarMonth", "loading", "maxDate", "minDate", "onChange", "onMonthChange", "reduceAnimations", "renderLoading", "shouldDisableDate", "shouldDisableYear", "view", "views", "openTo", "className"]); var utils = useUtils(); var defaultDates = useDefaultDates(); var minDate = minDateProp != null ? minDateProp : defaultDates.minDate; var maxDate = maxDateProp != null ? maxDateProp : defaultDates.maxDate; var _useViews = useViews({ view: view, views: views, openTo: openTo, onChange: onChange, onViewChange: onViewChange }), openView = _useViews.openView, setOpenView = _useViews.setOpenView; var _useCalendarState = useCalendarState({ date: date, defaultCalendarMonth: defaultCalendarMonth, reduceAnimations: reduceAnimations, onMonthChange: onMonthChange, minDate: minDate, maxDate: maxDate, shouldDisableDate: shouldDisableDate, disablePast: disablePast, disableFuture: disableFuture }), calendarState = _useCalendarState.calendarState, changeFocusedDay = _useCalendarState.changeFocusedDay, changeMonth = _useCalendarState.changeMonth, isDateDisabled = _useCalendarState.isDateDisabled, handleChangeMonth = _useCalendarState.handleChangeMonth, onMonthSwitchingAnimationEnd = _useCalendarState.onMonthSwitchingAnimationEnd; React.useEffect(function () { if (date && isDateDisabled(date)) { var closestEnabledDate = findClosestEnabledDate({ utils: utils, date: date, minDate: minDate, maxDate: maxDate, disablePast: disablePast, disableFuture: disableFuture, shouldDisableDate: isDateDisabled }); onChange(closestEnabledDate, 'partial'); } // This call is too expensive to run it on each prop change. // So just ensure that we are not rendering disabled as selected on mount. }, []); // eslint-disable-line React.useEffect(function () { if (date) { changeMonth(date); } }, [date]); // eslint-disable-line var styleProps = props; var classes = useUtilityClasses(styleProps); return /*#__PURE__*/_jsxs(CalendarPickerRoot, { ref: ref, className: clsx(classes.root, className), styleProps: styleProps, children: [/*#__PURE__*/_jsx(PickersCalendarHeader, _extends({}, other, { views: views, openView: openView, currentMonth: calendarState.currentMonth, onViewChange: setOpenView, onMonthChange: function onMonthChange(newMonth, direction) { return handleChangeMonth({ newMonth: newMonth, direction: direction }); }, minDate: minDate, maxDate: maxDate, disablePast: disablePast, disableFuture: disableFuture, reduceAnimations: reduceAnimations })), /*#__PURE__*/_jsx(CalendarPickerViewTransitionContainer, { reduceAnimations: reduceAnimations, className: classes.viewTransitionContainer, transKey: openView, styleProps: styleProps, children: /*#__PURE__*/_jsxs("div", { children: [openView === 'year' && /*#__PURE__*/_jsx(YearPicker, _extends({}, other, { autoFocus: autoFocus, date: date, onChange: onChange, minDate: minDate, maxDate: maxDate, disableFuture: disableFuture, disablePast: disablePast, isDateDisabled: isDateDisabled, shouldDisableYear: shouldDisableYear, onFocusedDayChange: changeFocusedDay })), openView === 'month' && /*#__PURE__*/_jsx(MonthPicker, _extends({}, other, { date: date, onChange: onChange, minDate: minDate, maxDate: maxDate, onMonthChange: onMonthChange })), openView === 'day' && /*#__PURE__*/_jsx(PickersCalendar, _extends({}, other, calendarState, { autoFocus: autoFocus, onMonthSwitchingAnimationEnd: onMonthSwitchingAnimationEnd, onFocusedDayChange: changeFocusedDay, reduceAnimations: reduceAnimations, date: date, onChange: onChange, isDateDisabled: isDateDisabled, loading: loading, renderLoading: renderLoading }))] }) })] }); }); process.env.NODE_ENV !== "production" ? CalendarPicker.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit TypeScript types and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * @ignore */ autoFocus: PropTypes.bool, /** * @ignore */ classes: PropTypes.object, /** * @ignore */ className: PropTypes.string, /** * @ignore */ date: PropTypes.any, /** * Default calendar month displayed when `value={null}`. */ defaultCalendarMonth: PropTypes.any, /** * @default false */ disableFuture: PropTypes.bool, /** * @default false */ disablePast: PropTypes.bool, /** * If `true` renders `LoadingComponent` in calendar instead of calendar view. * Can be used to preload information and show it in calendar. * @default false */ loading: PropTypes.bool, /** * Max selectable date. @DateIOType */ maxDate: PropTypes.any, /** * Min selectable date. @DateIOType */ minDate: PropTypes.any, /** * Callback fired on date change */ onChange: PropTypes.func.isRequired, /** * Callback firing on month change. @DateIOType */ onMonthChange: PropTypes.func, /** * Callback fired on view change. */ onViewChange: PropTypes.func, /** * Initially open view. * @default 'day' */ openTo: PropTypes.oneOf(['day', 'month', 'year']), /** * Disable heavy animations. * @default typeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent) */ reduceAnimations: PropTypes.bool, /** * Component displaying when passed `loading` true. * @default () => <span data-mui-test="loading-progress">...</span> */ renderLoading: PropTypes.func, /** * Disable specific date. @DateIOType */ shouldDisableDate: PropTypes.func, /** * Disable specific years dynamically. * Works like `shouldDisableDate` but for year selection view @DateIOType. */ shouldDisableYear: PropTypes.func, /** * Controlled open view. */ view: PropTypes.oneOf(['day', 'month', 'year']), /** * Views for calendar picker. * @default ['year', 'day'] */ views: PropTypes.arrayOf(PropTypes.oneOf(['day', 'month', 'year']).isRequired) } : void 0; /** * * Demos: * * - [Date Picker](https://material-ui.com/components/date-picker/) * * API: * * - [CalendarPicker API](https://material-ui.com/api/calendar-picker/) */ export default CalendarPicker;