Date Range Picker
A date range selector with preset buttons, a popover, and a two-month calendar.
Installation
pnpm dlx shadcn@latest add @ecodeliver/date-range-pickerThe component depends on @daypicker/react (v10) and the shadcn Calendar component.
Usage
You supply the preset list. The component applies the selected preset or a custom range from the calendar.
import { useState } from "react";
import type { DateRange } from "@daypicker/react";
import { endOfDay, startOfDay, subDays } from "date-fns";
import { DateRangeSelector } from "@/components/registry/date-range-picker";
export function Example() {
const [range, setRange] = useState(() => {
const to = endOfDay(new Date());
return { from: startOfDay(subDays(to, 13)), to };
});
return (
<DateRangeSelector
defaultPreset="2weeks"
value={range}
presets={[
{ value: "7days", label: "Últimos 7 días", daysBack: 6 },
{ value: "2weeks", label: "Últimas 2 semanas", daysBack: 13 },
{ value: "month", label: "Último mes", daysBack: 29 },
]}
onDateChange={setRange}
/>
);
}Longer windows, for example billing:
<DateRangeSelector
defaultPreset="30days"
value={range}
presets={[
{ value: "30days", label: "Último mes", daysBack: 29 },
{ value: "90days", label: "Últimos 3 meses", daysBack: 89 },
{ value: "365days", label: "Último año", daysBack: 364 },
]}
onDateChange={setRange}
/>Behaviour
The trigger shows the selected range in Spanish (es via date-fns). Presets sit on the left of the popover, the two-month calendar on the right. Presets use startOfDay / endOfDay. Picking dates on the calendar clears the active preset. Dates after today, and more than 13 months back, are disabled.
API Reference
DateRangeSelector
DateRangeSelector is the exported component from date-range-picker.
| Prop | Type | Default |
|---|---|---|
presets | DateRangePreset[] | - |
value | DateRange | undefined | - |
onDateChange | (range: DateRange | undefined) => void | - |
defaultPreset | string | presets[0].value |
DateRangePreset
| Prop | Type | Default |
|---|---|---|
value | string | - |
label | string | - |
daysBack | number | - |