frontend/app/components/combobox.tsx
2025-07-29 11:05:49 +02:00

81 lines
2.2 KiB
TypeScript

import * as React from "react"
import { CheckIcon, ChevronsUpDownIcon } 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 function Combobox({
values = [],
defaultText = "Select value...",
placeholderText = "Search...",
emptyText = "No value found.",
current, setValue
}: {
values?: { value: string; label: string }[]
emptyText?: string
placeholderText?: string
defaultText?: string
current?: string
setValue: (value: string) => void
}) {
const [open, setOpen] = React.useState(false)
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
>
{current
? values.find((value) => value.value === current)?.label!
: defaultText}
<ChevronsUpDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">
<Command>
<CommandInput placeholder={placeholderText} />
<CommandList>
<CommandEmpty>{emptyText}</CommandEmpty>
<CommandGroup>
{values.map(({ value, label }) => (
<CommandItem
key={value}
value={value}
onSelect={(currentValue) => {
setValue(currentValue === current ? "" : currentValue)
setOpen(false)
}}
>
<CheckIcon
className={cn(
"mr-2 h-4 w-4",
current === value ? "opacity-100" : "opacity-0"
)}
/>
{label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}