feat: replace raw date/time inputs with shadcn Calendar + Select picker
- Add popover shadcn component (radix-ui based) - Create DateTimePicker component: Calendar popover for date + hour/minute selects for time, allDay-aware, emits ISO / YYYY-MM-DD strings - Replace datetime-local / date <Input> fields in EventDialog with DateTimePicker - Remove unused CalendarIcon and Clock imports from event-dialog
This commit is contained in:
171
src/components/date-time-picker.tsx
Normal file
171
src/components/date-time-picker.tsx
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { format, isValid, parse, parseISO } from "date-fns";
|
||||||
|
import { CalendarIcon } from "lucide-react";
|
||||||
|
import * as React from "react";
|
||||||
|
import { Calendar } from "@/components/ui/calendar";
|
||||||
|
import {
|
||||||
|
Popover,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverTrigger,
|
||||||
|
} from "@/components/ui/popover";
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "@/components/ui/select";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
|
interface DateTimePickerProps {
|
||||||
|
value: string;
|
||||||
|
onChange: (value: string) => void;
|
||||||
|
allDay: boolean;
|
||||||
|
placeholder?: string;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parse the incoming ISO / date string into a Date object, or return undefined. */
|
||||||
|
function parseValue(value: string): Date | undefined {
|
||||||
|
if (!value) return undefined;
|
||||||
|
// Try ISO 8601 with offset (e.g. "2024-01-15T14:30:00+00:00")
|
||||||
|
const iso = parseISO(value);
|
||||||
|
if (isValid(iso)) return iso;
|
||||||
|
// Try plain date "YYYY-MM-DD"
|
||||||
|
const plain = parse(value, "yyyy-MM-dd", new Date());
|
||||||
|
if (isValid(plain)) return plain;
|
||||||
|
// Try datetime-local "YYYY-MM-DDTHH:mm"
|
||||||
|
const local = parse(value, "yyyy-MM-dd'T'HH:mm", new Date());
|
||||||
|
if (isValid(local)) return local;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Emit ISO date string from Date + time parts depending on allDay mode. */
|
||||||
|
function buildValue(
|
||||||
|
date: Date,
|
||||||
|
hour: number,
|
||||||
|
minute: number,
|
||||||
|
allDay: boolean,
|
||||||
|
): string {
|
||||||
|
if (allDay) {
|
||||||
|
return format(date, "yyyy-MM-dd");
|
||||||
|
}
|
||||||
|
const hh = String(hour).padStart(2, "0");
|
||||||
|
const mm = String(minute).padStart(2, "0");
|
||||||
|
return `${format(date, "yyyy-MM-dd")}T${hh}:${mm}:00`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const HOURS = Array.from({ length: 24 }, (_, i) => i);
|
||||||
|
const MINUTES = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
|
||||||
|
|
||||||
|
export function DateTimePicker({
|
||||||
|
value,
|
||||||
|
onChange,
|
||||||
|
allDay,
|
||||||
|
placeholder = "Pick a date",
|
||||||
|
className,
|
||||||
|
}: DateTimePickerProps) {
|
||||||
|
const parsed = parseValue(value);
|
||||||
|
|
||||||
|
// Derive hour/minute from the current value (fallback to 0:00)
|
||||||
|
const currentHour = parsed && !allDay ? parsed.getHours() : 0;
|
||||||
|
const currentMinute = parsed && !allDay ? parsed.getMinutes() : 0;
|
||||||
|
|
||||||
|
// Snap current minute to nearest MINUTES bucket for the select
|
||||||
|
const snappedMinute = MINUTES.reduce((prev, curr) =>
|
||||||
|
Math.abs(curr - currentMinute) < Math.abs(prev - currentMinute)
|
||||||
|
? curr
|
||||||
|
: prev,
|
||||||
|
);
|
||||||
|
|
||||||
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
|
const handleDaySelect = (day: Date | undefined) => {
|
||||||
|
if (!day) return;
|
||||||
|
onChange(buildValue(day, currentHour, snappedMinute, allDay));
|
||||||
|
setOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleHourChange = (h: string) => {
|
||||||
|
const base = parsed ?? new Date();
|
||||||
|
onChange(buildValue(base, Number(h), snappedMinute, allDay));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMinuteChange = (m: string) => {
|
||||||
|
const base = parsed ?? new Date();
|
||||||
|
onChange(buildValue(base, currentHour, Number(m), allDay));
|
||||||
|
};
|
||||||
|
|
||||||
|
const displayLabel = parsed
|
||||||
|
? allDay
|
||||||
|
? format(parsed, "MMM d, yyyy")
|
||||||
|
: format(parsed, "MMM d, yyyy · HH:mm")
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("flex items-center gap-2", className)}>
|
||||||
|
{/* Date popover trigger */}
|
||||||
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
|
<PopoverTrigger asChild>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={cn(
|
||||||
|
"flex h-9 flex-1 items-center gap-2 rounded-md border border-input bg-transparent px-3 py-1 text-sm",
|
||||||
|
"ring-offset-background transition-colors",
|
||||||
|
"hover:bg-accent/30 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring",
|
||||||
|
!displayLabel && "text-muted-foreground",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<CalendarIcon className="h-3.5 w-3.5 shrink-0 text-muted-foreground/70" />
|
||||||
|
<span className="flex-1 truncate text-left">
|
||||||
|
{displayLabel ?? placeholder}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent className="w-auto p-0" align="start" sideOffset={8}>
|
||||||
|
<Calendar
|
||||||
|
mode="single"
|
||||||
|
selected={parsed}
|
||||||
|
onSelect={handleDaySelect}
|
||||||
|
initialFocus
|
||||||
|
/>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
|
||||||
|
{/* Time selects — only when !allDay */}
|
||||||
|
{!allDay && (
|
||||||
|
<div className="flex items-center gap-1">
|
||||||
|
<Select value={String(currentHour)} onValueChange={handleHourChange}>
|
||||||
|
<SelectTrigger className="h-9 w-[62px] px-2 text-xs">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent className="max-h-56">
|
||||||
|
{HOURS.map((h) => (
|
||||||
|
<SelectItem key={h} value={String(h)} className="text-xs">
|
||||||
|
{String(h).padStart(2, "0")}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
<span className="text-muted-foreground text-xs select-none">:</span>
|
||||||
|
<Select
|
||||||
|
value={String(snappedMinute)}
|
||||||
|
onValueChange={handleMinuteChange}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="h-9 w-[62px] px-2 text-xs">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
{MINUTES.map((m) => (
|
||||||
|
<SelectItem key={m} value={String(m)} className="text-xs">
|
||||||
|
{String(m).padStart(2, "0")}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { CalendarIcon, Clock, LucideMapPin } from "lucide-react";
|
import { LucideMapPin } from "lucide-react";
|
||||||
|
import { DateTimePicker } from "@/components/date-time-picker";
|
||||||
import { RecurrencePicker } from "@/components/recurrence-picker";
|
import { RecurrencePicker } from "@/components/recurrence-picker";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Checkbox } from "@/components/ui/checkbox";
|
import { Checkbox } from "@/components/ui/checkbox";
|
||||||
@@ -139,44 +140,18 @@ export const EventDialog = ({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<div className="relative">
|
<DateTimePicker
|
||||||
<CalendarIcon className="absolute left-3 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground/50 pointer-events-none" />
|
|
||||||
{!allDay ? (
|
|
||||||
<Input
|
|
||||||
type="datetime-local"
|
|
||||||
value={start}
|
value={start}
|
||||||
onChange={(e) => setStart(e.target.value)}
|
onChange={setStart}
|
||||||
className="pl-8"
|
allDay={allDay}
|
||||||
placeholder="Start"
|
placeholder="Start date"
|
||||||
/>
|
/>
|
||||||
) : (
|
<DateTimePicker
|
||||||
<Input
|
|
||||||
type="date"
|
|
||||||
value={start ? start.split("T")[0] : ""}
|
|
||||||
onChange={(e) => setStart(e.target.value)}
|
|
||||||
className="pl-8"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<div className="relative">
|
|
||||||
<Clock className="absolute left-3 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-muted-foreground/50 pointer-events-none" />
|
|
||||||
{!allDay ? (
|
|
||||||
<Input
|
|
||||||
type="datetime-local"
|
|
||||||
value={end}
|
value={end}
|
||||||
onChange={(e) => setEnd(e.target.value)}
|
onChange={setEnd}
|
||||||
className="pl-8"
|
allDay={allDay}
|
||||||
placeholder="End"
|
placeholder="End date"
|
||||||
/>
|
/>
|
||||||
) : (
|
|
||||||
<Input
|
|
||||||
type="date"
|
|
||||||
value={end ? end.split("T")[0] : ""}
|
|
||||||
onChange={(e) => setEnd(e.target.value)}
|
|
||||||
className="pl-8"
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
89
src/components/ui/popover.tsx
Normal file
89
src/components/ui/popover.tsx
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Popover as PopoverPrimitive } from "radix-ui";
|
||||||
|
import type * as React from "react";
|
||||||
|
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
|
function Popover({
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PopoverPrimitive.Root>) {
|
||||||
|
return <PopoverPrimitive.Root data-slot="popover" {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverTrigger({
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PopoverPrimitive.Trigger>) {
|
||||||
|
return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverContent({
|
||||||
|
className,
|
||||||
|
align = "center",
|
||||||
|
sideOffset = 4,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PopoverPrimitive.Content>) {
|
||||||
|
return (
|
||||||
|
<PopoverPrimitive.Portal>
|
||||||
|
<PopoverPrimitive.Content
|
||||||
|
data-slot="popover-content"
|
||||||
|
align={align}
|
||||||
|
sideOffset={sideOffset}
|
||||||
|
className={cn(
|
||||||
|
"z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-hidden data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95",
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
</PopoverPrimitive.Portal>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverAnchor({
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PopoverPrimitive.Anchor>) {
|
||||||
|
return <PopoverPrimitive.Anchor data-slot="popover-anchor" {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverHeader({ className, ...props }: React.ComponentProps<"div">) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-slot="popover-header"
|
||||||
|
className={cn("flex flex-col gap-1 text-sm", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverTitle({ className, ...props }: React.ComponentProps<"h2">) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
data-slot="popover-title"
|
||||||
|
className={cn("font-medium", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PopoverDescription({
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<"p">) {
|
||||||
|
return (
|
||||||
|
<p
|
||||||
|
data-slot="popover-description"
|
||||||
|
className={cn("text-muted-foreground", className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
Popover,
|
||||||
|
PopoverAnchor,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverDescription,
|
||||||
|
PopoverHeader,
|
||||||
|
PopoverTitle,
|
||||||
|
PopoverTrigger,
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user