Files
hats-app/components/ui/combobox-custom-ui.tsx
Ichsan Kurniawan 6f0d8481a1 Refactor assignment status & assignment form
Normalize project-assignment status handling and improve assignment creation flow.

- Use project_assignment_status / project_assignment_status_detail fields throughout (ClientDetail, AssignmentDetail, WorkOrderCard, StatusTimeline, UpdateTimelineStatus, ProjectDetail filters) and prefer start_date/end_date for timeline ranges.
- Filter timeline lists to only include entries with start_date and format dates using dayjs.
- Update UpdateTimelineStatus to submit detail/master IDs and call PUT /project-assignment/status/:assignmentId; adjust table and editing state types.
- Rework ManageTeam and related types to match API shape (user object nested) and update UI/behaviors.
- Add a dedicated "Clients" step to ProjectAssignmentForm with client & end-user comboboxes, client creation modal, client/end-user fetching, validation, and navigation; fix payload mapping, default values, and post-submit routing.
- Add new project-assignment page entry (app/(dashboard)/task/project-assignment/page.tsx).
- Wrap ClientGrid cards in links and tidy card actions; adjust HomeTask view options and create-button behavior to route to project assignment flow.
- Add optional redirect behavior to FormClient and small UI spacing/structure tweaks.

These changes align front-end data structures with backend fields, improve timeline accuracy, and streamline the project assignment creation UX.
2026-04-28 18:47:02 +07:00

223 lines
8.0 KiB
TypeScript

"use client";
import * as React from "react";
import { Check, ChevronsUpDown } from "lucide-react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
export interface ComboboxOption {
value: string | number;
label: string;
// Allows you to pass raw data for custom UI rendering without using 'any'
originalData?: unknown;
}
interface ComboboxProps {
options: ComboboxOption[];
value?: string | number;
onChange: (value: string | number | undefined) => void;
onSelect?: (option: ComboboxOption) => void;
placeholder?: string;
searchPlaceholder?: string;
emptyText?: string;
disabled?: boolean;
className?: string;
// Optional props for Custom UI
renderTrigger?: (selectedOption?: ComboboxOption) => React.ReactNode;
renderOption?: (option: ComboboxOption, isSelected: boolean) => React.ReactNode;
}
export function ComboboxCustomUI({
options,
value,
onChange,
onSelect,
placeholder = "Select option...",
searchPlaceholder = "Search...",
emptyText = "No results found.",
disabled = false,
className,
renderTrigger,
renderOption,
}: ComboboxProps) {
const [open, setOpen] = React.useState(false);
// Safely find the selected option by comparing strings to avoid number/string mismatch bugs
const selectedOption = React.useMemo(
() => options.find((opt) => String(opt.value) === String(value)),
[value, options]
);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild disabled={disabled}>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className={cn("w-full justify-between font-normal", !selectedOption && "text-muted-foreground", className)}
>
{renderTrigger ? (
renderTrigger(selectedOption)
) : (
<span className="truncate">
{selectedOption ? selectedOption.label : placeholder}
</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-(--radix-popover-trigger-width) max-h-(--radix-popover-content-available-height) p-0">
<Command>
<CommandInput placeholder={searchPlaceholder} />
<CommandList>
<CommandEmpty>{emptyText}</CommandEmpty>
<CommandGroup>
{options.map((option) => {
const isSelected = String(option.value) === String(value);
return (
<CommandItem
key={option.value}
// cmdk uses the 'value' prop for searching. We pass the label here so users can search by text.
value={option.label}
onSelect={() => {
// Toggle selection: if already selected, clear it (undefined), else set new value
onChange(isSelected ? undefined : option.value);
setOpen(false);
onSelect?.(option);
}}
>
{renderOption ? (
renderOption(option, isSelected)
) : (
<>
<Check
className={cn(
"mr-2 h-4 w-4 shrink-0",
isSelected ? "opacity-100" : "opacity-0"
)}
/>
<span className="truncate">{option.label}</span>
</>
)}
</CommandItem>
);
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}
/** Standalone (No Form)
export function SalesFilter() {
const [selectedId, setSelectedId] = useState<string | number>();
return (
<div className="w-64">
<Combobox
options={mappedSalesPrograms}
value={selectedId}
onChange={setSelectedId}
/>
</div>
);
}
*/
/** TanStack Form (Standard UI)
import { Combobox } from "@/components/ui/combobox";
import { useMemo } from "react";
export function SalesForm() {
// 1. Map your raw data to the standard option format
const mappedSalesPrograms = useMemo(() => {
return dataSalesProgram?.map((item) => ({
label: item.nama,
value: item.id,
})) || [];
}, [dataSalesProgram]);
return (
<form.AppField name="sales_program">
{(field) => (
<FieldGroup>
<FormLabel>Sales Program</FormLabel>
<Combobox
options={mappedSalesPrograms}
value={field.state.value}
onChange={(val) => field.handleChange(val as number)} // Cast back to your strict type
placeholder="Pilih Sales Program"
searchPlaceholder="Cari Sales Program..."
/>
{field.state.meta.errors && <FormMessage>{field.state.meta.errors}</FormMessage>}
</FieldGroup>
)}
</form.AppField>
);
}
*/
/** Custom UI Selection
export function CustomUICombobox() {
// Passing the raw item in `originalData` so we can use it in the custom render
const users = [
{ id: 1, name: "Luthfi Aditama", role: "Team Leader" },
{ id: 2, name: "Ihsan", role: "Developer" },
];
const options = users.map(user => ({
value: user.id,
label: user.name,
originalData: user // <-- Store raw data here
}));
return (
<Combobox
options={options}
value={1}
onChange={(val) => console.log(val)}
// 1. Custom Selected Value
renderTrigger={(selected) => (
<div className="flex items-center gap-2">
<span className="font-bold text-primary">{selected?.label || "Pilih User"}</span>
</div>
)}
// 2. Custom Dropdown Items
renderOption={(option, isSelected) => {
// Safely cast originalData back to your expected type
const user = option.originalData as typeof users[0];
return (
<div className="flex items-center justify-between w-full">
<div className="flex flex-col">
<span className={isSelected ? "font-bold" : ""}>{option.label}</span>
<span className="text-xs text-muted-foreground">{user.role}</span>
</div>
{isSelected && <Badge variant="secondary">Active</Badge>}
</div>
);
}}
/>
);
}
*/