Skip to content

Fix: trigger month change when pressing the arrows in the calendar #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Maxime-BARRY-SQLI
Copy link

Issue #195
Trigger onMonthChange callback when user change month from the arrows in the calendar display

@florismettey
Copy link

Thank you @Maxime-BARRY-SQLI, we need this fix!

@florismettey
Copy link

Hello @farhoudshapouran, are you ok to merge this PR ?

@SWARVY
Copy link

SWARVY commented Aug 7, 2025

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

@Maxime-BARRY-SQLI
Copy link
Author

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. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants