Skip to content

Commit

Permalink
fix(CalendarMonth): unfocus date on year change
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Oct 9, 2023
1 parent 8107be7 commit 0de2966
Showing 1 changed file with 3 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,8 @@ export const CalendarMonth = ({

const changeYear = (newYear: number) => changeMonth(focusedDate.getMonth(), newYear);

const changeMonth = (newMonth: number, newYear?: number) => {
const year = newYear ?? focusedDate.getFullYear();
const daysInNewMonth = new Date(year, (newMonth + 1) % 12, 0).getDate(); // Setting day 0 of the next month returns the last day of current month
const desiredDay = initialDate.getDate();
const day = desiredDay <= daysInNewMonth ? desiredDay : daysInNewMonth;

return new Date(year, newMonth, day);
};
const changeMonth = (newMonth: number, newYear?: number) =>
new Date(newYear ?? focusedDate.getFullYear(), newMonth, 1);

const addMonth = (toAdd: -1 | 1) => {
let newMonth = focusedDate.getMonth() + toAdd;
Expand Down Expand Up @@ -332,6 +326,7 @@ export const CalendarMonth = ({
setFocusedDate(newDate);
setHoveredDate(newDate);
setShouldFocus(false);
focusRef.current.blur(); // will unfocus a date when changing year via up/down arrows
onMonthChange(ev, newDate);
}}
/>
Expand Down

0 comments on commit 0de2966

Please sign in to comment.