UNPKG

@material-ui/lab

Version:
326 lines (296 loc) 9.34 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; var _span; const _excluded = ["autoFocus", "onViewChange", "date", "disableFuture", "disablePast", "defaultCalendarMonth", "loading", "maxDate", "minDate", "onChange", "onMonthChange", "reduceAnimations", "renderLoading", "shouldDisableDate", "shouldDisableYear", "view", "views", "openTo", "className"]; 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 const calendarPickerClasses = generateUtilityClasses('MuiCalendarPicker', ['root', 'viewTransitionContainer']); const useUtilityClasses = styleProps => { const { classes } = styleProps; const slots = { root: ['root'], viewTransitionContainer: ['viewTransitionContainer'] }; return composeClasses(slots, getCalendarPickerUtilityClass, classes); }; const CalendarPickerRoot = styled(PickerView, { name: 'MuiCalendarPicker', slot: 'Root', overridesResolver: (props, styles) => styles.root })({ display: 'flex', flexDirection: 'column' }); const CalendarPickerViewTransitionContainer = styled(FadeTransitionGroup, { name: 'MuiCalendarPicker', slot: 'ViewTransitionContainer', overridesResolver: (props, styles) => styles.viewTransitionContainer })({ overflowY: 'auto' }); export const defaultReduceAnimations = typeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent); const CalendarPicker = /*#__PURE__*/React.forwardRef(function CalendarPicker(inProps, ref) { const props = useThemeProps({ props: inProps, name: 'MuiCalendarPicker' }); const { autoFocus, onViewChange, date, disableFuture = false, disablePast = false, defaultCalendarMonth, loading = false, maxDate: maxDateProp, minDate: minDateProp, onChange, onMonthChange, reduceAnimations = defaultReduceAnimations, renderLoading = () => _span || (_span = /*#__PURE__*/_jsx("span", { children: "..." })), shouldDisableDate, shouldDisableYear, view, views = ['year', 'day'], openTo = 'day', className } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const utils = useUtils(); const defaultDates = useDefaultDates(); const minDate = minDateProp ?? defaultDates.minDate; const maxDate = maxDateProp ?? defaultDates.maxDate; const { openView, setOpenView } = useViews({ view, views, openTo, onChange, onViewChange }); const { calendarState, changeFocusedDay, changeMonth, isDateDisabled, handleChangeMonth, onMonthSwitchingAnimationEnd } = useCalendarState({ date, defaultCalendarMonth, reduceAnimations, onMonthChange, minDate, maxDate, shouldDisableDate, disablePast, disableFuture }); React.useEffect(() => { if (date && isDateDisabled(date)) { const closestEnabledDate = findClosestEnabledDate({ utils, date, minDate, maxDate, disablePast, 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(() => { if (date) { changeMonth(date); } }, [date]); // eslint-disable-line const styleProps = props; const 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: (newMonth, direction) => handleChangeMonth({ newMonth, 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;