Skip to content

Commit 16191b2

Browse files
authored
fix: prevent selecting disabled focused days (#2860)
Fixes a test-only regression from #2851: jsdom allows focusing a disabled day, which drops the `disabled` attribute (keeps `aria-disabled`), so `toBeDisabled` fails and `onSelect` fires in tests even though browsers don’t allow focusing disabled buttons.
1 parent e166841 commit 16191b2

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/DayPicker.test.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
nextButton,
1010
previousButton,
1111
} from "@/test/elements";
12-
import { fireEvent, render, screen } from "@/test/render";
12+
import { act, fireEvent, render, screen } from "@/test/render";
1313
import { setTestTime } from "@/test/setTestTime";
1414
import { user } from "@/test/user";
1515
import { defaultLocale } from "./classes/DateLib";
@@ -122,6 +122,31 @@ describe("when the grid is focused", () => {
122122
});
123123
});
124124

125+
describe("when a disabled day is focused", () => {
126+
test("keyboard and mouse interactions do not select it", async () => {
127+
const disabledDay = new Date(2024, 8, 5);
128+
const handleSelect = jest.fn();
129+
130+
render(
131+
<DayPicker
132+
defaultMonth={disabledDay}
133+
disabled={[disabledDay]}
134+
mode="single"
135+
onSelect={handleSelect}
136+
/>,
137+
);
138+
139+
const disabledElement = dateButton(disabledDay);
140+
act(() => disabledElement.focus());
141+
142+
await user.keyboard("{Enter}");
143+
await user.click(disabledElement);
144+
145+
expect(handleSelect).not.toHaveBeenCalled();
146+
expect(disabledElement).toHaveAttribute("aria-disabled", "true");
147+
});
148+
});
149+
125150
describe("when navigation is disabled", () => {
126151
beforeEach(() => {
127152
jest.useFakeTimers();

src/DayPicker.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ export function DayPicker(initialProps: DayPickerProps) {
242242
e.preventDefault();
243243
e.stopPropagation();
244244
setFocused(day);
245+
if (m.disabled) {
246+
return;
247+
}
245248
select?.(day.date, m, e);
246249
onDayClick?.(day.date, m, e);
247250
},

0 commit comments

Comments
 (0)