Skip to content

Commit 63760c5

Browse files
committed
Fix range selection logic in DatePicker for swapRange functionality
1 parent 22510f2 commit 63760c5

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/day.tsx

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

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

347347
if (selectsStart) {
348348
return isSameDay(day, selectingDate);
349-
} else {
350-
return isSameDay(day, startDate);
351349
}
350+
351+
if (selectsRange && swapRange && startDate && selectingDate) {
352+
return isSameDay(day, isBefore(selectingDate, startDate) ? selectingDate : startDate);
353+
}
354+
355+
return isSameDay(day, startDate);
352356
};
353357

354358
isSelectingRangeEnd = () => {
355359
if (!this.isInSelectingRange()) {
356360
return false;
357361
}
358362

359-
const { day, endDate, selectsEnd, selectsRange } = this.props;
363+
const { day, endDate, selectsEnd, selectsRange, swapRange, startDate } = this.props;
360364
const selectingDate = this.props.selectingDate ?? this.props.preSelection;
361365

362-
if (selectsEnd || selectsRange) {
366+
if (selectsEnd) {
363367
return isSameDay(day, selectingDate);
364-
} else {
365-
return isSameDay(day, endDate);
366368
}
369+
370+
if (selectsRange && swapRange && startDate && selectingDate) {
371+
return isSameDay(day, isBefore(selectingDate, startDate) ? startDate : selectingDate);
372+
}
373+
374+
if (selectsRange) {
375+
return isSameDay(day, selectingDate);
376+
}
377+
378+
return isSameDay(day, endDate);
367379
};
368380

369381
isRangeStart = () => {

src/month.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,6 @@ export default class Month extends Component<MonthProps> {
11011101
? ariaLabelPrefix.trim() + " "
11021102
: "";
11031103

1104-
console.log('Month render:', this.props.selectingDate);
1105-
11061104
return (
11071105
<div
11081106
className={this.getClassNames()}

src/test/day_test.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ describe("Day", () => {
898898
});
899899

900900
describe("for a date picker with selectsRange and swapRange prop", () => {
901-
it("should select range from startDate to selectingDate if selectingDate mote then startDate", () => {
901+
it("should select range from startDate to selectingDate if selectingDate more than startDate", () => {
902902
const startDate = newDate();
903903
const dayInRange = addDays(startDate, 1);
904904
const selectingDate = addDays(startDate, 2);
@@ -916,7 +916,7 @@ describe("Day", () => {
916916
).toBe(true);
917917
})
918918

919-
it("should select range from selectingDate to startDate if selectingDate less then startDate", () => {
919+
it("should select range from selectingDate to startDate if selectingDate less than startDate", () => {
920920
const startDate = newDate();
921921
const dayInRange = subDays(startDate, 1);
922922
const selectingDate = subDays(startDate, 2);

0 commit comments

Comments
 (0)