Skip to content

Commit 0b685f0

Browse files
committed
Refactor DatePicker code for improved readability and consistency in range selection logic
1 parent 241e97e commit 0b685f0

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/day.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,19 @@ export default class Day extends Component<DayProps> {
341341
return false;
342342
}
343343

344-
const { day, startDate, selectsStart, swapRange, selectsRange } = this.props;
344+
const { day, startDate, selectsStart, swapRange, selectsRange } =
345+
this.props;
345346
const selectingDate = this.props.selectingDate ?? this.props.preSelection;
346347

347348
if (selectsStart) {
348349
return isSameDay(day, selectingDate);
349350
}
350351

351352
if (selectsRange && swapRange && startDate && selectingDate) {
352-
return isSameDay(day, isBefore(selectingDate, startDate) ? selectingDate : startDate);
353+
return isSameDay(
354+
day,
355+
isBefore(selectingDate, startDate) ? selectingDate : startDate,
356+
);
353357
}
354358

355359
return isSameDay(day, startDate);
@@ -360,15 +364,19 @@ export default class Day extends Component<DayProps> {
360364
return false;
361365
}
362366

363-
const { day, endDate, selectsEnd, selectsRange, swapRange, startDate } = this.props;
367+
const { day, endDate, selectsEnd, selectsRange, swapRange, startDate } =
368+
this.props;
364369
const selectingDate = this.props.selectingDate ?? this.props.preSelection;
365370

366371
if (selectsEnd) {
367372
return isSameDay(day, selectingDate);
368373
}
369374

370375
if (selectsRange && swapRange && startDate && selectingDate) {
371-
return isSameDay(day, isBefore(selectingDate, startDate) ? startDate : selectingDate);
376+
return isSameDay(
377+
day,
378+
isBefore(selectingDate, startDate) ? startDate : selectingDate,
379+
);
372380
}
373381

374382
if (selectsRange) {

src/test/day_test.test.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ describe("Day", () => {
914914
.querySelector(".react-datepicker__day")
915915
?.classList.contains(rangeDayClassName),
916916
).toBe(true);
917-
})
917+
});
918918

919919
it("should select range from selectingDate to startDate if selectingDate is before startDate", () => {
920920
const startDate = newDate();
@@ -936,7 +936,7 @@ describe("Day", () => {
936936

937937
it("should select as range if selectingDate is equal to startDate", () => {
938938
const startDate = newDate();
939-
const selectingDate = startDate
939+
const selectingDate = startDate;
940940

941941
const containerStartDay = renderDay(startDate, {
942942
startDate,
@@ -967,7 +967,6 @@ describe("Day", () => {
967967
?.classList.contains(rangeDayStartClassName),
968968
).toBe(true);
969969

970-
971970
const containerEndDay = renderDay(startDate, {
972971
startDate,
973972
selectingDate,
@@ -980,7 +979,7 @@ describe("Day", () => {
980979
.querySelector(".react-datepicker__day")
981980
?.classList.contains(rangeDayEndClassName),
982981
).toBe(true);
983-
})
982+
});
984983

985984
it("should set selectingDate as the end of range and startDate as the start of range if selectingDate is after startDate", () => {
986985
const startDate = newDate();
@@ -1010,9 +1009,9 @@ describe("Day", () => {
10101009
.querySelector(".react-datepicker__day")
10111010
?.classList.contains(rangeDayEndClassName),
10121011
).toBe(true);
1013-
})
1012+
});
10141013

1015-
it('should set startDate as the end and start range if selectionDate equal startDate', () => {
1014+
it("should set startDate as the end and start range if selectionDate equal startDate", () => {
10161015
const startDate = newDate();
10171016
const selectingDate = startDate;
10181017

@@ -1041,7 +1040,7 @@ describe("Day", () => {
10411040
?.classList.contains(rangeDayEndClassName),
10421041
).toBe(true);
10431042
});
1044-
})
1043+
});
10451044
});
10461045

10471046
describe("today", () => {

0 commit comments

Comments
 (0)