Skip to content

Commit 5634c50

Browse files
Merge pull request #6111 from Hacker0x01/fix/issue-3367-calendar-view-update
fix: Update calendar view when selected/startDate props change programmatically
2 parents 2ddeacb + 393cd91 commit 5634c50

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,15 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
345345
prevProps: DatePickerProps,
346346
prevState: DatePickerState,
347347
): void {
348+
// Update preSelection when selected/startDate prop changes to a different month/year.
349+
// This ensures the calendar view updates when dates are programmatically set
350+
// (e.g., via "Today" or "This Week" buttons). (Fix for #3367)
348351
if (
349-
prevProps.inline &&
352+
this.props.selectsRange &&
353+
hasPreSelectionChanged(prevProps.startDate, this.props.startDate)
354+
) {
355+
this.setPreSelection(this.props.startDate);
356+
} else if (
350357
hasPreSelectionChanged(prevProps.selected, this.props.selected)
351358
) {
352359
this.setPreSelection(this.props.selected);

src/test/datepicker_test.test.tsx

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,56 @@ describe("DatePicker", () => {
28912891
formatDate(future, "yyyy-MM-dd"),
28922892
);
28932893
});
2894+
it("should update calendar view when selected prop changes to a different month (issue #3367)", () => {
2895+
// This test verifies that when a user programmatically sets selected date
2896+
// (e.g., via a "Today" button), the calendar view updates to show
2897+
// the month containing the new date, even if user was viewing a different month.
2898+
const initialDate = new Date("2024-01-15");
2899+
let instance: DatePicker | null = null;
2900+
2901+
const { container, rerender } = render(
2902+
<DatePicker
2903+
ref={(node) => {
2904+
instance = node;
2905+
}}
2906+
selected={initialDate}
2907+
onChange={() => {}}
2908+
/>,
2909+
);
2910+
2911+
expect(instance).toBeTruthy();
2912+
2913+
// Open the calendar
2914+
const input = safeQuerySelector(container, "input");
2915+
fireEvent.click(input);
2916+
2917+
// Verify initial preSelection is set to January 2024
2918+
expect(instance!.state.preSelection?.getMonth()).toBe(0); // January
2919+
expect(instance!.state.preSelection?.getFullYear()).toBe(2024);
2920+
2921+
// Close calendar
2922+
fireEvent.click(input);
2923+
2924+
// Simulate programmatic update to a different month (e.g., user clicks "Jump to March" button)
2925+
const newDate = new Date("2024-03-10");
2926+
2927+
rerender(
2928+
<DatePicker
2929+
ref={(node) => {
2930+
instance = node;
2931+
}}
2932+
selected={newDate}
2933+
onChange={() => {}}
2934+
/>,
2935+
);
2936+
2937+
// Open the calendar again
2938+
fireEvent.click(input);
2939+
2940+
// The calendar should now show March 2024, not January 2024
2941+
expect(instance!.state.preSelection?.getMonth()).toBe(2); // March
2942+
expect(instance!.state.preSelection?.getFullYear()).toBe(2024);
2943+
});
28942944
it("should not set open state when focusing on the date input and the preventOpenOnFocus prop is set", () => {
28952945
const { container } = render(<DatePicker preventOpenOnFocus />);
28962946
const input = safeQuerySelector(container, "input");
@@ -3855,6 +3905,63 @@ describe("DatePicker", () => {
38553905
const input = safeQuerySelector<HTMLInputElement>(container, "input");
38563906
expect(input.value).toBe("2025/01/01 to 2025/01/03");
38573907
});
3908+
3909+
it("should update calendar view when startDate prop changes to a different month (issue #3367)", () => {
3910+
// This test verifies that when a user programmatically sets startDate
3911+
// (e.g., via a "This Week" button), the calendar view updates to show
3912+
// the month containing the new startDate, even if user was viewing a different month.
3913+
const initialStartDate = new Date("2024-01-15");
3914+
const initialEndDate = new Date("2024-01-20");
3915+
let instance: DatePicker | null = null;
3916+
3917+
const { container, rerender } = render(
3918+
<DatePicker
3919+
ref={(node) => {
3920+
instance = node;
3921+
}}
3922+
selectsRange
3923+
startDate={initialStartDate}
3924+
endDate={initialEndDate}
3925+
onChange={() => {}}
3926+
/>,
3927+
);
3928+
3929+
expect(instance).toBeTruthy();
3930+
3931+
// Open the calendar
3932+
const input = safeQuerySelector(container, "input");
3933+
fireEvent.click(input);
3934+
3935+
// Verify initial preSelection is set to January 2024
3936+
expect(instance!.state.preSelection?.getMonth()).toBe(0); // January
3937+
expect(instance!.state.preSelection?.getFullYear()).toBe(2024);
3938+
3939+
// Close calendar
3940+
fireEvent.click(input);
3941+
3942+
// Simulate programmatic update to a different month (e.g., user clicks "This Week" button in March)
3943+
const newStartDate = new Date("2024-03-10");
3944+
const newEndDate = new Date("2024-03-15");
3945+
3946+
rerender(
3947+
<DatePicker
3948+
ref={(node) => {
3949+
instance = node;
3950+
}}
3951+
selectsRange
3952+
startDate={newStartDate}
3953+
endDate={newEndDate}
3954+
onChange={() => {}}
3955+
/>,
3956+
);
3957+
3958+
// Open the calendar again
3959+
fireEvent.click(input);
3960+
3961+
// The calendar should now show March 2024, not January 2024
3962+
expect(instance!.state.preSelection?.getMonth()).toBe(2); // March
3963+
expect(instance!.state.preSelection?.getFullYear()).toBe(2024);
3964+
});
38583965
});
38593966

38603967
describe("duplicate dates when multiple months", () => {

0 commit comments

Comments
 (0)