81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Check, ChevronsUpDown } from "lucide-react";
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
import {
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
} from "@/components/ui/command";
|
|
import {
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
} from "@/components/ui/popover";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
interface ComboboxProps {
|
|
choices: string[];
|
|
value: string;
|
|
onChange: (value: string) => void;
|
|
placeholder?: string;
|
|
}
|
|
|
|
export function Combobox({
|
|
choices,
|
|
value,
|
|
onChange,
|
|
placeholder = "Select...",
|
|
}: ComboboxProps) {
|
|
const [open, setOpen] = useState(false);
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger
|
|
className={cn(
|
|
buttonVariants({ variant: "outline" }),
|
|
"h-auto min-h-8 w-full justify-between font-normal",
|
|
)}
|
|
>
|
|
<span
|
|
className={cn("flex-1 text-left", !value && "text-muted-foreground")}
|
|
>
|
|
{value || placeholder}
|
|
</span>
|
|
<ChevronsUpDown className="text-muted-foreground size-4 shrink-0" />
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-80 p-0">
|
|
<Command>
|
|
<CommandInput placeholder="Search..." />
|
|
<CommandList>
|
|
<CommandEmpty>No matches.</CommandEmpty>
|
|
<CommandGroup>
|
|
{choices.map((choice) => (
|
|
<CommandItem
|
|
key={choice}
|
|
onSelect={() => {
|
|
onChange(choice);
|
|
setOpen(false);
|
|
}}
|
|
>
|
|
<Check
|
|
className={cn(
|
|
"mr-2 size-4",
|
|
value === choice ? "opacity-100" : "opacity-0",
|
|
)}
|
|
/>
|
|
{choice}
|
|
</CommandItem>
|
|
))}
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
}
|