Skip to content

useCalendarState start date doesn't recalculate when updating visibleDuration #7809

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
paddyjoneill opened this issue Feb 21, 2025 · 7 comments · May be fixed by #8177
Open

useCalendarState start date doesn't recalculate when updating visibleDuration #7809

paddyjoneill opened this issue Feb 21, 2025 · 7 comments · May be fixed by #8177
Labels
bug Something isn't working

Comments

@paddyjoneill
Copy link

Provide a general summary of the issue here

I'm creating a mobile calendar and when I scroll up I want to add the previous months. I am doing this by increasing the visibleDuration.

When the visibleMonths is updated the value of the startDate is not recalculated and therefore my calendar never goes before the initial startDate.

🤔 Expected Behavior?

When updating visibleDuration the startDate will be recalculated.

😯 Current Behavior

When updating visibleDuration the startDate is not recalculated.

💁 Possible Solution

I have copied the source code and amended locally and made the change below

Before:

  let [startDate, setStartDate] = useState(() => {
    switch (selectionAlignment) {
      case 'start':
        return alignStart(focusedDate, visibleDuration, locale, minValue, maxValue);
      case 'end':
        return alignEnd(focusedDate, visibleDuration, locale, minValue, maxValue);
      case 'center':
      default:
        return alignCenter(focusedDate, visibleDuration, locale, minValue, maxValue);
    }
  });

After:

  const calculateStartDate = () => {
    switch (selectionAlignment) {
      case 'start':
        return alignStart(
          focusedDate,
          visibleDuration,
          locale,
          minValue,
          maxValue
        )
      case 'end':
        return alignEnd(
          focusedDate,
          visibleDuration,
          locale,
          minValue,
          maxValue
        )
      case 'center':
      default:
        return alignCenter(
          focusedDate,
          visibleDuration,
          locale,
          minValue,
          maxValue
        )
    }
  }

  const [startDate, setStartDate] = useState(() => calculateStartDate())

  const visibleDurationRef = useRef(visibleDuration)

  useEffect(() => {
    if (visibleDuration.months !== visibleDurationRef.current.months) {
      visibleDurationRef.current = visibleDuration
      setStartDate(calculateStartDate())
    }
  }, [visibleDuration])

If you would like me to open a PR let me know and I can also create a CodeSandbox or similar to show.

🔦 Context

No response

🖥️ Steps to Reproduce

to follow

Version

3.6.0

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

MacOS

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

@LFDanLu
Copy link
Member

LFDanLu commented Mar 7, 2025

I must have missed this somehow when looking for any duplicates of #7876, sorry about that! I think what you have is promising, would you like to also try to add locale as mentioned above to the useEffect so that the start date also updates on locale change?

@LFDanLu LFDanLu added the bug Something isn't working label Mar 7, 2025
@paddyjoneill
Copy link
Author

Hi @LFDanLu ,

I'm trying to open a PR for this but am unable to push to the repo.

Can you help?

@LFDanLu
Copy link
Member

LFDanLu commented Apr 30, 2025

Sure, did you make a fork of the repo from which you are trying to open the PR?

@paddyjoneill
Copy link
Author

i just pulled the repo and made a branch.

Is there any documentation on the process?

@LFDanLu
Copy link
Member

LFDanLu commented Apr 30, 2025

We have https://react-spectrum.adobe.com/contribute.html#pull-requests, but feel free to let me know if you have any additional questions!

@paddyjoneill
Copy link
Author

#8177

@LFDanLu

I opened a draft PR, still need to add a test for this though

Should I add it here: packages/@react-aria/calendar/test/useCalendar.test.js

@LFDanLu
Copy link
Member

LFDanLu commented May 1, 2025

That should be fine or you could add a test to Calendar.test.js if you would prefer to test it against the React Aria Components calendar implementation instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants