mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
"use client"
|
|
|
|
import { HTMLAttributes, useState } from "react"
|
|
import { CalendarIcon } from "@radix-ui/react-icons"
|
|
import { addDays, format } from "date-fns"
|
|
import { DateRange } from "react-day-picker"
|
|
|
|
import { cn } from "@/lib/utils"
|
|
import { Button } from "@/components/ui/button"
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
|
|
import { Calendar } from "@/components/ui/calendar"
|
|
|
|
export function CalendarDateRangePicker({ className }: HTMLAttributes<HTMLDivElement>) {
|
|
|
|
const [date, setDate] = useState<DateRange | undefined>({
|
|
from: new Date(2023, 0, 20),
|
|
to: addDays(new Date(2023, 0, 20), 20),
|
|
})
|
|
|
|
return (
|
|
<div className={cn("grid gap-2", className)}>
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<Button id="date" variant={"outline"}
|
|
className={cn(
|
|
"w-full sm:w-[260px] justify-start text-left font-normal",
|
|
!date && "text-muted-foreground"
|
|
)}
|
|
>
|
|
<CalendarIcon className="sm:mr-2 h-4 w-4" />
|
|
<div className="hidden sm:block">
|
|
{date?.from ? (
|
|
date.to ? (
|
|
<>
|
|
{format(date.from, "LLL dd, y")} -{" "}
|
|
{format(date.to, "LLL dd, y")}
|
|
</>
|
|
) : (
|
|
format(date.from, "LLL dd, y")
|
|
)
|
|
) : (
|
|
<span>Pick a date</span>
|
|
)}
|
|
</div>
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-auto p-0" align="end">
|
|
<Calendar
|
|
initialFocus
|
|
mode="range"
|
|
defaultMonth={date?.from}
|
|
selected={date}
|
|
onSelect={setDate}
|
|
numberOfMonths={2}
|
|
/>
|
|
</PopoverContent>
|
|
</Popover>
|
|
</div>
|
|
)
|
|
} |