Skip to content

Commit 5855bba

Browse files
ability to go forward or backwards
1 parent 9cf6968 commit 5855bba

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/components/DatePicker/DateRangePicker.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,32 @@ interface DateRange {
141141
endDate: Date;
142142
}
143143

144-
const getLastMonths = (numberOfMonths: number = 6): Array<DateRange> => {
144+
const getMonthsByNumber = (numberOfMonths: number): Array<DateRange> => {
145145
const now = dayjs();
146146

147-
const lastSixMonths: Array<DateRange> = [];
147+
if (numberOfMonths < 0) {
148+
const lastSixMonths: Array<DateRange> = [];
149+
for (let i = 0; i < Math.abs(numberOfMonths); i++) {
150+
const date = now.subtract(i, "month");
151+
lastSixMonths.push({
152+
startDate: date.startOf("month").toDate(),
153+
endDate: i === 0 ? now.toDate() : date.endOf("month").toDate(),
154+
});
155+
}
156+
157+
return lastSixMonths;
158+
}
159+
160+
const nextSixMonths: Array<DateRange> = [];
148161
for (let i = 0; i < numberOfMonths; i++) {
149-
const date = now.subtract(i, "month");
150-
lastSixMonths.push({
162+
const date = now.add(i, "month");
163+
nextSixMonths.push({
151164
startDate: date.startOf("month").toDate(),
152165
endDate: i === 0 ? now.toDate() : date.endOf("month").toDate(),
153166
});
154167
}
155168

156-
return lastSixMonths;
169+
return nextSixMonths;
157170
};
158171

159172
interface PredefinedDatesProps {
@@ -177,7 +190,7 @@ const PredefinedDates = ({
177190
shouldShowCustomRange,
178191
showCustomDateRange,
179192
}: PredefinedDatesProps) => {
180-
const pastSixMonths = getLastMonths(predefinedDatesCount);
193+
const pastSixMonths = getMonthsByNumber(predefinedDatesCount);
181194

182195
const handleCustomTimePeriodClick = (event: MouseEvent) => {
183196
event.preventDefault();
@@ -318,6 +331,18 @@ export const DateRangePicker = ({
318331
[onSelectDateRange, selectedEndDate, selectedStartDate]
319332
);
320333

334+
const shouldShowPredefinedDates =
335+
predefinedDatesCount !== undefined && predefinedDatesCount !== 0;
336+
337+
let clampedPredefinedDatesCount = 0;
338+
if (shouldShowPredefinedDates) {
339+
if (predefinedDatesCount > 0) {
340+
clampedPredefinedDatesCount = Math.min(6, predefinedDatesCount);
341+
} else {
342+
clampedPredefinedDatesCount = Math.max(-6, predefinedDatesCount);
343+
}
344+
}
345+
321346
return (
322347
<Dropdown
323348
onOpenChange={handleOpenChange}
@@ -334,14 +359,14 @@ export const DateRangePicker = ({
334359
/>
335360
</Dropdown.Trigger>
336361
<Dropdown.Content align="start">
337-
{predefinedDatesCount !== undefined && predefinedDatesCount > 0 ? (
362+
{shouldShowPredefinedDates ? (
338363
<Panel
339364
orientation="horizontal"
340365
padding="none"
341366
>
342367
<PredefinedDates
343368
onSelectDateRange={onSelectDateRange}
344-
predefinedDatesCount={Math.min(6, predefinedDatesCount)}
369+
predefinedDatesCount={clampedPredefinedDatesCount}
345370
selectedEndDate={selectedEndDate}
346371
selectedStartDate={selectedStartDate}
347372
setEndDate={setSelectedEndDate}

0 commit comments

Comments
 (0)