Skip to content
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

selected value handling in react-date-picker causes getMonth() errors #9

Open
delmac-dev opened this issue Jan 29, 2025 · 2 comments
Open

Comments

@delmac-dev
Copy link

Bug: selected value handling in react-date-picker causes getMonth() errors

Description:

When using react-date-picker in modes like range or multiple, selecting a month by tapping on the month caption causes an error. This happens because the selected value can be a string, an object (from and to in range mode), or an array (multiple dates). Directly treating selected as a Date leads to errors when trying to call getMonth().

Steps to Reproduce:

  1. Use react-date-picker in range or multiple mode.
  2. Tap on the month caption to select a month.
  3. Select any month.
  4. The error TypeError: selected.getMonth is not a function occurs.

Expected Behavior:

getMonth() should only be called on valid Date objects, with proper checks for selected being a string, object, or array.

Actual Behavior:

TypeError: selected.getMonth is not a function occurs when selected is not a Date.

Suggested Solution:

Implement proper type-checking for the selected value based on its type (string, object, array) before attempting to call getMonth().

@xpek20
Copy link

xpek20 commented Jan 30, 2025

Same error here

@alexalannunes
Copy link

alexalannunes commented Feb 11, 2025

My solution

setNavView("days");
if (mode === "range") {
  const selectedRange = selected as unknown as { from: Date; to: Date };
  // if mode is range, use `selectedRange.from`
  goToMonth(
    new Date(displayYears.from + i, selectedRange.from?.getMonth() ?? new Date().getMonth())
  );
  return;
}
goToMonth(new Date(displayYears.from + i, (selected as Date | undefined)?.getMonth() ?? 0));

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

No branches or pull requests

3 participants