Files
Iliyan Angelov 306b20e24a Frontend start
2025-09-14 00:54:48 +03:00

133 lines
5.2 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useCalendarState = exports.createCalendarStateReducer = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _useDateValidation = require("../internals/hooks/validation/useDateValidation");
var _useUtils = require("../internals/hooks/useUtils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const createCalendarStateReducer = (reduceAnimations, disableSwitchToMonthOnDayFocus, utils) => (state, action) => {
switch (action.type) {
case 'changeMonth':
return (0, _extends2.default)({}, state, {
slideDirection: action.direction,
currentMonth: action.newMonth,
isMonthSwitchingAnimating: !reduceAnimations
});
case 'finishMonthSwitchingAnimation':
return (0, _extends2.default)({}, state, {
isMonthSwitchingAnimating: false
});
case 'changeFocusedDay':
{
if (state.focusedDay != null && action.focusedDay != null && utils.isSameDay(action.focusedDay, state.focusedDay)) {
return state;
}
const needMonthSwitch = action.focusedDay != null && !disableSwitchToMonthOnDayFocus && !utils.isSameMonth(state.currentMonth, action.focusedDay);
return (0, _extends2.default)({}, state, {
focusedDay: action.focusedDay,
isMonthSwitchingAnimating: needMonthSwitch && !reduceAnimations && !action.withoutMonthSwitchingAnimation,
currentMonth: needMonthSwitch ? utils.startOfMonth(action.focusedDay) : state.currentMonth,
slideDirection: action.focusedDay != null && utils.isAfterDay(action.focusedDay, state.currentMonth) ? 'left' : 'right'
});
}
default:
throw new Error('missing support');
}
};
exports.createCalendarStateReducer = createCalendarStateReducer;
const useCalendarState = ({
date,
defaultCalendarMonth,
disableFuture,
disablePast,
disableSwitchToMonthOnDayFocus = false,
maxDate,
minDate,
onMonthChange,
reduceAnimations,
shouldDisableDate
}) => {
var _ref;
const now = (0, _useUtils.useNow)();
const utils = (0, _useUtils.useUtils)();
const reducerFn = React.useRef(createCalendarStateReducer(Boolean(reduceAnimations), disableSwitchToMonthOnDayFocus, utils)).current;
const [calendarState, dispatch] = React.useReducer(reducerFn, {
isMonthSwitchingAnimating: false,
focusedDay: date || now,
currentMonth: utils.startOfMonth((_ref = date != null ? date : defaultCalendarMonth) != null ? _ref : now),
slideDirection: 'left'
});
const handleChangeMonth = React.useCallback(payload => {
dispatch((0, _extends2.default)({
type: 'changeMonth'
}, payload));
if (onMonthChange) {
onMonthChange(payload.newMonth);
}
}, [onMonthChange]);
const changeMonth = React.useCallback(newDate => {
const newDateRequested = newDate != null ? newDate : now;
if (utils.isSameMonth(newDateRequested, calendarState.currentMonth)) {
return;
}
handleChangeMonth({
newMonth: utils.startOfMonth(newDateRequested),
direction: utils.isAfterDay(newDateRequested, calendarState.currentMonth) ? 'left' : 'right'
});
}, [calendarState.currentMonth, handleChangeMonth, now, utils]);
const isDateDisabled = (0, _useDateValidation.useIsDayDisabled)({
shouldDisableDate,
minDate,
maxDate,
disableFuture,
disablePast
});
const onMonthSwitchingAnimationEnd = React.useCallback(() => {
dispatch({
type: 'finishMonthSwitchingAnimation'
});
}, []);
const changeFocusedDay = React.useCallback((newFocusedDate, withoutMonthSwitchingAnimation) => {
if (!isDateDisabled(newFocusedDate)) {
dispatch({
type: 'changeFocusedDay',
focusedDay: newFocusedDate,
withoutMonthSwitchingAnimation
});
}
}, [isDateDisabled]);
return {
calendarState,
changeMonth,
changeFocusedDay,
isDateDisabled,
onMonthSwitchingAnimationEnd,
handleChangeMonth
};
};
exports.useCalendarState = useCalendarState;