Files
portabase/src/components/wrappers/common/combobox.tsx
T

120 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
"use client";
2024-11-17 19:13:51 +01:00
2025-07-17 09:28:57 +02:00
import {useEffect, useState} from "react";
import {Check, ChevronDown} 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";
import {SidebarMenuButton} from "@/components/ui/sidebar";
import {Separator} from "@/components/ui/separator";
2024-11-17 19:13:51 +01:00
2025-11-22 18:45:24 +01:00
export type ComboBoxProps<T = string> = {
values: Array<{ value: T; label: string }>;
defaultValue?: T;
onValueChangeAction?: (value: T) => void;
2025-05-16 15:54:17 +02:00
searchField?: boolean;
sideBar?: boolean;
2025-11-22 18:45:24 +01:00
onAddItemAction?: () => void;
addItemLabel?: string;
2025-05-16 15:54:17 +02:00
};
2024-11-17 19:13:51 +01:00
2025-11-22 18:45:24 +01:00
export function ComboBox<T = string>(props: ComboBoxProps<T>) {
2025-07-17 09:28:57 +02:00
const {
values: choices,
2025-11-22 18:45:24 +01:00
defaultValue,
onValueChangeAction,
2025-07-17 09:28:57 +02:00
searchField = false,
2025-11-22 18:45:24 +01:00
sideBar = false,
onAddItemAction,
addItemLabel = "Add item",
2025-07-17 09:28:57 +02:00
} = props;
2024-11-17 19:13:51 +01:00
2025-11-22 18:45:24 +01:00
const [value, setValue] = useState<T | undefined>(defaultValue);
const [open, setOpen] = useState(false);
2024-12-12 13:31:11 +01:00
useEffect(() => {
2025-11-22 18:45:24 +01:00
setValue(defaultValue);
}, [defaultValue]);
2024-11-17 19:13:51 +01:00
return (
2025-07-17 09:28:57 +02:00
<div>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
{sideBar ? (
<SidebarMenuButton>
2025-12-30 14:02:49 +01:00
<label className="max-w-[170px] truncate">
2025-11-22 18:45:24 +01:00
{value ? choices.find((c) => c.value === value)?.label : "Select choice..."}
2025-12-30 14:02:49 +01:00
</label>
<ChevronDown className="ml-auto"/>
2025-07-17 09:28:57 +02:00
</SidebarMenuButton>
) : (
<Button variant="outline" role="combobox" aria-expanded={open}
className="w-full justify-between">
2025-12-30 14:02:49 +01:00
<label className="max-w-[170px] truncate">
{value ? choices.find((c) => c.value === value)?.label : "Select choice..."}
</label>
2025-07-17 09:28:57 +02:00
<ChevronDown className="opacity-50"/>
</Button>
)}
</PopoverTrigger>
2025-11-22 18:45:24 +01:00
<PopoverContent
className="p-0"
align="start"
sideOffset={4}
2025-12-30 14:02:49 +01:00
style={{width: 'var(--radix-popover-trigger-width)'}}
2025-12-22 14:23:52 +01:00
2025-11-22 18:45:24 +01:00
>
2025-07-17 09:28:57 +02:00
<Command>
2025-11-22 18:45:24 +01:00
{searchField && <CommandInput placeholder="Search choice..." className="h-9"/>}
2025-07-17 09:28:57 +02:00
<CommandList>
<CommandEmpty>No choice found.</CommandEmpty>
<CommandGroup>
{choices.map((choice) => (
<CommandItem
2025-11-22 18:45:24 +01:00
key={String(choice.value)}
value={String(choice.value)}
2025-07-17 09:28:57 +02:00
onSelect={(currentValue) => {
2025-11-22 18:45:24 +01:00
const v = choices.find(c => String(c.value) === currentValue)?.value;
if (v !== undefined) {
setValue(v);
onValueChangeAction?.(v);
setOpen(false);
}
2025-07-17 09:28:57 +02:00
}}
>
2025-12-30 14:02:49 +01:00
<label className="max-w-[170px] truncate">
{choice.label}
</label>
2025-07-17 09:28:57 +02:00
<Check
className={cn("ml-auto", value === choice.value ? "opacity-100" : "opacity-0")}/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
2025-11-22 18:45:24 +01:00
{onAddItemAction && (
<>
<Separator/>
{sideBar ? (
<SidebarMenuButton onClick={() => {
setOpen(false);
onAddItemAction();
}}>
+ {addItemLabel}
</SidebarMenuButton>
) : (
<Button variant="outline" className="w-full" onClick={onAddItemAction}>
+ {addItemLabel}
</Button>
)}
</>
)}
2025-07-17 09:28:57 +02:00
</PopoverContent>
</Popover>
</div>
2025-05-16 15:54:17 +02:00
);
2024-11-17 19:13:51 +01:00
}