Skip to content

Commit 07d369a

Browse files
review: fix datesAreWithinMaxRange, add it to utils, add tests
1 parent b082866 commit 07d369a

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/components/DatePicker/DateRangePicker.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Body, CalendarRenderer, DateRangePickerInput, DateTableCell } from "./C
1313
import { Container } from "../Container/Container";
1414
import { Panel } from "../Panel/Panel";
1515
import { Icon } from "../Icon/Icon";
16-
import { DateRange, selectedDateFormatter } from "./utils";
16+
import { DateRange, datesAreWithinMaxRange, selectedDateFormatter } from "./utils";
1717

1818
const PredefinedCalendarContainer = styled(Panel)`
1919
align-items: start;
@@ -57,17 +57,6 @@ const DateRangeTableCell = styled(DateTableCell)<{
5757
`}
5858
`;
5959

60-
const datesAreWithinMaxRange = (
61-
startDate: Date,
62-
endDate: Date,
63-
maxRangeLength: number
64-
): boolean => {
65-
const daysDifference =
66-
Math.abs(startDate.getTime() - endDate.getTime()) / (1000 * 60 * 60 * 24);
67-
68-
return daysDifference >= maxRangeLength;
69-
};
70-
7160
interface CalendarProps {
7261
calendarBody: Body;
7362
closeDatepicker: () => void;
@@ -122,7 +111,7 @@ const Calendar = ({
122111
if (
123112
maxRangeLength > 1 &&
124113
startDate &&
125-
datesAreWithinMaxRange(startDate, fullDate, maxRangeLength)
114+
!datesAreWithinMaxRange(startDate, fullDate, maxRangeLength)
126115
) {
127116
isDisabled = true;
128117
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { datesAreWithinMaxRange } from "./utils";
2+
3+
describe("DatePicker utils", () => {
4+
describe("checking if two dates are fall within a range", () => {
5+
it("returns true if the two dates are within the range", () => {
6+
const date1 = new Date("07-01-2025");
7+
const date2 = new Date("07-08-2025");
8+
9+
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeTruthy();
10+
});
11+
12+
it("returns false if the two dates are not within the range", () => {
13+
const date1 = new Date("07-01-2025");
14+
const date2 = new Date("07-31-2025");
15+
16+
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeFalsy();
17+
})
18+
19+
it("is inclusive with dates", () => {
20+
const date1 = new Date("07-01-2025");
21+
const date2 = new Date("07-16-2025");
22+
23+
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeTruthy();
24+
})
25+
});
26+
});

src/components/DatePicker/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ export const getPredefinedMonthsByNumber = (numberOfMonths: number): Array<DateR
4949

5050
return nextSixMonths;
5151
};
52+
53+
export const datesAreWithinMaxRange = (
54+
startDate: Date,
55+
endDate: Date,
56+
maxRangeLength: number
57+
): boolean => {
58+
const daysDifference = Math.abs(dayjs(startDate).diff(dayjs(endDate), "days"));
59+
60+
return daysDifference <= maxRangeLength;
61+
};

0 commit comments

Comments
 (0)