From 9c4bba5c07367d2312136dec17db64384638daec Mon Sep 17 00:00:00 2001 From: balajis-qb Date: Tue, 8 Sep 2026 15:39:47 +0530 Subject: [PATCH] fix: honor calendarStartDay when navigating with the End key The Home keyboard handler passed `locale` and `calendarStartDay` to `getStartOfWeek`, but the End handler called `getEndOfWeek(date)` with no configuration. With a non-Sunday week start (e.g. `calendarStartDay={1}`), End moved focus to Saturday instead of the configured last day (Sunday). `getEndOfWeek` now accepts optional `locale` and `calendarStartDay` arguments, mirroring `getStartOfWeek`, and the End handler forwards both so Home and End compute symmetric week boundaries. Fixes #6326 --- src/date_utils.ts | 16 ++++++++++++-- src/index.tsx | 2 +- src/test/date_utils_test.test.ts | 36 +++++++++++++++++++++++++++++++ src/test/datepicker_test.test.tsx | 16 ++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/date_utils.ts b/src/date_utils.ts index 7fba0d960..cf4384ad3 100644 --- a/src/date_utils.ts +++ b/src/date_utils.ts @@ -681,10 +681,22 @@ export function getEndOfDay(date: Date): Date { * Gets the end of the week for a given date. * * @param date - The date. + * @param locale - The locale. + * @param calendarStartDay - The day the calendar starts on. * @returns - The end of the week. */ -export function getEndOfWeek(date: Date): Date { - return endOfWeek(date); +export function getEndOfWeek( + date: Date, + locale?: Locale, + calendarStartDay?: Day, +): Date { + const localeObj = locale + ? getLocaleObject(locale) + : getLocaleObject(getDefaultLocale()); + return endOfWeek(date, { + locale: localeObj, + weekStartsOn: calendarStartDay, + }); } /** diff --git a/src/index.tsx b/src/index.tsx index 05fde7b81..f8c4136d3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1566,7 +1566,7 @@ export class DatePicker extends Component { newCalculatedDate = getStartOfWeek(date, locale, calendarStartDay); break; case KeyType.End: - newCalculatedDate = getEndOfWeek(date); + newCalculatedDate = getEndOfWeek(date, locale, calendarStartDay); break; } return newCalculatedDate; diff --git a/src/test/date_utils_test.test.ts b/src/test/date_utils_test.test.ts index ee5b98047..c2a320607 100644 --- a/src/test/date_utils_test.test.ts +++ b/src/test/date_utils_test.test.ts @@ -6,6 +6,7 @@ import { setMinutes, } from "date-fns"; import { ptBR } from "date-fns/locale/pt-BR"; +import { enGB } from "date-fns/locale/en-GB"; import { newDate, @@ -39,6 +40,8 @@ import { isQuarterInRange, isYearInRange, getStartOfYear, + getStartOfWeek, + getEndOfWeek, getYearsPeriod, setDefaultLocale, yearsDisabledAfter, @@ -1382,6 +1385,39 @@ describe("date_utils", () => { }); }); + describe("getEndOfWeek", () => { + // 2024-06-12 is a Wednesday + const wednesday = new Date("2024-06-12T12:00:00"); + + it("defaults to a Sunday-Saturday week", () => { + // end of a Sunday-start week containing Wed 2024-06-12 is Sat 2024-06-15 + expect(getEndOfWeek(wednesday).getDate()).toBe(15); + }); + + it("respects calendarStartDay for a Monday-Sunday week (#6326)", () => { + // end of a Monday-start week containing Wed 2024-06-12 is Sun 2024-06-16 + expect(getEndOfWeek(wednesday, undefined, 1).getDate()).toBe(16); + }); + + it("stays symmetric with getStartOfWeek for the same calendarStartDay", () => { + const start = getStartOfWeek(wednesday, undefined, 1); + const end = getEndOfWeek(wednesday, undefined, 1); + expect(start.getDate()).toBe(10); // Mon 2024-06-10 + expect(end.getDate()).toBe(16); // Sun 2024-06-16 + }); + + it("uses the week start defined by a passed locale object", () => { + // enGB weeks start on Monday, so the week containing Wed 2024-06-12 + // ends on Sun 2024-06-16 + expect(getEndOfWeek(wednesday, enGB).getDate()).toBe(16); + }); + + it("uses the week start defined by a registered locale name", () => { + registerLocale("en-GB", enGB); + expect(getEndOfWeek(wednesday, "en-GB").getDate()).toBe(16); + }); + }); + describe("safeDateRangeFormat", () => { const props = { dateFormat: "MM/dd/yyyy", diff --git a/src/test/datepicker_test.test.tsx b/src/test/datepicker_test.test.tsx index 31b239b37..1afa5c0fd 100644 --- a/src/test/datepicker_test.test.tsx +++ b/src/test/datepicker_test.test.tsx @@ -1494,6 +1494,22 @@ describe("DatePicker", () => { ); }); + it("should handle onDayKeyDown End honoring calendarStartDay (#6326)", () => { + const data = getOnInputKeyDownStuff({ + selected: newDate("2024-06-12"), // Wednesday + calendarStartDay: 1, // Monday-Sunday week + }); + fireEvent.keyDown(data.dateInput, getKey(KeyType.ArrowDown)); + const selectedDayNode = getSelectedDayNode(data.container); + expect(selectedDayNode).toBeTruthy(); + fireEvent.keyDown(selectedDayNode!, getKey(KeyType.End)); + expect(data.instance.state.preSelection).toBeTruthy(); + // last day of a Monday-Sunday week containing Wed 2024-06-12 is Sun 2024-06-16 + expect(formatDate(data.instance.state.preSelection!, data.testFormat)).toBe( + "2024-06-16", + ); + }); + it("should handle onDayKeyDown when the minDate is null", () => { const data = getOnInputKeyDownStuff({ minDate: null }); fireEvent.keyDown(data.dateInput, getKey(KeyType.ArrowDown));