138 lines
5.8 KiB
JavaScript
138 lines
5.8 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 _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");
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
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 = params => {
|
|
const {
|
|
value,
|
|
referenceDate: referenceDateProp,
|
|
defaultCalendarMonth,
|
|
disableFuture,
|
|
disablePast,
|
|
disableSwitchToMonthOnDayFocus = false,
|
|
maxDate,
|
|
minDate,
|
|
onMonthChange,
|
|
reduceAnimations,
|
|
shouldDisableDate,
|
|
timezone
|
|
} = params;
|
|
const utils = (0, _useUtils.useUtils)();
|
|
const reducerFn = React.useRef(createCalendarStateReducer(Boolean(reduceAnimations), disableSwitchToMonthOnDayFocus, utils)).current;
|
|
const referenceDate = React.useMemo(() => {
|
|
let externalReferenceDate = null;
|
|
if (referenceDateProp) {
|
|
externalReferenceDate = referenceDateProp;
|
|
} else if (defaultCalendarMonth) {
|
|
// For `defaultCalendarMonth`, we just want to keep the month and the year to avoid a behavior change.
|
|
externalReferenceDate = utils.startOfMonth(defaultCalendarMonth);
|
|
}
|
|
return _valueManagers.singleItemValueManager.getInitialReferenceValue({
|
|
value,
|
|
utils,
|
|
timezone,
|
|
props: params,
|
|
referenceDate: externalReferenceDate,
|
|
granularity: _getDefaultReferenceDate.SECTION_TYPE_GRANULARITY.day
|
|
});
|
|
}, [] // eslint-disable-line react-hooks/exhaustive-deps
|
|
);
|
|
const [calendarState, dispatch] = React.useReducer(reducerFn, {
|
|
isMonthSwitchingAnimating: false,
|
|
focusedDay: referenceDate,
|
|
currentMonth: utils.startOfMonth(referenceDate),
|
|
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;
|
|
if (utils.isSameMonth(newDateRequested, calendarState.currentMonth)) {
|
|
return;
|
|
}
|
|
handleChangeMonth({
|
|
newMonth: utils.startOfMonth(newDateRequested),
|
|
direction: utils.isAfterDay(newDateRequested, calendarState.currentMonth) ? 'left' : 'right'
|
|
});
|
|
}, [calendarState.currentMonth, handleChangeMonth, utils]);
|
|
const isDateDisabled = (0, _useIsDateDisabled.useIsDateDisabled)({
|
|
shouldDisableDate,
|
|
minDate,
|
|
maxDate,
|
|
disableFuture,
|
|
disablePast,
|
|
timezone
|
|
});
|
|
const onMonthSwitchingAnimationEnd = React.useCallback(() => {
|
|
dispatch({
|
|
type: 'finishMonthSwitchingAnimation'
|
|
});
|
|
}, []);
|
|
const changeFocusedDay = (0, _useEventCallback.default)((newFocusedDate, withoutMonthSwitchingAnimation) => {
|
|
if (!isDateDisabled(newFocusedDate)) {
|
|
dispatch({
|
|
type: 'changeFocusedDay',
|
|
focusedDay: newFocusedDate,
|
|
withoutMonthSwitchingAnimation
|
|
});
|
|
}
|
|
});
|
|
return {
|
|
referenceDate,
|
|
calendarState,
|
|
changeMonth,
|
|
changeFocusedDay,
|
|
isDateDisabled,
|
|
onMonthSwitchingAnimationEnd,
|
|
handleChangeMonth
|
|
};
|
|
};
|
|
exports.useCalendarState = useCalendarState; |