-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuseCalendarPicker.ts
63 lines (51 loc) · 2.1 KB
/
useCalendarPicker.ts
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
import { useEffect, useState } from 'react';
import { type CalendarPickerProps } from './CalendarPicker';
import { generateSequentialDates, validateSlots } from './utils';
export default function useCalendarPicker({
initialDate,
selectedSlot,
selectableSlot,
numberOfMonths = 1,
}: Omit<CalendarPickerProps, 'modalPosition' | 'calendarPickerRef' | 'onDayClick'>) {
validateSlots(selectedSlot, selectableSlot, initialDate);
const [activeDate, setActiveDate] = useState(new Date());
const displayedMonthsStartDates = generateSequentialDates(activeDate, numberOfMonths);
const activeYear = activeDate.getFullYear();
const activeMonth = activeDate.getMonth();
const firstDayOfMonth = new Date(activeYear, activeMonth, 1);
const lastDayOfMonth = new Date(activeYear, activeMonth + 1, 0);
const canGoToPreviousMonth =
selectableSlot === undefined || selectableSlot.start === null
? true
: selectableSlot.start < firstDayOfMonth;
const canGoToNextMonth =
selectableSlot === undefined || selectableSlot.end === null
? true
: selectableSlot.end > lastDayOfMonth;
const showNavigationBtn = canGoToPreviousMonth || canGoToNextMonth;
const handleGoToPreviousMonth = () => {
if (canGoToPreviousMonth) {
const previousActiveMonth = activeMonth === 0 ? 11 : activeMonth - 1;
const previousActiveYear = activeMonth === 0 ? activeYear - 1 : activeYear;
setActiveDate(new Date(previousActiveYear, previousActiveMonth, 1));
}
};
const handleGoToNextMonth = () => {
if (canGoToNextMonth) {
const nextActiveMonth = activeMonth === 11 ? 0 : activeMonth + 1;
const nextActiveYear = activeMonth === 11 ? activeYear + 1 : activeYear;
setActiveDate(new Date(nextActiveYear, nextActiveMonth, 1));
}
};
useEffect(() => {
setActiveDate(initialDate ?? selectedSlot?.start ?? selectableSlot?.start ?? new Date());
}, [initialDate, selectedSlot, selectableSlot]);
return {
displayedMonthsStartDates,
showNavigationBtn,
canGoToPreviousMonth,
canGoToNextMonth,
handleGoToPreviousMonth,
handleGoToNextMonth,
};
}