Skip to content

Commit 17a0a33

Browse files
Merge pull request #4955 from qburst/issue-4933/fix/default-keyboard-selected-on-next-view
Fix #4933: 🐛Remove the auto set of the '--keyboard-selected' class from the disabled dates while switching to the next or the previous view
2 parents f5ff41d + da382fb commit 17a0a33

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

src/day.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,20 @@ export default class Day extends Component<DayProps> {
188188
? this.props.selectedDates?.some((date) => this.isSameDayOrWeek(date))
189189
: this.isSameDayOrWeek(this.props.selected);
190190

191-
return !isSelectedDate && this.isSameDayOrWeek(this.props.preSelection);
191+
const isDisabled =
192+
this.props.preSelection && this.isDisabled(this.props.preSelection);
193+
194+
return (
195+
!isSelectedDate &&
196+
this.isSameDayOrWeek(this.props.preSelection) &&
197+
!isDisabled
198+
);
192199
};
193200

194-
isDisabled = () =>
201+
isDisabled = (day = this.props.day) =>
195202
// Almost all props previously were passed as this.props w/o proper typing with prop-types
196203
// after the migration to TS i made it explicit
197-
isDayDisabled(this.props.day, {
204+
isDayDisabled(day, {
198205
minDate: this.props.minDate,
199206
maxDate: this.props.maxDate,
200207
excludeDates: this.props.excludeDates,

src/month.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ export default class Month extends Component<MonthProps> {
819819
"react-datepicker__month-text--keyboard-selected":
820820
!this.props.disabledKeyboardNavigation &&
821821
preSelection &&
822-
this.isSelectedMonth(day, m, preSelection),
822+
this.isSelectedMonth(day, m, preSelection) &&
823+
!this.isMonthDisabled(m),
823824
"react-datepicker__month-text--in-selecting-range":
824825
this.isInSelectingRangeMonth(m),
825826
"react-datepicker__month-text--in-range":
@@ -887,9 +888,17 @@ export default class Month extends Component<MonthProps> {
887888
selected,
888889
minDate,
889890
maxDate,
891+
excludeDates,
892+
includeDates,
893+
filterDate,
890894
preSelection,
891895
disabledKeyboardNavigation,
892896
} = this.props;
897+
898+
const isDisabled =
899+
(minDate || maxDate || excludeDates || includeDates || filterDate) &&
900+
isQuarterDisabled(setQuarter(day, q), this.props);
901+
893902
return clsx(
894903
"react-datepicker__quarter-text",
895904
`react-datepicker__quarter-${q}`,
@@ -903,7 +912,8 @@ export default class Month extends Component<MonthProps> {
903912
"react-datepicker__quarter-text--keyboard-selected":
904913
!disabledKeyboardNavigation &&
905914
preSelection &&
906-
this.isSelectedQuarter(day, q, preSelection),
915+
this.isSelectedQuarter(day, q, preSelection) &&
916+
!isDisabled,
907917
"react-datepicker__quarter-text--in-selecting-range":
908918
this.isInSelectingRangeQuarter(q),
909919
"react-datepicker__quarter-text--in-range":

src/test/day_test.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ describe("Day", () => {
164164
).toBe(true);
165165
});
166166

167+
it("should not apply the key-selected class when pre-selected is a part of disabled dates", () => {
168+
const day = newDate();
169+
const container = renderDay(day, {
170+
excludeDates: [day],
171+
preSelection: day,
172+
});
173+
const dayNode = container.querySelector(".react-datepicker__day")!;
174+
175+
expect(dayNode.classList.contains(className)).toBe(false);
176+
});
177+
167178
it("should not apply the keyboard-selected class when selected", () => {
168179
const day = newDate();
169180
const container = renderDay(day, { selected: day, preSelection: day });

src/test/month_test.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,4 +2418,53 @@ describe("Month", () => {
24182418
).toBe(false);
24192419
});
24202420
});
2421+
2422+
describe("keyboard-selected", () => {
2423+
it("should not apply the keyboard-selected class when the month is a part of disabled dates", () => {
2424+
const keyboardSelectedDate = newDate("2024-06-03");
2425+
const excludeDates = [addWeeks(keyboardSelectedDate, 1)];
2426+
2427+
const { container } = render(
2428+
<Month
2429+
day={keyboardSelectedDate}
2430+
preSelection={keyboardSelectedDate}
2431+
excludeDates={excludeDates}
2432+
showMonthYearPicker
2433+
/>,
2434+
);
2435+
expect(
2436+
container.querySelector(
2437+
".react-datepicker__month-text--keyboard-selected",
2438+
),
2439+
).toBeNull();
2440+
});
2441+
2442+
it("should not apply the keyboard-selected class when the quarter is a part of disabled dates", () => {
2443+
const currentSelectedDate = newDate("2023-08-08");
2444+
const maxDate = newDate("2024-08-03");
2445+
2446+
const { container } = render(
2447+
<DatePicker
2448+
selected={currentSelectedDate}
2449+
maxDate={maxDate}
2450+
dateFormat="yyyy, QQQ"
2451+
showQuarterYearPicker
2452+
/>,
2453+
);
2454+
const dateInput = container.querySelector("input")!;
2455+
fireEvent.focus(dateInput);
2456+
2457+
const calendar = container.querySelector(".react-datepicker")!;
2458+
const nextButton = calendar.querySelector(
2459+
".react-datepicker__navigation--next",
2460+
)!;
2461+
fireEvent.click(nextButton);
2462+
2463+
expect(
2464+
container.querySelector(
2465+
".react-datepicker__quarter-text--keyboard-selected",
2466+
),
2467+
).toBeNull();
2468+
});
2469+
});
24212470
});

src/test/year_picker_test.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,35 @@ describe("YearPicker", () => {
603603
);
604604
expect(allPreselectedYears.length).toBe(1);
605605
});
606+
607+
it("should not set the key-selected class when the year is a part of disabled dates", () => {
608+
const date = newDate("2024-06-01");
609+
const excludeDates = [newDate("2036-05-05")];
610+
611+
const { container } = render(
612+
<DatePicker
613+
selected={date}
614+
excludeDates={excludeDates}
615+
showYearPicker
616+
dateFormat="yyyy"
617+
/>,
618+
);
619+
620+
const dateInput = container.querySelector("input")!;
621+
fireEvent.focus(dateInput);
622+
623+
const calendar = container.querySelector(".react-datepicker")!;
624+
const nextButton = calendar.querySelector(
625+
".react-datepicker__navigation--next",
626+
)!;
627+
fireEvent.click(nextButton);
628+
629+
expect(
630+
container.querySelector(
631+
".react-datepicker__year-text--keyboard-selected",
632+
),
633+
).toBeNull();
634+
});
606635
});
607636

608637
describe("Keyboard navigation", () => {

src/year.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,21 @@ export default class Year extends Component<YearProps> {
222222
) {
223223
return;
224224
}
225+
226+
const { minDate, maxDate, excludeDates, includeDates, filterDate } =
227+
this.props;
228+
225229
const date = getStartOfYear(setYear(this.props.date, y));
230+
const isDisabled =
231+
(minDate || maxDate || excludeDates || includeDates || filterDate) &&
232+
isYearDisabled(y, this.props);
233+
226234
return (
227235
!this.props.disabledKeyboardNavigation &&
228236
!this.props.inline &&
229237
!isSameDay(date, getStartOfYear(this.props.selected)) &&
230-
isSameDay(date, getStartOfYear(this.props.preSelection))
238+
isSameDay(date, getStartOfYear(this.props.preSelection)) &&
239+
!isDisabled
231240
);
232241
};
233242

0 commit comments

Comments
 (0)