Skip to content

Commit 0845602

Browse files
committed
🐛 Apply --keyboard-selected to <Month /> only if it's not already selected
Updated --keyboard-selected applying condition similar to <Day /> component Closes #5643
1 parent 9a234e8 commit 0845602

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/month.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ export default class Month extends Component<MonthProps> {
413413
isSelectedQuarter = (day: Date, q: number, selected: Date): boolean =>
414414
getQuarter(day) === q && getYear(day) === getYear(selected);
415415

416+
isMonthSelected = () => {
417+
const { day, selected, selectedDates, selectsMultiple } = this.props;
418+
const monthIdx = getMonth(day);
419+
420+
if (selectsMultiple) {
421+
return selectedDates?.some((date) =>
422+
this.isSelectedMonth(day, monthIdx, date),
423+
);
424+
}
425+
426+
return !!selected && this.isSelectedMonth(day, monthIdx, selected);
427+
};
428+
416429
renderWeeks = () => {
417430
const weeks = [];
418431
const isFixedHeight = this.props.fixedHeight;
@@ -821,6 +834,7 @@ export default class Month extends Component<MonthProps> {
821834
!this.props.disabledKeyboardNavigation &&
822835
preSelection &&
823836
this.isSelectedMonth(day, m, preSelection) &&
837+
!this.isMonthSelected() &&
824838
!this.isMonthDisabled(m),
825839
"react-datepicker__month-text--in-selecting-range":
826840
this.isInSelectingRangeMonth(m),

src/test/month_test.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,30 @@ describe("Month", () => {
25522552
).toBeNull();
25532553
});
25542554

2555+
it("should not apply the keyboard-selected class when the month is a part of selected dates", () => {
2556+
const selectedDate = newDate("2024-06-03");
2557+
const keyboardSelectedDate = selectedDate;
2558+
2559+
const { container } = render(
2560+
<Month
2561+
day={selectedDate}
2562+
selected={selectedDate}
2563+
preSelection={keyboardSelectedDate}
2564+
showMonthYearPicker
2565+
/>,
2566+
);
2567+
2568+
const selected = container.querySelector(
2569+
".react-datepicker__month-text--selected",
2570+
);
2571+
const keyboardSelected = container.querySelector(
2572+
".react-datepicker__month-text--keyboard-selected",
2573+
);
2574+
2575+
expect(selected).not.toBeNull();
2576+
expect(keyboardSelected).toBeNull();
2577+
});
2578+
25552579
it("should not apply the keyboard-selected class when the quarter is a part of disabled dates", () => {
25562580
const currentSelectedDate = newDate("2023-08-08");
25572581
const maxDate = newDate("2024-08-03");

0 commit comments

Comments
 (0)