mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
ce6750ff02
Require a date and time when creating activity logs, allow minute-level selection, and improve date-picker scrolling within dialogs.
143 lines
6.0 KiB
TypeScript
143 lines
6.0 KiB
TypeScript
"use client"
|
|
|
|
import { useMemo, useState } from "react";
|
|
import { CalendarIcon } from "@radix-ui/react-icons";
|
|
import { format } from "date-fns";
|
|
import { cn } from "@/lib/utils";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Calendar } from "@/components/ui/calendar";
|
|
import {
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
} from "@/components/ui/popover";
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
import type { Matcher } from "react-day-picker";
|
|
|
|
interface DateTimePickerProps {
|
|
date: Date | undefined;
|
|
setDate: (date: Date | undefined) => void;
|
|
placeholder?: string;
|
|
disabled?: boolean;
|
|
disabledDate?: Matcher | Matcher[] | undefined;
|
|
captionLayout?: "label" | "dropdown" | "dropdown-months" | "dropdown-years";
|
|
formatDate?: string;
|
|
}
|
|
|
|
export function DateTimePicker({ date, setDate, placeholder, disabled, disabledDate, captionLayout, formatDate }: DateTimePickerProps) {
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
const hours = Array.from({ length: 24 }, (_, i) => i).reverse();
|
|
const minutes = Array.from({ length: 60 }, (_, i) => i * 1);
|
|
|
|
const parsedDate = useMemo(() => {
|
|
if (!date) return undefined;
|
|
if (typeof date === "string") return new Date(date);
|
|
return date;
|
|
}, [date]);
|
|
|
|
const handleDateSelect = (selectedDate: Date | undefined) => {
|
|
if (selectedDate) {
|
|
if (parsedDate) {
|
|
selectedDate.setHours(parsedDate.getHours());
|
|
selectedDate.setMinutes(parsedDate.getMinutes());
|
|
}
|
|
setDate(selectedDate);
|
|
}
|
|
};
|
|
|
|
const handleTimeChange = (type: "hour" | "minute", value: string) => {
|
|
if (date) {
|
|
const current = parsedDate || new Date();
|
|
const newDate = new Date(current);
|
|
|
|
if (type === "hour") {
|
|
newDate.setHours(parseInt(value));
|
|
} else if (type === "minute") {
|
|
newDate.setMinutes(parseInt(value));
|
|
}
|
|
setDate(newDate);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Popover open={isOpen} onOpenChange={setIsOpen}>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
variant={"outline"} disabled={disabled}
|
|
className={cn(
|
|
"w-full justify-start text-left font-normal",
|
|
!parsedDate && "text-muted-foreground"
|
|
)}
|
|
>
|
|
<CalendarIcon className="mr-2 h-4 w-4" />
|
|
{parsedDate ?
|
|
format(parsedDate, formatDate || "yyyy-MM-dd HH:mm")
|
|
:
|
|
<span>{placeholder || "YYYY-MM-DD HH:mm"}</span>
|
|
}
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent
|
|
className="w-auto p-0"
|
|
onWheel={(e) => e.stopPropagation()}
|
|
onTouchMove={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="sm:flex">
|
|
<Calendar
|
|
mode="single"
|
|
captionLayout={captionLayout || 'dropdown'}
|
|
disabled={disabledDate}
|
|
selected={parsedDate}
|
|
defaultMonth={parsedDate}
|
|
onSelect={handleDateSelect}
|
|
initialFocus
|
|
endMonth={new Date(2050, 11)}
|
|
/>
|
|
<div className="flex flex-col sm:flex-row sm:h-75 divide-y sm:divide-y-0 sm:divide-x">
|
|
<ScrollArea className="w-64 sm:w-auto sm:h-full">
|
|
<div className="flex sm:flex-col p-2">
|
|
{hours.map((hour) => (
|
|
<Button
|
|
type="button"
|
|
key={hour}
|
|
size="icon"
|
|
variant={parsedDate && parsedDate?.getHours() === hour ? "default" : "ghost"}
|
|
className="sm:w-full shrink-0 aspect-square"
|
|
onClick={() => handleTimeChange("hour", hour.toString())}
|
|
>
|
|
{hour}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
<ScrollBar orientation="horizontal" className="sm:hidden" />
|
|
</ScrollArea>
|
|
<ScrollArea className="w-64 sm:w-auto sm:h-full">
|
|
<div className="flex sm:flex-col p-2">
|
|
{minutes.map((minute) => (
|
|
<Button
|
|
type="button"
|
|
key={minute}
|
|
size="icon"
|
|
variant={parsedDate && parsedDate?.getMinutes() === minute ? "default" : "ghost"}
|
|
className="sm:w-full shrink-0 aspect-square"
|
|
onClick={() => handleTimeChange("minute", minute.toString())}
|
|
>
|
|
{minute.toString().padStart(2, "0")}
|
|
</Button>
|
|
))}
|
|
</div>
|
|
<ScrollBar orientation="horizontal" className="sm:hidden" />
|
|
</ScrollArea>
|
|
</div>
|
|
</div>
|
|
{/* --- ADDED FOOTER HERE --- */}
|
|
<div className="p-3 border-t border-border flex justify-end">
|
|
<Button onClick={() => setIsOpen(false)} type="button">
|
|
Done
|
|
</Button>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
} |