Skip to content

Commit 29db157

Browse files
Merge pull request #4692 from lasseklovstad/monthpicker-keyboard
fix: #4400 Keyboard accessibility MonthYearPicker
2 parents 4500100 + 65ea2f6 commit 29db157

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/index.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,10 @@ export default class DatePicker extends React.Component {
838838
const selectorString =
839839
this.props.showWeekPicker && this.props.showWeekNumbers
840840
? '.react-datepicker__week-number[tabindex="0"]'
841-
: '.react-datepicker__day[tabindex="0"]';
841+
: this.props.showFullMonthYearPicker ||
842+
this.props.showMonthYearPicker
843+
? '.react-datepicker__month-text[tabindex="0"]'
844+
: '.react-datepicker__day[tabindex="0"]';
842845
const selectedItem =
843846
this.calendar.componentNode &&
844847
this.calendar.componentNode.querySelector(selectorString);

test/datepicker_test.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,25 @@ describe("DatePicker", () => {
12851285
fireEvent.keyDown(data.dateInput, getKey("ArrowDown"));
12861286
expect(data.instance.state.open).toBe(true);
12871287
});
1288+
it("should focus first month when the down arrow key is pressed", async () => {
1289+
const div = document.createElement("div");
1290+
document.body.appendChild(div);
1291+
render(<DatePicker showMonthYearPicker />, {
1292+
container: div,
1293+
});
1294+
1295+
// user focuses the input field, the calendar opens
1296+
const dateInput = div.querySelector("input");
1297+
fireEvent.focus(dateInput);
1298+
fireEvent.keyDown(dateInput, getKey("ArrowDown"));
1299+
1300+
await waitFor(() => {
1301+
const selectedMonth = div.querySelector(
1302+
'.react-datepicker__month-text[tabindex="0"]',
1303+
);
1304+
expect(document.activeElement).toBe(selectedMonth);
1305+
});
1306+
});
12881307
it("should not open the calendar when the left arrow key is pressed", () => {
12891308
const data = getOnInputKeyDownStuff();
12901309
act(() => {

0 commit comments

Comments
 (0)