@@ -141,19 +141,32 @@ interface DateRange {
141
141
endDate : Date ;
142
142
}
143
143
144
- const getLastMonths = ( numberOfMonths : number = 6 ) : Array < DateRange > => {
144
+ const getMonthsByNumber = ( numberOfMonths : number ) : Array < DateRange > => {
145
145
const now = dayjs ( ) ;
146
146
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 > = [ ] ;
148
161
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 ( {
151
164
startDate : date . startOf ( "month" ) . toDate ( ) ,
152
165
endDate : i === 0 ? now . toDate ( ) : date . endOf ( "month" ) . toDate ( ) ,
153
166
} ) ;
154
167
}
155
168
156
- return lastSixMonths ;
169
+ return nextSixMonths ;
157
170
} ;
158
171
159
172
interface PredefinedDatesProps {
@@ -177,7 +190,7 @@ const PredefinedDates = ({
177
190
shouldShowCustomRange,
178
191
showCustomDateRange,
179
192
} : PredefinedDatesProps ) => {
180
- const pastSixMonths = getLastMonths ( predefinedDatesCount ) ;
193
+ const pastSixMonths = getMonthsByNumber ( predefinedDatesCount ) ;
181
194
182
195
const handleCustomTimePeriodClick = ( event : MouseEvent ) => {
183
196
event . preventDefault ( ) ;
@@ -318,6 +331,18 @@ export const DateRangePicker = ({
318
331
[ onSelectDateRange , selectedEndDate , selectedStartDate ]
319
332
) ;
320
333
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
+
321
346
return (
322
347
< Dropdown
323
348
onOpenChange = { handleOpenChange }
@@ -334,14 +359,14 @@ export const DateRangePicker = ({
334
359
/>
335
360
</ Dropdown . Trigger >
336
361
< Dropdown . Content align = "start" >
337
- { predefinedDatesCount !== undefined && predefinedDatesCount > 0 ? (
362
+ { shouldShowPredefinedDates ? (
338
363
< Panel
339
364
orientation = "horizontal"
340
365
padding = "none"
341
366
>
342
367
< PredefinedDates
343
368
onSelectDateRange = { onSelectDateRange }
344
- predefinedDatesCount = { Math . min ( 6 , predefinedDatesCount ) }
369
+ predefinedDatesCount = { clampedPredefinedDatesCount }
345
370
selectedEndDate = { selectedEndDate }
346
371
selectedStartDate = { selectedStartDate }
347
372
setEndDate = { setSelectedEndDate }
0 commit comments