react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

feat(RangeCalendar): Added anchorDate functionality

Open prachiaroraa opened this issue 2 years ago • 16 comments

Description

Added anchor date functionality where user can pass anchorDate prop to the RangeCalendar component and it will fix that particular date for date range selection.

Usage

<RangeCalendar 
      aria-label='Interval selection'
      value={dateRange}
      maxValue={parseDate(format(new Date(), 'yyyy-MM-dd'))}
      anchorDate={parseDate(format(new Date(), 'yyyy-MM-dd'))}
      onChange={setDateRange}
/>

react-calendar-anchorDate

prachiaroraa avatar Mar 20 '23 08:03 prachiaroraa

GET_BUILD

snowystinger avatar Mar 21 '23 00:03 snowystinger

Thanks for the PR! would you mind including an issue as well, that way you can describe the usecase you are solving for?

I think there is something here we could use. However, I don't think it's going to be the anchorDate prop. Stately already exposes a setAnchorDate, so it'd be strange if we had both a prop controlling that value and a function to update it. Which one would win?

Maybe it'd be good to include some examples in our storybook? I tried it out locally, and either I'm using it wrong in our Spectrum components, or this isn't working as intended. https://github.com/adobe/react-spectrum/blob/main/packages/%40react-spectrum/datepicker/stories/DateRangePicker.stories.tsx

snowystinger avatar Mar 21 '23 00:03 snowystinger

Thanks for the PR! would you mind including an issue as well, that way you can describe the usecase you are solving for?

I think there is something here we could use. However, I don't think it's going to be the anchorDate prop. Stately already exposes a setAnchorDate, so it'd be strange if we had both a prop controlling that value and a function to update it. Which one would win?

Maybe it'd be good to include some examples in our storybook? I tried it out locally, and either I'm using it wrong in our Spectrum components, or this isn't working as intended. https://github.com/adobe/react-spectrum/blob/main/packages/%40react-spectrum/datepicker/stories/DateRangePicker.stories.tsx

Hi @snowystinger, To handle the scenario where we would want either the start date or end date of the dateRange to be fixed, we've added the support for anchorDate prop.

For instance, consider a scenario where we want to show data for date ranges that are in the form of "Last X days" (Last 7 days, Last 30 days, Last 90 days...) For this, we want the endDate to be set to Today. This scenario can now be handled, if we pass anchorDate as Today to RangeCalendar component.

<RangeCalendar 
      aria-label='Interval selection'
      anchorDate={parseDate(format(new Date(), 'yyyy-MM-dd'))}
      onChange={setDateRange}
/>

prachiaroraa avatar Mar 27 '23 08:03 prachiaroraa

Hi @snowystinger, @devongovett Is there any update on this?

prachiaroraa avatar Mar 29 '23 09:03 prachiaroraa

Hey, sorry, we're in the middle of making a release and have not had time to look at this. Apologies for the delay, it'll be a few more days probably.

snowystinger avatar Mar 29 '23 20:03 snowystinger

Hey, sorry, we're in the middle of making a release and have not had time to look at this. Apologies for the delay, it'll be a few more days probably.

Hi @snowystinger @devongovett Can you please assign someone else to review these changes? My team is working on a strict timeline project, and we're currently blocked on this use-case.

prachiaroraa avatar Apr 04 '23 06:04 prachiaroraa

Hey, there unfortunately isn't anyone else right now. If you are working on a strict timeline, I advise that you make use of a tool such as patch-package in order to use the functionality now. Then when we support something that can meet your needs here, you can remove the patch.

snowystinger avatar Apr 05 '23 00:04 snowystinger

You could also probably implement this in your own component using the React Stately hooks by calling state.setAnchorDate from there in the meantime.

devongovett avatar Apr 05 '23 00:04 devongovett

Hi @snowystinger @devongovett Can you update on this feature, please?

prachiaroraa avatar Jun 19 '23 08:06 prachiaroraa

GET_BUILD

snowystinger avatar Jun 19 '23 15:06 snowystinger

## API Changes

unknown top level export { type: 'identifier', name: 'Column' } unknown top level export { type: 'identifier', name: 'Column' } unknown type { type: 'link' } unknown type { type: 'link' } unknown type { type: 'link' } unknown type { type: 'link' } unknown type { type: 'link' } unknown type { type: 'link' }

@react-aria/calendar

AriaCalendarProps

 AriaCalendarProps<T extends DateValue> {
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
 }
 

AriaRangeCalendarProps

 AriaRangeCalendarProps<T extends DateValue> {
   allowsNonContiguousRanges?: boolean
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
 }
 

CalendarProps

 CalendarProps<T extends DateValue> {
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
 }
 

RangeCalendarProps

 RangeCalendarProps<T extends DateValue> {
   allowsNonContiguousRanges?: boolean
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
 }
 

@react-spectrum/calendar

Calendar

changed by:

  • Calendar
 SpectrumCalendarProps<T extends DateValue> {
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
   visibleMonths?: number = 1
 }
 

it changed:

  • AnyCalendarDate
  • Calendar
  • AnyDateTime

RangeCalendar

 SpectrumRangeCalendarProps<T extends DateValue> {
   allowsNonContiguousRanges?: boolean
+  anchorDate?: CalendarDate
   autoFocus?: boolean = false
   defaultFocusedValue?: DateValue
   errorMessage?: ReactNode
   focusedValue?: DateValue
   isDisabled?: boolean = false
   isReadOnly?: boolean = false
   maxValue?: DateValue
   minValue?: DateValue
   onFocusChange?: (CalendarDate) => void
   pageBehavior?: PageBehavior = visible
   validationState?: ValidationState
   visibleMonths?: number = 1
 }
 

rspbot avatar Jun 19 '23 15:06 rspbot

Hey there, we're still a bit confused by the use case for this. Would you be willing to share some other popular libraries which implement this feature?

Some concerns we have are:

  • Focused date isn't the anchorDate, this is confusing because it's not apparent that one date in the range is already selected.
  • The anchorDate and range is invisible but becomes suddenly selected when the user selects their first date. It should show up from first mouse over. This is also confusing for the same reason as the first one, it doesn't look like part of the range is already determined.
  • Why can one date be pinned, but users can select in either direction? What does it mean to have a date anchor? This is where other libraries as examples will help.

snowystinger avatar Jun 19 '23 21:06 snowystinger

related: https://github.com/adobe/react-spectrum/issues/4723

snowystinger avatar Jun 28 '23 16:06 snowystinger

Hi @snowystinger, To answer your question related to use case of this functionality, We had a scenario where we wanted user to be able to select a date range like between November 1 to November 9, but we also wanted to allow the user to just select the start date of the date range and end date would be fixed to today, so the date selection would look like Since Nov 8. To handle this scenario, we had to keep today fixed, which means we had to set today as our anchorDate.

We have been using this feature in our application using patch file from last 10 months, and have seen no issues. In case, you see any issues that these changes can cause, then please highlight.

range-calendar

prachiaroraa avatar Nov 17 '23 03:11 prachiaroraa

It's very cool feature that I'd love to see in the lib.

dnzg avatar Feb 06 '25 18:02 dnzg