Kiarash-Z / react-modern-calendar-datepicker

A modern, beautiful, customizable date picker for React
https://kiarash-z.github.io/react-modern-calendar-datepicker/
MIT License
1.02k stars 214 forks source link

Navigate to Today #361

Open AkbarBakhshi opened 12 months ago

AkbarBakhshi commented 12 months ago

Hello and thank you for this great library.

For the Calendar component, in the RenderFooter function, I am adding a button that will set the selected day to the current day.

Screenshot 2023-09-29 at 10 15 59 PM

If I move to another month and then click on this button, the calendar does not navigate back to the current month. The selected day will correctly change to today, but it is not visually obvious to the user if they moved to another month/year.

Is there a way to achieve that? Once "Today" is clicked, the calendar view also navigates back to the current date, if we have moved to a different month.

Thank you.

Cloner311 commented 11 months ago

I have such a problem

2pehr commented 11 months ago

Thank you for your amazing work. I am facing the same problem. However as a solution to this, I tried to re-render the component whenever a 'reset' button is clicked, it errors on "reading removeEventListner" and still couldn't've figure it out.

alexferreira1796 commented 3 months ago

I managed to make it work in this solution

I added a resetCounter state that is updated whenever the "Today" button is clicked. This counter is used as a key for calendar components, forcing render

const [resetCounter, setResetCounter] = useState<number>(0);

const onHandleOption = () => {
    setResetCounter((prevCounter) => prevCounter + 1);
  };

<Calendar
       key={`calendar1-${resetCounter}`}
       value={selectedDayRange1}
       onChange={setSelectedDayRange1}
       shouldHighlightWeekends
       locale={monthsLocale}
 />