Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4483: Enable onKeyDown handler for the month picker view and the year picker view #4533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ export default class Calendar extends React.Component {
monthClassName={this.props.monthClassName}
onDayClick={this.handleDayClick}
handleOnKeyDown={this.props.handleOnDayKeyDown}
handleOnMonthKeyDown={this.props.handleOnKeyDown}
onDayMouseEnter={this.handleDayMouseEnter}
onMouseLeave={this.handleMonthMouseLeave}
onWeekSelect={this.props.onWeekSelect}
Expand Down
4 changes: 4 additions & 0 deletions src/month.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class Month extends React.Component {
showQuarterYearPicker: PropTypes.bool,
showWeekPicker: PropTypes.bool,
handleOnKeyDown: PropTypes.func,
handleOnMonthKeyDown: PropTypes.func,
isInputFocused: PropTypes.bool,
weekAriaLabelPrefix: PropTypes.string,
containerRef: PropTypes.oneOfType([
Expand Down Expand Up @@ -400,6 +401,7 @@ export default class Month extends React.Component {
showTwoColumnMonthYearPicker,
showFourColumnMonthYearPicker,
setPreSelection,
handleOnMonthKeyDown,
} = this.props;
const eventKey = event.key;
if (eventKey !== "Tab") {
Expand Down Expand Up @@ -451,6 +453,8 @@ export default class Month extends React.Component {
break;
}
}

handleOnMonthKeyDown && handleOnMonthKeyDown(event);
};

onQuarterClick = (e, q) => {
Expand Down
5 changes: 5 additions & 0 deletions src/year.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class Year extends React.Component {
includeDates: PropTypes.array,
filterDate: PropTypes.func,
yearItemNumber: PropTypes.number,
handleOnKeyDown: PropTypes.func,
};

constructor(props) {
Expand Down Expand Up @@ -157,6 +158,8 @@ export default class Year extends React.Component {

onYearKeyDown = (e, y) => {
const { key } = e;
const { handleOnKeyDown } = this.props;

if (!this.props.disabledKeyboardNavigation) {
switch (key) {
case "Enter":
Expand All @@ -177,6 +180,8 @@ export default class Year extends React.Component {
break;
}
}

handleOnKeyDown && handleOnKeyDown(e);
};

getYearClassNames = (y) => {
Expand Down
26 changes: 26 additions & 0 deletions test/month_test.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import Month from "../src/month";
import Day from "../src/day";
import DatePicker from "../src";
import range from "lodash/range";
import { mount, shallow } from "enzyme";
import * as utils from "../src/date_utils";
Expand Down Expand Up @@ -935,6 +937,30 @@ describe("Month", () => {
).toBe(false);
});
});

it("should call onKeyDown handler on any key press", () => {
const onKeyDownSpy = jest.fn();

const { container } = render(
<DatePicker
selected={new Date()}
dateFormat="MM/yyyy"
showMonthYearPicker
onKeyDown={onKeyDownSpy}
/>,
);

const dateInput = container.querySelector("input");
fireEvent.focus(dateInput);

const month = container.querySelector(".react-datepicker__month-0");

fireEvent.keyDown(month, {
key: "ArrowDown",
});

expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});
});

describe("Keyboard navigation", () => {
Expand Down
25 changes: 25 additions & 0 deletions test/year_picker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DatePicker from "../src/index.jsx";
import Year from "../src/year.jsx";
import TestUtils from "react-dom/test-utils";
import ReactDOM from "react-dom";
import { render, fireEvent } from "@testing-library/react";
import * as utils from "../src/date_utils.js";
import Calendar from "../src/calendar.jsx";
import { getKey } from "./test_utils.js";
Expand Down Expand Up @@ -599,6 +600,30 @@ describe("YearPicker", () => {
expect(utils.getYear(selectedDay)).toBe(2021);
});

it("should call onKeyDown handler on any key press", () => {
const onKeyDownSpy = jest.fn();

const { container } = render(
<DatePicker
selected={new Date()}
showYearPicker
dateFormat="yyyy"
onKeyDown={onKeyDownSpy}
/>,
);

const dateInput = container.querySelector("input");
fireEvent.focus(dateInput);

const year = container.querySelector(".react-datepicker__year-text");

fireEvent.keyDown(year, {
key: "ArrowDown",
});

expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});

it("should select 2021 when Space key is pressed", () => {
const yearPicker = getPicker("2021-01-01");

Expand Down
Loading