-
Notifications
You must be signed in to change notification settings - Fork 832
/
useCalendarState.tsx
169 lines (153 loc) · 4.61 KB
/
useCalendarState.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as React from 'react';
import { CalendarViewProps } from './CalendarView';
import { SlideDirection } from './SlideTransition';
import { validateDate } from '../../_helpers/date-utils';
import { MaterialUiPickersDate } from '../../typings/date';
import { MuiPickersAdapter, useUtils, useNow } from '../../_shared/hooks/useUtils';
interface CalendarState {
isMonthSwitchingAnimating: boolean;
currentMonth: MaterialUiPickersDate;
focusedDay: MaterialUiPickersDate | null;
slideDirection: SlideDirection;
}
type ReducerAction<TType, TAdditional = {}> = { type: TType } & TAdditional;
interface ChangeMonthPayload {
direction: SlideDirection;
newMonth: MaterialUiPickersDate;
}
export const createCalendarStateReducer = (
reduceAnimations: boolean,
disableSwitchToMonthOnDayFocus: boolean,
utils: MuiPickersAdapter
) => (
state: CalendarState,
action:
| ReducerAction<'finishMonthSwitchingAnimation'>
| ReducerAction<'changeMonth', ChangeMonthPayload>
| ReducerAction<'changeFocusedDay', { focusedDay: MaterialUiPickersDate }>
): CalendarState => {
switch (action.type) {
case 'changeMonth': {
return {
...state,
slideDirection: action.direction,
currentMonth: action.newMonth,
isMonthSwitchingAnimating: !reduceAnimations,
};
}
case 'finishMonthSwitchingAnimation': {
return {
...state,
isMonthSwitchingAnimating: false,
};
}
case 'changeFocusedDay': {
// action.focusedDay = action.focusedDay || utils.date()
const needMonthSwitch =
Boolean(action.focusedDay) &&
!disableSwitchToMonthOnDayFocus &&
!utils.isSameMonth(state.currentMonth, action.focusedDay);
return {
...state,
focusedDay: action.focusedDay,
isMonthSwitchingAnimating: needMonthSwitch && !reduceAnimations,
currentMonth: needMonthSwitch ? utils.startOfMonth(action.focusedDay) : state.currentMonth,
slideDirection: utils.isAfterDay(action.focusedDay, state.currentMonth) ? 'left' : 'right',
};
}
}
};
type CalendarStateInput = Pick<
CalendarViewProps,
| 'disableFuture'
| 'disablePast'
| 'shouldDisableDate'
| 'date'
| 'reduceAnimations'
| 'onMonthChange'
> & {
minDate: MaterialUiPickersDate;
maxDate: MaterialUiPickersDate;
disableSwitchToMonthOnDayFocus?: boolean;
};
export function useCalendarState({
date,
disableFuture,
disablePast,
disableSwitchToMonthOnDayFocus = false,
maxDate,
minDate,
onMonthChange,
reduceAnimations,
shouldDisableDate,
}: CalendarStateInput) {
const now = useNow();
const utils = useUtils();
const dateForMonth = date || now;
const reducerFn = React.useRef(
createCalendarStateReducer(Boolean(reduceAnimations), disableSwitchToMonthOnDayFocus, utils)
).current;
const [calendarState, dispatch] = React.useReducer(reducerFn, {
isMonthSwitchingAnimating: false,
focusedDay: date,
currentMonth: utils.startOfMonth(dateForMonth),
slideDirection: 'left',
});
const handleChangeMonth = React.useCallback(
(payload: ChangeMonthPayload) => {
dispatch({
type: 'changeMonth',
...payload,
});
if (onMonthChange) {
onMonthChange(payload.newMonth);
}
},
[onMonthChange]
);
const changeMonth = React.useCallback(
(newDate: MaterialUiPickersDate) => {
const newDateRequested = 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 = React.useCallback(
(day: MaterialUiPickersDate) =>
validateDate(utils, day, {
disablePast,
disableFuture,
minDate,
maxDate,
shouldDisableDate,
}) !== null,
[disableFuture, disablePast, maxDate, minDate, shouldDisableDate, utils]
);
const onMonthSwitchingAnimationEnd = React.useCallback(() => {
dispatch({ type: 'finishMonthSwitchingAnimation' });
}, []);
const changeFocusedDay = React.useCallback(
(newFocusedDate: MaterialUiPickersDate) => {
if (!isDateDisabled(newFocusedDate)) {
dispatch({ type: 'changeFocusedDay', focusedDay: newFocusedDate });
}
},
[isDateDisabled]
);
return {
calendarState,
changeMonth,
changeFocusedDay,
isDateDisabled,
onMonthSwitchingAnimationEnd,
handleChangeMonth,
};
}