Support independently controlled displayed months in multi-month range calendars #3000
Replies: 3 comments 2 replies
-
|
@Albatrosso cool, thanks for the idea! I think the use case makes sense, e.g. for range pickers where the start and end months are far apart. We should however explore this first, as a small draft PR focused on the core behavior before finalizing the API. The feature touches some assumptions in DayPicker: the rendered months are based on one anchor month + I'd work on a prototype with a minima Some ideas for the API:
function RangePicker({ selected }) {
const [visibleMonths, setVisibleMonths] = useState([
startOfMonth(selected?.from ?? new Date()),
startOfMonth(selected?.to ?? addMonths(new Date(), 1)),
]);
return (
<DayPicker
mode="range"
selected={selected}
visibleMonths={visibleMonths}
onVisibleMonthsChange={setVisibleMonths}
/>
);
}of visibleMonths?: Date[];
defaultVisibleMonths?: Date[];
onVisibleMonthsChange?: (
months: Date[],
context: {
changedIndex?: number;
month?: Date;
source?: "navigation" | "dropdown" | "keyboard";
}
) => void;What do you think? |
Beta Was this translation helpful? Give feedback.
-
|
@gpbl Thanks for the feedback and the suggested API shape! I agree with the approach extending the API with a separate set of props. Extending is better than changing here. I'll prepare a draft PR focused on the core behavior and link it back to this discussion. |
Beta Was this translation helpful? Give feedback.
-
|
@gpbl Hey! Just a gentle ping on #3002 would love your feedback when you get a chance |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Support independently controlled displayed months in multi-month range calendars
Summary
I would like to propose an API for independently controlling the visible months in a multi-month range calendar.
Today,
numberOfMonths={2}renders a contiguous sequence of months derived from a single starting month. This works well for common date pickers, but it is limiting for range-selection UIs where the start and end dates may be far apart and each side of the picker represents a different endpoint.Use case
In a data-heavy reporting/filtering UI, users often select date ranges spanning many months or years.
For example:
When the range picker opens, the most useful layout is:
This lets users immediately see and adjust both endpoints of the selected range.
With the current
numberOfMonths={2}model, the picker can show:or, if anchored near the end:
but not both endpoint months at the same time unless they happen to be adjacent.
Current behavior
numberOfMonths={2}appears to model the visible calendars as:The recent fix for multi-month dropdown navigation is useful: selecting a month in the second calendar can keep that selected month in the second calendar by shifting the common start month.
However, the visible months are still contiguous. There does not seem to be a way to say:
or to control each displayed month independently.
Expected capability
It would be useful if DayPicker supported a controlled multi-month display where each visible month can be independently specified.
Conceptually:
or another API shape along these lines:
The exact API is less important than the capability: the visible months should not have to be adjacent.
Why this matters
This is useful for range filter UIs where:
Without this, applications need to render two separate
DayPickerinstances and coordinate range state, hover preview, endpoint highlighting, disabled days, navigation bounds, and accessibility behavior manually.Backward compatibility
This could be additive. Existing
numberOfMonthsbehavior could remain unchanged.The independent-month behavior could be enabled only when a new controlled prop is provided, such as
visibleMonths,displayMonths, or a similar API.Prior related issues
This is related to the multi-month dropdown behavior discussed in:
That fix improves dropdown behavior for contiguous multi-month calendars. This proposal is about a separate capability: non-contiguous independently controlled displayed months.
Willingness to contribute
If this direction sounds aligned with DayPicker's goals, I would be interested in exploring an implementation and adding tests/examples.
Beta Was this translation helpful? Give feedback.
All reactions