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

DayPicker resets to current month after date selection #2389

Closed
DarXnake opened this issue Aug 19, 2024 · 7 comments · Fixed by #2429
Closed

DayPicker resets to current month after date selection #2389

DarXnake opened this issue Aug 19, 2024 · 7 comments · Fixed by #2429
Assignees

Comments

@DarXnake
Copy link

Hi, and thanks for reviewing this bug report! :)

Code

import { useState } from "react";
import { DayPicker, DateRange } from "react-day-picker";
import "react-day-picker/style.css";

export default function App() {
  const [selectedPeriod, setSelectedPeriod] = useState<DateRange | undefined>();

  return (
    <DayPicker
      mode="range"
      startMonth={new Date("2024-07-01")}
      endMonth={new Date("2025-07-31")}
      onSelect={setSelectedPeriod}
    />
  );
}

Expected Behavior

When a date is selected, the DayPicker should remain on the month of the selected date.

Actual Behavior

In any selection mode (single, range, or multiple), if you set a startMonth or endMonth and trigger an onSelect that updates a React state, the DayPicker display will reset to the current month instead of remaining on the month where you selected a day.

Removing either startMonth and endMonth or onSelect resolves this issue.

Exemple

CodeSandbox

@hwennnn
Copy link

hwennnn commented Aug 20, 2024

I have encountered this issue as well!

@hwennnn
Copy link

hwennnn commented Aug 20, 2024

I have downgraded the library to v9.0.6 for now, and it works. It seems like #2343 or #2358 caused this issue.

@lariane-guerra
Copy link

It didn't work for me to revert the version. Could you take a look at my issue to see if you can help me?

#2402

@rbansal27
Copy link

Reverting the version didn't help.

The issue that I am facing is, I have the date picker placed inside a popover and on selecting the date from month other than the current, popover gets closed and reopening it, opens the date picker with the current date in focus.
Ideally the selected date / month should be in view.

@Pilaton
Copy link

Pilaton commented Aug 27, 2024

You can make an additional state for month navigation

const [currentMonth, setCurrentMonth] = useState<Date | undefined>(new Date());
  
<DayPicker
  ...
  month={currentMonth}
  onMonthChange={setCurrentMonth}
/>

@tresorama
Copy link

tresorama commented Sep 29, 2024

As alternative It's possible to use defaultMonth instead of month, but it's required to use a React state that rerender the whole Calendar component on date change.
In this way it's possible to avoid having a separate state for the month.

const [selectedDate, setSelectedDate] = useState(new Date());

<Calendar
  mode="single"
  selected={selectedDate}
  onSelect={setSelectedDate}
  defaultMonth={selectedDate} // NOTE ME
/>

So, if you are using react-hook-form you must use the <Controller /> API and not the register API.

@hushanidini
Copy link

const [currentMonth, setCurrentMonth] = useState<Date | undefined>(selectedDate || new Date());
<Calendar
    mode="single"
      selected={date}
      month={currentMonth} 
      onMonthChange={setCurrentMonth} 
      onSelect={(dayDate: any) => {
        setDate(dayDate);
        if (setSelectedDate) {
          setSelectedDate(dayDate);
        }
      }}
/> pls try this

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 a pull request may close this issue.

8 participants