"use client"; 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"; export type ComboBoxProps = { values: Array<{ value: T; label: string }>; defaultValue?: T; onValueChangeAction?: (value: T) => void; searchField?: boolean; sideBar?: boolean; onAddItemAction?: () => void; addItemLabel?: string; }; export function ComboBox(props: ComboBoxProps) { const { values: choices, defaultValue, onValueChangeAction, searchField = false, sideBar = false, onAddItemAction, addItemLabel = "Add item", } = props; const [value, setValue] = useState(defaultValue); const [open, setOpen] = useState(false); useEffect(() => { setValue(defaultValue); }, [defaultValue]); return (
{sideBar ? ( ) : ( )} {searchField && } No choice found. {choices.map((choice) => ( { const v = choices.find(c => String(c.value) === currentValue)?.value; if (v !== undefined) { setValue(v); onValueChangeAction?.(v); setOpen(false); } }} > ))} {onAddItemAction && ( <> {sideBar ? ( { setOpen(false); onAddItemAction(); }}> + {addItemLabel} ) : ( )} )}
); }