"use client";
import { useState, useMemo } from "react";
import {
Calendar as CalendarIcon,
ChevronLeft,
ChevronRight,
Check,
X,
} from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Calendar } from "@/components/ui/calendar";
import { Separator } from "@/components/ui/separator";
import { cn } from "@/lib/utils";
import { DateRange } from "react-day-picker";
type PresetRange = {
id: string;
label: string;
getValue: () => DateRange;
};
const getPresetRanges = (): PresetRange[] => {
const today = new Date();
const startOfToday = new Date(today.getFullYear(), today.getMonth(), today.getDate());
return [
{
id: "today",
label: "Today",
getValue: () => ({ from: startOfToday, to: startOfToday }),
},
{
id: "yesterday",
label: "Yesterday",
getValue: () => {
const yesterday = new Date(startOfToday);
yesterday.setDate(yesterday.getDate() - 1);
return { from: yesterday, to: yesterday };
},
},
{
id: "last7",
label: "Last 7 days",
getValue: () => {
const from = new Date(startOfToday);
from.setDate(from.getDate() - 6);
return { from, to: startOfToday };
},
},
{
id: "last30",
label: "Last 30 days",
getValue: () => {
const from = new Date(startOfToday);
from.setDate(from.getDate() - 29);
return { from, to: startOfToday };
},
},
{
id: "thisMonth",
label: "This month",
getValue: () => {
const from = new Date(today.getFullYear(), today.getMonth(), 1);
return { from, to: startOfToday };
},
},
{
id: "lastMonth",
label: "Last month",
getValue: () => {
const from = new Date(today.getFullYear(), today.getMonth() - 1, 1);
const to = new Date(today.getFullYear(), today.getMonth(), 0);
return { from, to };
},
},
{
id: "thisQuarter",
label: "This quarter",
getValue: () => {
const quarter = Math.floor(today.getMonth() / 3);
const from = new Date(today.getFullYear(), quarter * 3, 1);
return { from, to: startOfToday };
},
},
{
id: "thisYear",
label: "This year",
getValue: () => {
const from = new Date(today.getFullYear(), 0, 1);
return { from, to: startOfToday };
},
},
];
};
export const title = "React Dialog Block Date Range Picker";
export default function DialogDateRangePicker() {
const [open, setOpen] = useState(false);
const [dateRange, setDateRange] = useState<DateRange | undefined>({
from: new Date(new Date().setDate(new Date().getDate() - 6)),
to: new Date(),
});
const [selectedPreset, setSelectedPreset] = useState<string>("last7");
const [month, setMonth] = useState<Date>(new Date());
const presetRanges = useMemo(() => getPresetRanges(), []);
const formatDate = (date: Date): string => {
return new Intl.DateTimeFormat("en-US", {
month: "short",
day: "numeric",
year: "numeric",
}).format(date);
};
const getDaysDifference = (from: Date, to: Date): number => {
const diffTime = Math.abs(to.getTime() - from.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1;
};
const handlePresetClick = (preset: PresetRange) => {
const range = preset.getValue();
setDateRange(range);
setSelectedPreset(preset.id);
if (range.from) {
setMonth(range.from);
}
};
const handleCalendarSelect = (range: DateRange | undefined) => {
setDateRange(range);
setSelectedPreset("");
};
const handleApply = () => {
// In real app: apply the date range filter
setOpen(false);
};
const handleClear = () => {
setDateRange(undefined);
setSelectedPreset("");
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<div className="flex min-h-[350px] items-center justify-center">
<DialogTrigger asChild>
<Button variant="outline">
<CalendarIcon className="mr-2 h-4 w-4" />
{dateRange?.from ? (
dateRange.to ? (
<>
{formatDate(dateRange.from)} - {formatDate(dateRange.to)}
</>
) : (
formatDate(dateRange.from)
)
) : (
"Select date range"
)}
</Button>
</DialogTrigger>
</div>
<DialogContent className="sm:max-w-fit">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<CalendarIcon className="h-5 w-5" />
Select Date Range
</DialogTitle>
<DialogDescription>
Choose a preset range or select custom dates from the calendar.
</DialogDescription>
</DialogHeader>
<div className="flex gap-4 py-4">
{/* Preset Ranges */}
<div className="w-40 space-y-1">
<p className="text-xs font-medium text-muted-foreground mb-2">
Quick Select
</p>
{presetRanges.map((preset) => (
<button
key={preset.id}
onClick={() => handlePresetClick(preset)}
className={cn(
"w-full text-left px-3 py-2 text-sm rounded-md transition-colors",
selectedPreset === preset.id
? "bg-primary text-primary-foreground"
: "hover:bg-muted"
)}
>
{preset.label}
</button>
))}
</div>
<Separator orientation="vertical" className="h-auto" />
{/* Calendar */}
<div className="flex-1">
<Calendar
mode="range"
selected={dateRange}
onSelect={handleCalendarSelect}
month={month}
onMonthChange={setMonth}
numberOfMonths={2}
className="rounded-md border"
/>
{/* Selected Range Summary */}
{dateRange?.from && dateRange?.to && (
<div className="mt-4 flex items-center justify-between rounded-lg border p-3">
<div className="space-y-1">
<p className="text-xs text-muted-foreground">Selected range</p>
<p className="text-sm font-medium">
{formatDate(dateRange.from)} - {formatDate(dateRange.to)}
</p>
</div>
<Badge variant="secondary">
{getDaysDifference(dateRange.from, dateRange.to)} days
</Badge>
</div>
)}
</div>
</div>
<DialogFooter className="flex-col sm:flex-row gap-2">
<Button
variant="ghost"
onClick={handleClear}
className="sm:mr-auto"
disabled={!dateRange}
>
<X className="mr-2 h-4 w-4" />
Clear
</Button>
<Button variant="outline" onClick={() => setOpen(false)}>
Cancel
</Button>
<Button onClick={handleApply} disabled={!dateRange?.from || !dateRange?.to}>
<Check className="mr-2 h-4 w-4" />
Apply Range
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}