"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;
label: string;
disabled?: boolean;
}
interface ComboboxProps {
options: ComboboxOption[];
value?: string;
onValueChange?: (value: string) => void;
placeholder?: string;
searchPlaceholder?: string;
emptyMessage?: string;
disabled?: boolean;
className?: string;
triggerClassName?: string;
contentClassName?: string;
}
export function Combobox({
options,
value,
onValueChange,
placeholder = "Select option...",
searchPlaceholder = "Search...",
emptyMessage = "No results found.",
disabled = false,
className,
triggerClassName,
contentClassName,
}: ComboboxProps) {
const [open, setOpen] = React.useState(false);
const selectedOption = options.find((option) => option.value === value);
return (
{emptyMessage}
{options.map((option) => (
{
onValueChange?.(currentValue === value ? "" : currentValue);
setOpen(false);
}}
>
{option.label}
))}
);
}
// Specialized combobox for Google Places Autocomplete
interface PlacesComboboxProps {
value: string;
onValueChange: (value: string) => void;
onSelect: (address: string) => void;
suggestions: Array<{ place_id: string; description: string }>;
status: string;
disabled?: boolean;
placeholder?: string;
className?: string;
}
export function PlacesCombobox({
value,
onValueChange,
onSelect,
suggestions,
status,
disabled = false,
placeholder = "Cari Alamat",
className,
}: PlacesComboboxProps) {
const [open, setOpen] = React.useState(false);
const handleSelect = (address: string) => {
onSelect(address);
setOpen(false);
};
return (
{status === "OK" && suggestions.length > 0 ? (
{suggestions.map(({ place_id, description }) => (
handleSelect(description)}
>
{description}
))}
) : (
No results found.
)}
);
}