-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathdate-picker.tsx
More file actions
66 lines (61 loc) · 1.6 KB
/
date-picker.tsx
File metadata and controls
66 lines (61 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"use client";
import { RiCalendar2Line as CalendarIcon } from "@remixicon/react";
import * as React from "react";
import { Button } from "@/components/ui/button";
import { Calendar } from "@/components/ui/calendar";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { cn } from "@/lib/utils";
import { format } from "date-fns";
import type { SelectSingleEventHandler } from "react-day-picker";
export type DatePickerProps = {
className?: string;
dateFormat?: string;
disabled?: boolean;
locale?: string;
selected: Date;
onSelect: SelectSingleEventHandler;
placeholder?: string;
};
const DatePicker = ({
className,
dateFormat,
disabled,
locale,
selected,
onSelect,
placeholder,
}: DatePickerProps) => {
return (
<Popover>
<PopoverTrigger asChild>
<Button
variant={"outline"}
className={cn(
"w-full text-left font-normal",
!selected && "text-muted-foreground",
)}
>
{selected ? format(selected, "PPP") : <span>Pick a date</span>}
<CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={new Date(selected)}
onSelect={onSelect}
disabled={(date) => {
if (disabled) return true;
return date > new Date() || date < new Date("1900-01-01");
}}
initialFocus
/>
</PopoverContent>
</Popover>
);
};
export default DatePicker;