Skip to content

Commit

Permalink
fix: update the displayed month only if start/end month change (#2358)
Browse files Browse the repository at this point in the history
* fix: update the displayed month only if start/end month change
* fix missing effect for month prop
  • Loading branch information
gpbl authored Aug 7, 2024
1 parent 8642bb3 commit d9fec52
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions src/useCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,29 @@ export function useCalendar(
>,
dateLib: DateLib
): Calendar {
const {
fromYear,
toYear,
startMonth,
endMonth,
today,
numberOfMonths,
month,
defaultMonth
} = props;
const [navStart, navEnd] = getNavMonths(props, dateLib);

const { startOfMonth, endOfMonth } = dateLib;

const initialDisplayMonth = getInitialMonth(props, dateLib);
const initialMonth = getInitialMonth(props, dateLib);

// The first month displayed in the calendar
const [firstMonth, setFirstMonth] = useState(initialDisplayMonth);
const [firstMonth, setFirstMonth] = useState(initialMonth);

// Update the displayed month if `month` changes
useEffect(() => {
const initialDisplayMonth = getInitialMonth(
{
fromYear,
toYear,
startMonth,
endMonth,
month,
defaultMonth,
today,
numberOfMonths
},
dateLib
);
const initialDisplayMonth = getInitialMonth(props, dateLib);
setFirstMonth(initialDisplayMonth);
}, [
dateLib,
defaultMonth,
endMonth,
fromYear,
month,
numberOfMonths,
startMonth,
toYear,
today
]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.month]);

// Update the displayed month if start/end month changes
useEffect(() => {
// TOFIX: this effect should do nothing if the current firstMonth is between
// startMonth and endMonth
const initialDisplayMonth = getInitialMonth(props, dateLib);
setFirstMonth(initialDisplayMonth);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.startMonth, props.endMonth]);

/** The months displayed in the calendar. */
const displayMonths = getDisplayMonths(firstMonth, navEnd, props, dateLib);
Expand Down

0 comments on commit d9fec52

Please sign in to comment.