Skip to content

Commit ec9b1eb

Browse files
committed
2 parents 7e0ab25 + 019b328 commit ec9b1eb

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/DayPicker.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,46 @@ describe("when the grid is focused", () => {
122122
});
123123
});
124124

125+
describe("when navigation is disabled", () => {
126+
beforeEach(() => {
127+
jest.useFakeTimers();
128+
});
129+
130+
afterEach(() => {
131+
jest.useRealTimers();
132+
});
133+
134+
test("keyboard navigation stays within the visible month", async () => {
135+
const defaultMonth = new Date(2025, 6, 1);
136+
const lastDay = new Date(2025, 6, 31);
137+
const previousDay = new Date(2025, 6, 30);
138+
139+
render(
140+
<DayPicker
141+
disableNavigation
142+
defaultMonth={defaultMonth}
143+
selected={lastDay}
144+
mode="single"
145+
/>,
146+
);
147+
148+
await user.tab();
149+
await user.tab();
150+
await user.tab();
151+
152+
const lastDayButton = dateButton(lastDay);
153+
const previousDayButton = dateButton(previousDay);
154+
155+
expect(activeElement()).toBe(lastDayButton);
156+
157+
await user.keyboard("{ArrowRight}");
158+
expect(activeElement()).toBe(lastDayButton);
159+
160+
await user.keyboard("{ArrowLeft}");
161+
expect(activeElement()).toBe(previousDayButton);
162+
});
163+
});
164+
125165
describe("when a day is mouse entered", () => {
126166
const handleDayMouseEnter = jest.fn();
127167
const handleDayMouseLeave = jest.fn();

src/useFocus.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ export function useFocus<T extends DayPickerProps>(
8080
);
8181
if (!nextFocus) return;
8282

83+
if (props.disableNavigation) {
84+
const isNextInCalendar = calendar.days.some((day) =>
85+
day.isEqualTo(nextFocus),
86+
);
87+
if (!isNextInCalendar) {
88+
return;
89+
}
90+
}
91+
8392
calendar.goToDay(nextFocus);
8493
setFocused(nextFocus);
8594
};

0 commit comments

Comments
 (0)