UNPKG

@mui/x-date-pickers

Version:

The community edition of the Date and Time Picker components (MUI X).

164 lines (161 loc) 6.04 kB
"use strict"; 'use client'; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCalendarState = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _useEventCallback = _interopRequireDefault(require("@mui/utils/useEventCallback")); var _useIsDateDisabled = require("./useIsDateDisabled"); var _useUtils = require("../internals/hooks/useUtils"); var _valueManagers = require("../internals/utils/valueManagers"); var _getDefaultReferenceDate = require("../internals/utils/getDefaultReferenceDate"); var _dateUtils = require("../internals/utils/date-utils"); const createCalendarStateReducer = (reduceAnimations, utils) => (state, action) => { switch (action.type) { case 'setVisibleDate': return (0, _extends2.default)({}, state, { slideDirection: action.direction, currentMonth: action.month, isMonthSwitchingAnimating: !utils.isSameMonth(action.month, state.currentMonth) && !reduceAnimations && !action.skipAnimation, focusedDay: action.focusedDay }); case 'changeMonthTimezone': { const newTimezone = action.newTimezone; if (utils.getTimezone(state.currentMonth) === newTimezone) { return state; } let newCurrentMonth = utils.setTimezone(state.currentMonth, newTimezone); if (utils.getMonth(newCurrentMonth) !== utils.getMonth(state.currentMonth)) { newCurrentMonth = utils.setMonth(newCurrentMonth, utils.getMonth(state.currentMonth)); } return (0, _extends2.default)({}, state, { currentMonth: newCurrentMonth }); } case 'finishMonthSwitchingAnimation': return (0, _extends2.default)({}, state, { isMonthSwitchingAnimating: false }); default: throw new Error('missing support'); } }; const useCalendarState = params => { const { value, referenceDate: referenceDateProp, disableFuture, disablePast, maxDate, minDate, onMonthChange, onYearChange, reduceAnimations, shouldDisableDate, timezone, getCurrentMonthFromVisibleDate } = params; const utils = (0, _useUtils.useUtils)(); const reducerFn = React.useRef(createCalendarStateReducer(Boolean(reduceAnimations), utils)).current; const referenceDate = React.useMemo(() => { return _valueManagers.singleItemValueManager.getInitialReferenceValue({ value, utils, timezone, props: params, referenceDate: referenceDateProp, granularity: _getDefaultReferenceDate.SECTION_TYPE_GRANULARITY.day }); }, // We want the `referenceDate` to update on prop and `timezone` change (https://github.com/mui/mui-x/issues/10804) // eslint-disable-next-line react-hooks/exhaustive-deps [referenceDateProp, timezone]); const [calendarState, dispatch] = React.useReducer(reducerFn, { isMonthSwitchingAnimating: false, focusedDay: referenceDate, currentMonth: utils.startOfMonth(referenceDate), slideDirection: 'left' }); const isDateDisabled = (0, _useIsDateDisabled.useIsDateDisabled)({ shouldDisableDate, minDate, maxDate, disableFuture, disablePast, timezone }); // Ensure that `calendarState.currentMonth` timezone is updated when `referenceDate` (or timezone changes) // https://github.com/mui/mui-x/issues/10804 React.useEffect(() => { dispatch({ type: 'changeMonthTimezone', newTimezone: utils.getTimezone(referenceDate) }); }, [referenceDate, utils]); const setVisibleDate = (0, _useEventCallback.default)(({ target, reason }) => { if (reason === 'cell-interaction' && calendarState.focusedDay != null && utils.isSameDay(target, calendarState.focusedDay)) { return; } const skipAnimation = reason === 'cell-interaction'; let month; let focusedDay; if (reason === 'cell-interaction') { month = getCurrentMonthFromVisibleDate(target, calendarState.currentMonth); focusedDay = target; } else { month = utils.isSameMonth(target, calendarState.currentMonth) ? calendarState.currentMonth : utils.startOfMonth(target); focusedDay = target; // If the date is disabled, we try to find a non-disabled date inside the same month. if (isDateDisabled(focusedDay)) { const startOfMonth = utils.startOfMonth(target); const endOfMonth = utils.endOfMonth(target); focusedDay = (0, _dateUtils.findClosestEnabledDate)({ utils, date: focusedDay, minDate: utils.isBefore(minDate, startOfMonth) ? startOfMonth : minDate, maxDate: utils.isAfter(maxDate, endOfMonth) ? endOfMonth : maxDate, disablePast, disableFuture, isDateDisabled, timezone }); } } const hasChangedMonth = !utils.isSameMonth(calendarState.currentMonth, month); const hasChangedYear = !utils.isSameYear(calendarState.currentMonth, month); if (hasChangedMonth) { onMonthChange?.(month); } if (hasChangedYear) { onYearChange?.(utils.startOfYear(month)); } dispatch({ type: 'setVisibleDate', month, direction: utils.isAfterDay(month, calendarState.currentMonth) ? 'left' : 'right', focusedDay: calendarState.focusedDay != null && focusedDay != null && utils.isSameDay(focusedDay, calendarState.focusedDay) ? calendarState.focusedDay : focusedDay, skipAnimation }); }); const onMonthSwitchingAnimationEnd = React.useCallback(() => { dispatch({ type: 'finishMonthSwitchingAnimation' }); }, []); return { referenceDate, calendarState, setVisibleDate, isDateDisabled, onMonthSwitchingAnimationEnd }; }; exports.useCalendarState = useCalendarState;