Skip to content

Commit 677ee67

Browse files
committed
✨ Fix quarter selection style issue for selectsMultiple
- Introduced `isSelectQuarterInList` method to handle selection of multiple quarters. - Updated the rendering logic to apply the selected class based on the new method. - Added tests to verify correct application of the selected class for both single and multiple selected dates. Closes #5971
1 parent 7dd045e commit 677ee67

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

src/month.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ export default class Month extends Component<MonthProps> {
415415
isSelectedQuarter = (day: Date, q: number, selected: Date): boolean =>
416416
getQuarter(selected) === q && getYear(day) === getYear(selected);
417417

418+
isSelectQuarterInList = (day: Date, q: number, selectedDates: Date[]) =>
419+
selectedDates.some((selectedDate) =>
420+
this.isSelectedQuarter(day, q, selectedDate),
421+
);
422+
418423
isMonthSelected = () => {
419424
const { day, selected, selectedDates, selectsMultiple } = this.props;
420425
const monthIdx = getMonth(day);
@@ -934,7 +939,6 @@ export default class Month extends Component<MonthProps> {
934939
day,
935940
startDate,
936941
endDate,
937-
selected,
938942
minDate,
939943
maxDate,
940944
excludeDates,
@@ -954,13 +958,15 @@ export default class Month extends Component<MonthProps> {
954958
disabled) &&
955959
isQuarterDisabled(setQuarter(day, q), this.props);
956960

961+
const selection = this.getSelection();
962+
957963
return clsx(
958964
"react-datepicker__quarter-text",
959965
`react-datepicker__quarter-${q}`,
960966
{
961967
"react-datepicker__quarter-text--disabled": isDisabled,
962-
"react-datepicker__quarter-text--selected": selected
963-
? this.isSelectedQuarter(day, q, selected)
968+
"react-datepicker__quarter-text--selected": selection
969+
? this.isSelectQuarterInList(day, q, selection)
964970
: undefined,
965971
"react-datepicker__quarter-text--keyboard-selected":
966972
!disabledKeyboardNavigation &&

src/test/month_test.test.tsx

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,56 @@ describe("Month", () => {
12021202
});
12031203

12041204
it("should have no axe violations", () => runAxe(monthComponent));
1205+
1206+
it("should apply the selected class when the quarter is a part of selected date", () => {
1207+
const selectedDate = newDate("2025-11-01");
1208+
const keyboardSelectedDate = selectedDate;
1209+
1210+
const { container } = render(
1211+
<Month
1212+
day={selectedDate}
1213+
selected={selectedDate}
1214+
preSelection={keyboardSelectedDate}
1215+
showQuarterYearPicker
1216+
/>,
1217+
);
1218+
1219+
const selected = container.querySelector(
1220+
".react-datepicker__quarter-text--selected",
1221+
);
1222+
expect(selected).not.toBeNull();
1223+
expect(selected?.textContent).toBe("Q4");
1224+
});
1225+
1226+
it("should apply the selected class when the quarter is a part of selected dates", () => {
1227+
const selectedDates = [newDate("2025-11-01"), newDate("2025-01-01")];
1228+
const keyboardSelectedDate = selectedDates[0];
1229+
const day = selectedDates[0] as Date;
1230+
1231+
const { container } = render(
1232+
<Month
1233+
day={day}
1234+
selectedDates={selectedDates}
1235+
preSelection={keyboardSelectedDate}
1236+
selectsMultiple
1237+
showQuarterYearPicker
1238+
/>,
1239+
);
1240+
1241+
const selectedElements = Array.from(
1242+
container.querySelectorAll(".react-datepicker__quarter-text--selected"),
1243+
);
1244+
expect(selectedElements.length).toBe(selectedDates.length);
1245+
1246+
const expectedQuarterLabels = selectedDates.map(
1247+
(date) => `Q${getQuarter(date)}`,
1248+
);
1249+
expect(
1250+
expectedQuarterLabels.every((label) =>
1251+
selectedElements.some((element) => element?.textContent === label),
1252+
),
1253+
).toBe(true);
1254+
});
12051255
});
12061256

12071257
describe("if quarter is not selected", () => {
@@ -2653,6 +2703,32 @@ describe("Month", () => {
26532703
expect(selected).not.toBeNull();
26542704
expect(keyboardSelected).toBeNull();
26552705
});
2706+
2707+
it("should not apply the keyboard-selected class when the quarter is a part of selected dates", () => {
2708+
const selectedDates = [newDate("2025-11-01")];
2709+
const keyboardSelectedDate = selectedDates[0];
2710+
const day = selectedDates[0] as Date;
2711+
2712+
const { container } = render(
2713+
<Month
2714+
day={day}
2715+
selectedDates={selectedDates}
2716+
preSelection={keyboardSelectedDate}
2717+
selectsMultiple
2718+
showQuarterYearPicker
2719+
/>,
2720+
);
2721+
2722+
const selected = container.querySelector(
2723+
".react-datepicker__quarter-text--selected",
2724+
);
2725+
const keyboardSelected = container.querySelector(
2726+
".react-datepicker__quarter-text--keyboard-selected",
2727+
);
2728+
2729+
expect(selected).not.toBeNull();
2730+
expect(keyboardSelected).toBeNull();
2731+
});
26562732
});
26572733

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

0 commit comments

Comments
 (0)