Skip to content

Commit 7dd045e

Browse files
committed
🎨 Fix quarter selection style issue to avoid --keyboard-selected class to the selected element
- Updated the `isSelectedQuarter` method to properly compare the selected quarter. - Introduced `isQuarterSelected` method to handle quarter selection logic. - Added a test to ensure the keyboard-selected class is not applied when the quarter is part of the selected date. Closes #5970
1 parent 3da627e commit 7dd045e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

‎src/month.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export default class Month extends Component<MonthProps> {
413413
);
414414

415415
isSelectedQuarter = (day: Date, q: number, selected: Date): boolean =>
416-
getQuarter(day) === q && getYear(day) === getYear(selected);
416+
getQuarter(selected) === q && getYear(day) === getYear(selected);
417417

418418
isMonthSelected = () => {
419419
const { day, selected, selectedDates, selectsMultiple } = this.props;
@@ -428,6 +428,19 @@ export default class Month extends Component<MonthProps> {
428428
return !!selected && this.isSelectedMonth(day, monthIdx, selected);
429429
};
430430

431+
isQuarterSelected = () => {
432+
const { day, selected, selectedDates, selectsMultiple } = this.props;
433+
const quarterIdx = getQuarter(day);
434+
435+
if (selectsMultiple) {
436+
return selectedDates?.some((selectedDate) =>
437+
this.isSelectedQuarter(day, quarterIdx, selectedDate),
438+
);
439+
}
440+
441+
return !!selected && this.isSelectedQuarter(day, quarterIdx, selected);
442+
};
443+
431444
renderWeeks = () => {
432445
const weeks = [];
433446
const isFixedHeight = this.props.fixedHeight;
@@ -953,6 +966,7 @@ export default class Month extends Component<MonthProps> {
953966
!disabledKeyboardNavigation &&
954967
preSelection &&
955968
this.isSelectedQuarter(day, q, preSelection) &&
969+
!this.isQuarterSelected() &&
956970
!isDisabled,
957971
"react-datepicker__quarter-text--in-selecting-range":
958972
this.isInSelectingRangeQuarter(q),

‎src/test/month_test.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,30 @@ describe("Month", () => {
26292629
),
26302630
).toBeNull();
26312631
});
2632+
2633+
it("should not apply the keyboard-selected class when the quarter is a part of selected date", () => {
2634+
const selectedDate = newDate("2025-11-01");
2635+
const keyboardSelectedDate = selectedDate;
2636+
2637+
const { container } = render(
2638+
<Month
2639+
day={selectedDate}
2640+
selected={selectedDate}
2641+
preSelection={keyboardSelectedDate}
2642+
showQuarterYearPicker
2643+
/>,
2644+
);
2645+
2646+
const selected = container.querySelector(
2647+
".react-datepicker__quarter-text--selected",
2648+
);
2649+
const keyboardSelected = container.querySelector(
2650+
".react-datepicker__quarter-text--keyboard-selected",
2651+
);
2652+
2653+
expect(selected).not.toBeNull();
2654+
expect(keyboardSelected).toBeNull();
2655+
});
26322656
});
26332657

26342658
describe("Auto-Focus", () => {

0 commit comments

Comments
 (0)