Skip to content

Commit c150b04

Browse files
Merge pull request #6054 from Hacker0x01/fix/keyboard-focus-inline-navigation
Fix keyboard focus loss during same-month navigation in inline mode
2 parents ca268cf + 3e53cf4 commit c150b04

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/index.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,20 +1395,12 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
13951395
this.setSelected(newSelection);
13961396
}
13971397
this.setPreSelection(newSelection);
1398-
// need to figure out whether month has changed to focus day in inline version
1398+
// In inline mode, always set shouldFocusDayInline to true when navigating via keyboard.
1399+
// This ensures focus is properly transferred to the new day element regardless of
1400+
// whether the month changed. The user initiated this navigation from a focused day,
1401+
// so we should always focus the destination day.
13991402
if (inline) {
1400-
const prevMonth = getMonth(copy);
1401-
const newMonth = getMonth(newSelection);
1402-
const prevYear = getYear(copy);
1403-
const newYear = getYear(newSelection);
1404-
1405-
if (prevMonth !== newMonth || prevYear !== newYear) {
1406-
// month has changed
1407-
this.setState({ shouldFocusDayInline: true });
1408-
} else {
1409-
// month hasn't changed
1410-
this.setState({ shouldFocusDayInline: false });
1411-
}
1403+
this.setState({ shouldFocusDayInline: true });
14121404
}
14131405
};
14141406

src/test/datepicker_test.test.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,7 +3884,7 @@ describe("DatePicker", () => {
38843884
describe("shouldFocusDayInline state", () => {
38853885
const dateFormat = "yyyy-MM-dd";
38863886

3887-
it("should not be updated when navigating with ArrowRight key without changing displayed month", () => {
3887+
it("should be set to true when navigating with ArrowRight key without changing displayed month", () => {
38883888
let instance: DatePicker | null = null;
38893889
const { container } = render(
38903890
<DatePicker
@@ -3900,7 +3900,8 @@ describe("DatePicker", () => {
39003900
expect(selectedDayNode).toBeTruthy();
39013901
fireEvent.keyDown(selectedDayNode!, getKey(KeyType.ArrowRight));
39023902
expect(instance).toBeTruthy();
3903-
expect(instance!.state.shouldFocusDayInline).toBe(false);
3903+
// Always set to true for keyboard navigation to ensure focus transfers correctly
3904+
expect(instance!.state.shouldFocusDayInline).toBe(true);
39043905
});
39053906

39063907
it("should be set to true when changing displayed month with ArrowRight key", () => {
@@ -3940,6 +3941,54 @@ describe("DatePicker", () => {
39403941
expect(instance).toBeTruthy();
39413942
expect(instance!.state.shouldFocusDayInline).toBe(true);
39423943
});
3944+
3945+
it("should maintain keyboard focus when navigating within same month in inline selectsRange mode with showPreviousMonths", () => {
3946+
// This test verifies the fix for GitHub issue #5750
3947+
// Focus was being lost when navigating within the same month in inline mode
3948+
// with selectsRange and showPreviousMonths enabled
3949+
const startDate = newDate("2025-06-01");
3950+
const endDate = newDate("2025-07-01");
3951+
const div = document.createElement("div");
3952+
document.body.appendChild(div);
3953+
3954+
const { container } = render(
3955+
<DatePicker
3956+
selectsRange
3957+
inline
3958+
showPreviousMonths
3959+
monthsShown={2}
3960+
startDate={startDate}
3961+
endDate={endDate}
3962+
openToDate={endDate}
3963+
/>,
3964+
{ container: div },
3965+
);
3966+
3967+
// Find the start date (June 1) and focus it
3968+
const startDateNode = container.querySelector(
3969+
'.react-datepicker__day--range-start[tabindex="0"]',
3970+
);
3971+
expect(startDateNode).toBeTruthy();
3972+
act(() => {
3973+
(startDateNode as HTMLElement)?.focus();
3974+
});
3975+
expect(document.activeElement).toBe(startDateNode);
3976+
3977+
// Navigate right (to June 2, same month)
3978+
fireEvent.keyDown(startDateNode!, getKey(KeyType.ArrowRight));
3979+
3980+
// After navigation, focus should be on June 2, not lost to body
3981+
const newFocusedDay = container.querySelector(
3982+
'.react-datepicker__day[tabindex="0"]',
3983+
);
3984+
expect(newFocusedDay).toBeTruthy();
3985+
expect(document.activeElement).not.toBe(document.body);
3986+
expect(
3987+
document.activeElement?.classList.contains("react-datepicker__day"),
3988+
).toBe(true);
3989+
3990+
document.body.removeChild(div);
3991+
});
39433992
});
39443993

39453994
describe("Calendar Header Accessibility", () => {

0 commit comments

Comments
 (0)