Skip to content

Commit 3d1f966

Browse files
Merge pull request #6103 from Hacker0x01/fix/issue-5275-multi-month-range-selection
fix: prevent month view jump when selecting from second calendar in range mode
2 parents 793ed81 + ff5e4b4 commit 3d1f966

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,16 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
357357
) {
358358
this.setState({ monthSelectedIn: 0 });
359359
}
360-
if (this.props.selectsRange && this.state.monthSelectedIn !== 0) {
360+
// Reset monthSelectedIn when calendar opens for range selection
361+
// This ensures startDate is displayed as the first month when reopening
362+
// (Fix for #5939), but we don't reset during active selection to avoid
363+
// the view jumping when clicking dates in the second calendar (Fix for #5275)
364+
if (
365+
this.props.selectsRange &&
366+
prevState.open === false &&
367+
this.state.open === true &&
368+
this.state.monthSelectedIn !== 0
369+
) {
361370
this.setState({ monthSelectedIn: 0 });
362371
}
363372
if (prevProps.highlightDates !== this.props.highlightDates) {

src/test/datepicker_test.test.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,37 +2922,67 @@ describe("DatePicker", () => {
29222922
expect(instance!.state.monthSelectedIn).toEqual(0);
29232923
});
29242924

2925-
it("should set monthSelectedIn to 0 when selectsRange is true", () => {
2925+
it("should set monthSelectedIn to 0 when calendar opens with selectsRange", () => {
29262926
let instance: DatePicker | null = null;
2927-
const { rerender } = render(
2927+
const { container } = render(
29282928
<DatePicker
29292929
ref={(node) => {
29302930
instance = node;
29312931
}}
29322932
monthsShown={2}
2933-
inline
2933+
selectsRange
29342934
/>,
29352935
);
29362936
expect(instance).toBeTruthy();
2937+
2938+
// Manually set monthSelectedIn to 1 (simulating previous user interaction)
29372939
act(() => {
29382940
(
29392941
instance!.state as unknown as { monthSelectedIn: number }
29402942
).monthSelectedIn = 1;
29412943
});
29422944
expect(instance!.state.monthSelectedIn).toEqual(1);
29432945

2944-
rerender(
2946+
// Open the calendar by clicking the input
2947+
const input = container.querySelector("input");
2948+
expect(input).not.toBeNull();
2949+
fireEvent.click(input!);
2950+
2951+
// monthSelectedIn should be reset to 0 when the calendar opens
2952+
expect(instance!.state.monthSelectedIn).toEqual(0);
2953+
});
2954+
2955+
it("should NOT reset monthSelectedIn when selecting a date from second calendar with selectsRange", () => {
2956+
let instance: DatePicker | null = null;
2957+
const onChange = jest.fn();
2958+
const { container } = render(
29452959
<DatePicker
29462960
ref={(node) => {
29472961
instance = node;
29482962
}}
29492963
monthsShown={2}
2950-
inline
29512964
selectsRange
2965+
inline
2966+
onChange={onChange}
29522967
/>,
29532968
);
2969+
expect(instance).toBeTruthy();
29542970

2955-
expect(instance!.state.monthSelectedIn).toEqual(0);
2971+
// Find a day in the second month container
2972+
const secondMonthContainer = container.querySelectorAll(
2973+
".react-datepicker__month-container",
2974+
)[1];
2975+
expect(secondMonthContainer).toBeTruthy();
2976+
const secondMonthDays = secondMonthContainer?.querySelectorAll(
2977+
".react-datepicker__day:not(.react-datepicker__day--outside-month)",
2978+
);
2979+
expect(secondMonthDays?.length).toBeGreaterThan(0);
2980+
2981+
// Click a day in the second month
2982+
fireEvent.click(secondMonthDays![10]!);
2983+
2984+
// monthSelectedIn should be 1 (second calendar), not reset to 0
2985+
expect(instance!.state.monthSelectedIn).toEqual(1);
29562986
});
29572987

29582988
it("should disable non-jumping if prop focusSelectedMonth is true", () => {

0 commit comments

Comments
 (0)