Skip to content

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

@paddyjoneill

Description

@paddyjoneill

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

Activity

LFDanLu

LFDanLu commented on Mar 7, 2025

@LFDanLu
Member

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?

added theissue type on Mar 7, 2025
paddyjoneill

paddyjoneill commented on Apr 30, 2025

@paddyjoneill
Author

Hi @LFDanLu ,

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

Can you help?

LFDanLu

LFDanLu commented on Apr 30, 2025

@LFDanLu
Member

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

paddyjoneill

paddyjoneill commented on Apr 30, 2025

@paddyjoneill
Author

i just pulled the repo and made a branch.

Is there any documentation on the process?

LFDanLu

LFDanLu commented on Apr 30, 2025

@LFDanLu
Member

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

paddyjoneill commented on May 1, 2025

@paddyjoneill
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

LFDanLu commented on May 1, 2025

@LFDanLu
Member

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @LFDanLu@paddyjoneill

      Issue actions

        useCalendarState start date doesn't recalculate when updating visibleDuration · Issue #7809 · adobe/react-spectrum