Files
hats-app/components/ui/datepicker-multi-mode.tsx
T
Ichsan Kurniawan 80cb73f7fa Add FilterModal and DatePickerMultiMode; UI fixes
Introduce a FilterModal component and a DatePickerMultiMode UI component; refactor FilterComponent to use the modal and expose new hide* props for finer control. Update HomeTask to use a Select for view switching and remove unused props. Improve client and assignment UIs: ClientGrid empty state, client card actions, AssignmentTable empty state, and various text/label adjustments. Fix PieChart null-safety with optional chaining and remove a stray console.log in ClientDetailComponent. Update assignment field mappings to prefer wo_* values where appropriate and pass hideCategory to project detail page. Also update .env.development API/IMG URLs.
2026-04-23 21:01:12 +07:00

155 lines
4.7 KiB
TypeScript

"use client"
import { 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 type { Matcher, DateRange } from "react-day-picker";
// 1. Base props shared across all modes
type BaseProps = {
placeholder?: string;
disabled?: boolean;
disabledDate?: Matcher | Matcher[] | undefined;
captionLayout?: "label" | "dropdown" | "dropdown-months" | "dropdown-years";
formatDate?: string;
className?: string;
};
// 2. Strict typings for each specific mode to avoid `any`
export type DatePickerSingleProps = BaseProps & {
mode?: "single";
date: Date | undefined;
setDate: (date: Date | undefined) => void;
};
export type DatePickerMultipleProps = BaseProps & {
mode: "multiple";
date: Date[] | undefined;
setDate: (date: Date[] | undefined) => void;
};
export type DatePickerRangeProps = BaseProps & {
mode: "range";
date: DateRange | undefined;
setDate: (date: DateRange | undefined) => void;
};
export type DatePickerProps =
| DatePickerSingleProps
| DatePickerMultipleProps
| DatePickerRangeProps;
export function DatePickerMultiMode(props: DatePickerProps) {
const {
placeholder,
disabled,
disabledDate,
captionLayout,
formatDate = "PPP",
className
} = props;
const [open, setOpen] = useState(false);
// Dynamic text rendering based on the active mode
const renderTriggerText = () => {
if (!props.date) return <span>{placeholder || "Pick a date"}</span>;
if (props.mode === "single" || !props.mode) {
return format(props.date as Date, formatDate);
}
if (props.mode === "multiple") {
const dates = props.date as Date[];
if (dates.length === 0) return <span>{placeholder || "Pick a date"}</span>;
if (dates.length === 1) return format(dates[0], formatDate);
if (dates.length <= 2) return dates.map(d => format(d, formatDate)).join(", ");
return <span>{dates.length} dates selected</span>;
}
if (props.mode === "range") {
const rangeDate = props.date as DateRange;
if (rangeDate.from) {
if (rangeDate.to) {
return (
<>
{format(rangeDate.from, formatDate)} - {format(rangeDate.to, formatDate)}
</>
);
}
return format(rangeDate.from, formatDate);
}
}
return <span>{placeholder || "Pick a date"}</span>;
};
// Conditional Calendar rendering to appease react-day-picker strict types
const renderCalendar = () => {
const commonProps = {
captionLayout: captionLayout || "dropdown",
disabled: disabledDate,
initialFocus: true,
endMonth: new Date(2050, 11),
};
if (props.mode === "multiple") {
return (
<Calendar
{...commonProps}
mode="multiple"
selected={props.date}
onSelect={props.setDate}
/>
);
}
if (props.mode === "range") {
return (
<Calendar
{...commonProps}
mode="range"
selected={props.date}
onSelect={props.setDate}
/>
);
}
// Default to single
return (
<Calendar
{...commonProps}
mode="single"
selected={props.date}
defaultMonth={props.date}
onSelect={(selectedDate) => {
props.setDate(selectedDate);
setOpen(false); // Only close automatically on single select mode
}}
/>
);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button variant={"outline"} disabled={disabled}
className={cn("w-full justify-start text-left font-normal", !props.date && "text-muted-foreground", className)}>
<CalendarIcon className="mr-2 h-4 w-4" />
{renderTriggerText()}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
{renderCalendar()}
</PopoverContent>
</Popover>
);
}