Skip to content

Commit d9fec52

Browse files
authored
fix: update the displayed month only if start/end month change (#2358)
* fix: update the displayed month only if start/end month change * fix missing effect for month prop
1 parent 8642bb3 commit d9fec52

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

src/useCalendar.ts

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,51 +88,29 @@ export function useCalendar(
8888
>,
8989
dateLib: DateLib
9090
): Calendar {
91-
const {
92-
fromYear,
93-
toYear,
94-
startMonth,
95-
endMonth,
96-
today,
97-
numberOfMonths,
98-
month,
99-
defaultMonth
100-
} = props;
10191
const [navStart, navEnd] = getNavMonths(props, dateLib);
10292

10393
const { startOfMonth, endOfMonth } = dateLib;
10494

105-
const initialDisplayMonth = getInitialMonth(props, dateLib);
95+
const initialMonth = getInitialMonth(props, dateLib);
10696

107-
// The first month displayed in the calendar
108-
const [firstMonth, setFirstMonth] = useState(initialDisplayMonth);
97+
const [firstMonth, setFirstMonth] = useState(initialMonth);
10998

99+
// Update the displayed month if `month` changes
110100
useEffect(() => {
111-
const initialDisplayMonth = getInitialMonth(
112-
{
113-
fromYear,
114-
toYear,
115-
startMonth,
116-
endMonth,
117-
month,
118-
defaultMonth,
119-
today,
120-
numberOfMonths
121-
},
122-
dateLib
123-
);
101+
const initialDisplayMonth = getInitialMonth(props, dateLib);
124102
setFirstMonth(initialDisplayMonth);
125-
}, [
126-
dateLib,
127-
defaultMonth,
128-
endMonth,
129-
fromYear,
130-
month,
131-
numberOfMonths,
132-
startMonth,
133-
toYear,
134-
today
135-
]);
103+
// eslint-disable-next-line react-hooks/exhaustive-deps
104+
}, [props.month]);
105+
106+
// Update the displayed month if start/end month changes
107+
useEffect(() => {
108+
// TOFIX: this effect should do nothing if the current firstMonth is between
109+
// startMonth and endMonth
110+
const initialDisplayMonth = getInitialMonth(props, dateLib);
111+
setFirstMonth(initialDisplayMonth);
112+
// eslint-disable-next-line react-hooks/exhaustive-deps
113+
}, [props.startMonth, props.endMonth]);
136114

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

0 commit comments

Comments
 (0)