Skip to content

Commit

Permalink
Merge pull request #46 from oleksii00ishchenko/master
Browse files Browse the repository at this point in the history
Fix: switch between dates in month mode
  • Loading branch information
vasylnahuliak authored Dec 30, 2022
2 parents 79cb2d9 + 3f63c93 commit 59d3883
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/components/CalendarContainer/CalendarContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,14 @@ function _CalendarContainer<T>(
(direction === 'LEFT' && !theme.isRTL) ||
(direction === 'RIGHT' && theme.isRTL)
) {
setTargetDate(targetDate.add(modeToNum(mode, targetDate), 'day'));
setTargetDate(
targetDate.add(modeToNum(mode, targetDate, direction), 'day')
);
calendarRef.current?.onSwipeLeft();
} else {
setTargetDate(targetDate.add(-modeToNum(mode, targetDate), 'day'));
setTargetDate(
targetDate.add(-modeToNum(mode, targetDate, direction), 'day')
);
calendarRef.current?.onSwipeRight();
}
},
Expand Down
16 changes: 15 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,28 @@ export function todayInMinutes() {
return today.diff(dayjs().startOf('day'), 'minute');
}

export function modeToNum(mode: Mode, current?: dayjs.Dayjs | Date): number {
export function modeToNum(
mode: Mode,
current?: dayjs.Dayjs | Date,
swipeDirection?: string
): number {
if (mode === 'dayGridMonth') {
if (!current) {
throw new Error('You must specify current date if mode is month');
}
if (current instanceof Date) {
current = dayjs(current);
}
if (swipeDirection) {
const currentDate = dayjs.utc(current).date();
const startOfMonth = dayjs.utc(current).startOf('month').date();
const endOfMonth = current.endOf('month').date();
if (currentDate === startOfMonth && swipeDirection === 'RIGHT') {
return current.set('month', current.month() - 1).daysInMonth();
} else if (currentDate === endOfMonth && swipeDirection === 'LEFT') {
return current.set('month', current.month() + 1).daysInMonth();
}
}
return current.daysInMonth();
}
switch (mode) {
Expand Down

0 comments on commit 59d3883

Please sign in to comment.