Fix: trigger month change when pressing the arrows in the calendar
Issue #195
Trigger onMonthChange callback when user change month from the arrows in the calendar display
Thank you @Maxime-BARRY-SQLI, we need this fix!
Hello @farhoudshapouran, are you ok to merge this PR ?
Thanks for the PR! The month navigation detection is working well.
I was wondering if we could also trigger the onYearChange callback when navigating between months that span different years?
For example, when going from December 2023 to January 2024, or from January 2024 to December 2023.
Currently, the onChangeMonth function only calls onMonthChange, but it might be useful to also check if the year has changed and trigger onYearChange accordingly.
Here's a suggestion:
const onChangeMonth = useCallback(
(value: number) => {
const currentDate = stateRef.current.currentDate;
const newDate = dayjs(currentDate).add(value, 'month');
const currentYear = dayjs(currentDate).year();
const newYear = dayjs(newDate).year();
if (currentYear !== newYear) {
onYearChange(newYear);
}
onMonthChange(newDate.month());
dispatch({
type: CalendarActionKind.CHANGE_CURRENT_DATE,
payload: dayjs(newDate),
});
},
[stateRef, dispatch, onMonthChange, onYearChange]
);
This would ensure that both onMonthChange and onYearChange are properly triggered when navigating between months that span different years. What do you think? @Maxime-BARRY-SQLI
Hello @SWARVY, thanks for your suggestion. I've pushed a new commit that allows onSelectYear to be triggered when the user switches from December to January (or vice versa).
If you think there are any new things I can improve, please let me know. 👍
@farhoudshapouran this feels like a necessary adjustment of the onMonthChange function. Can you please review and merge this PR?
+1, this is an important bug fix. Though you can always use a patch meanwhile which is what I did