Skip to content

Commit da382fb

Browse files
author
Balaji Sridharan
committed
šŸ› Remove the auto set of the '--keyboard-selected' class from the disabled dates from the next or the previous view
Closes #4933
1 parent c8c9f6f commit da382fb

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
@@ -797,7 +797,8 @@ export default class Month extends Component<MonthProps> {
797797
"react-datepicker__month-text--keyboard-selected":
798798
!this.props.disabledKeyboardNavigation &&
799799
preSelection &&
800-
this.isSelectedMonth(day, m, preSelection),
800+
this.isSelectedMonth(day, m, preSelection) &&
801+
!this.isMonthDisabled(m),
801802
"react-datepicker__month-text--in-selecting-range":
802803
this.isInSelectingRangeMonth(m),
803804
"react-datepicker__month-text--in-range":
@@ -865,9 +866,17 @@ export default class Month extends Component<MonthProps> {
865866
selected,
866867
minDate,
867868
maxDate,
869+
excludeDates,
870+
includeDates,
871+
filterDate,
868872
preSelection,
869873
disabledKeyboardNavigation,
870874
} = this.props;
875+
876+
const isDisabled =
877+
(minDate || maxDate || excludeDates || includeDates || filterDate) &&
878+
isQuarterDisabled(setQuarter(day, q), this.props);
879+
871880
return clsx(
872881
"react-datepicker__quarter-text",
873882
`react-datepicker__quarter-${q}`,
@@ -881,7 +890,8 @@ export default class Month extends Component<MonthProps> {
881890
"react-datepicker__quarter-text--keyboard-selected":
882891
!disabledKeyboardNavigation &&
883892
preSelection &&
884-
this.isSelectedQuarter(day, q, preSelection),
893+
this.isSelectedQuarter(day, q, preSelection) &&
894+
!isDisabled,
885895
"react-datepicker__quarter-text--in-selecting-range":
886896
this.isInSelectingRangeQuarter(q),
887897
"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
@@ -2372,4 +2372,53 @@ describe("Month", () => {
23722372
).toBe(false);
23732373
});
23742374
});
2375+
2376+
describe("keyboard-selected", () => {
2377+
it("should not apply the keyboard-selected class when the month is a part of disabled dates", () => {
2378+
const keyboardSelectedDate = newDate("2024-06-03");
2379+
const excludeDates = [addWeeks(keyboardSelectedDate, 1)];
2380+
2381+
const { container } = render(
2382+
<Month
2383+
day={keyboardSelectedDate}
2384+
preSelection={keyboardSelectedDate}
2385+
excludeDates={excludeDates}
2386+
showMonthYearPicker
2387+
/>,
2388+
);
2389+
expect(
2390+
container.querySelector(
2391+
".react-datepicker__month-text--keyboard-selected",
2392+
),
2393+
).toBeNull();
2394+
});
2395+
2396+
it("should not apply the keyboard-selected class when the quarter is a part of disabled dates", () => {
2397+
const currentSelectedDate = newDate("2023-08-08");
2398+
const maxDate = newDate("2024-08-03");
2399+
2400+
const { container } = render(
2401+
<DatePicker
2402+
selected={currentSelectedDate}
2403+
maxDate={maxDate}
2404+
dateFormat="yyyy, QQQ"
2405+
showQuarterYearPicker
2406+
/>,
2407+
);
2408+
const dateInput = container.querySelector("input")!;
2409+
fireEvent.focus(dateInput);
2410+
2411+
const calendar = container.querySelector(".react-datepicker")!;
2412+
const nextButton = calendar.querySelector(
2413+
".react-datepicker__navigation--next",
2414+
)!;
2415+
fireEvent.click(nextButton);
2416+
2417+
expect(
2418+
container.querySelector(
2419+
".react-datepicker__quarter-text--keyboard-selected",
2420+
),
2421+
).toBeNull();
2422+
});
2423+
});
23752424
});

ā€Ž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)