Skip to content

Commit 583d0e2

Browse files
Merge pull request #5524 from qburst/issue-5322/fix/selected-style-for-multi-year-picker
🐛🎨 Apply the `--selected` class for the YearPicker when the `selectsMultiple` is enabled
2 parents 386fce0 + 9d42380 commit 583d0e2

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/test/year_picker_test.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,47 @@ describe("YearPicker", () => {
107107
expect(year).toBe(getYear(date).toString());
108108
});
109109

110+
it("should has selected class applied for all the selectedDates when selectsMultiple is set", () => {
111+
const selectedDates = [new Date("2025-01-01"), new Date("2026-01-01")];
112+
const { container } = render(
113+
<Year
114+
selectsMultiple
115+
selectedDates={selectedDates}
116+
date={selectedDates[0]}
117+
onYearMouseEnter={() => {}}
118+
onYearMouseLeave={() => {}}
119+
/>,
120+
);
121+
const yearElements = Array.from(
122+
container.querySelectorAll(".react-datepicker__year-text--selected"),
123+
);
124+
125+
expect(yearElements.length).toBe(selectedDates.length);
126+
127+
const isSelectedDatesHighlighted = yearElements.every((yearElement) => {
128+
const yearValue = yearElement?.textContent;
129+
return selectedDates.some(
130+
(selectedDate) => getYear(selectedDate).toString() === yearValue,
131+
);
132+
});
133+
expect(isSelectedDatesHighlighted).toBe(true);
134+
});
135+
136+
it("should not has selected class where there is no selectedDates when selectsMultiple is set", () => {
137+
const { container } = render(
138+
<Year
139+
selectsMultiple
140+
date={new Date()}
141+
onYearMouseEnter={() => {}}
142+
onYearMouseLeave={() => {}}
143+
/>,
144+
);
145+
const yearElements = Array.from(
146+
container.querySelectorAll(".react-datepicker__year-text--selected"),
147+
);
148+
expect(yearElements.length).toBe(0);
149+
});
150+
110151
it("should have current year class when element of array equal of current year", () => {
111152
const date = new Date();
112153
const { container } = render(

src/year.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ interface YearProps
3838
) => void;
3939
preSelection?: Date | null;
4040
setPreSelection?: (date?: Date | null) => void;
41+
selectsMultiple?: boolean;
42+
selectedDates?: Date[];
4143
selected?: Date | null;
4244
inline?: boolean;
4345
usePointerEvent?: boolean;
@@ -240,6 +242,15 @@ export default class Year extends Component<YearProps> {
240242
);
241243
};
242244

245+
isSelectedYear = (year: number) => {
246+
const { selectsMultiple, selected, selectedDates } = this.props;
247+
248+
if (selectsMultiple) {
249+
return selectedDates?.some((date) => year === getYear(date));
250+
}
251+
return !selected || year === getYear(selected);
252+
};
253+
243254
onYearClick = (
244255
event:
245256
| React.MouseEvent<HTMLDivElement, MouseEvent>
@@ -360,7 +371,6 @@ export default class Year extends Component<YearProps> {
360371
date,
361372
minDate,
362373
maxDate,
363-
selected,
364374
excludeDates,
365375
includeDates,
366376
filterDate,
@@ -372,9 +382,7 @@ export default class Year extends Component<YearProps> {
372382
`react-datepicker__year-${y}`,
373383
date ? yearClassName?.(setYear(date, y)) : undefined,
374384
{
375-
"react-datepicker__year-text--selected": selected
376-
? y === getYear(selected)
377-
: undefined,
385+
"react-datepicker__year-text--selected": this.isSelectedYear(y),
378386
"react-datepicker__year-text--disabled":
379387
(minDate || maxDate || excludeDates || includeDates || filterDate) &&
380388
isYearDisabled(y, this.props),

0 commit comments

Comments
 (0)