Skip to content

Commit

Permalink
Merge pull request #4732 from ryantanrk/fix/disabled-month-kb
Browse files Browse the repository at this point in the history
fix: invalid time error when selecting excluded month with keyboard
  • Loading branch information
martijnrusschen authored Apr 26, 2024
2 parents 69a8ea6 + 7f9fb19 commit 0cb6f84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/month.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function getMonthColumnsLayout(
return MONTH_COLUMNS_LAYOUT.THREE_COLUMNS;
}

export default class Month extends React.Component {
class Month extends React.Component {
static propTypes = {
ariaLabelPrefix: PropTypes.string,
chooseDayAriaLabelPrefix: PropTypes.string,
Expand Down Expand Up @@ -455,8 +455,10 @@ export default class Month extends React.Component {
const monthsGrid = MONTH_COLUMNS[monthColumnsLayout].grid;
switch (eventKey) {
case "Enter":
this.onMonthClick(event, month);
setPreSelection(selected);
if (!this.isMonthDisabled(month)) {
this.onMonthClick(event, month);
setPreSelection(selected);
}
break;
case "ArrowRight":
this.handleMonthNavigation(
Expand Down Expand Up @@ -839,3 +841,5 @@ export default class Month extends React.Component {
);
}
}

export default Month;
22 changes: 22 additions & 0 deletions test/month_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,28 @@ describe("Month", () => {
);
});

it("should prevent selection of disabled month", () => {
const excludeDates = [utils.newDate("2015-08-01")];
let selected = utils.newDate("2015-07-01");
let day = utils.newDate("2015-08-01");

const monthComponent = renderMonth({
selected: selected,
day: day,
month: 7,
preSelection: excludeDates[0],
setPreSelection: () => {},
excludeDates: excludeDates,
});

fireEvent.keyDown(
monthComponent.querySelector(".react-datepicker__month-7"),
getKey("Enter"),
);

expect(selected).not.toBe(excludeDates[0]);
});

it("should prevent navigation", () => {
let preSelected = utils.newDate("2015-08-01");
const setPreSelection = (param) => {
Expand Down

0 comments on commit 0cb6f84

Please sign in to comment.