"use client"
import * as React from "react"
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react"
import { cn } from "@/client/lib/utils"
import { Button } from "@/client/components/ui/button"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/client/components/ui/command"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/client/components/ui/popover"
export interface ComboboxOption {
value: string
label: string
}
interface ComboboxProps {
options: ComboboxOption[]
value?: string
onValueChange?: (value: string) => void
onSearchChange?: (search: string) => void
placeholder?: string
searchPlaceholder?: string
emptyMessage?: string
className?: string
disabled?: boolean
}
// 将 Command 逻辑封装为独立组件,确保 options 更新时重新渲染
interface ComboboxCommandProps {
options: ComboboxOption[]
value?: string
onValueChange?: (value: string) => void
onSearchChange?: (search: string) => void
searchPlaceholder?: string
emptyMessage?: string
onClose: () => void
}
function ComboboxCommand({
options,
value,
onValueChange,
onSearchChange,
searchPlaceholder = "搜索...",
emptyMessage = "未找到匹配项",
onClose,
}: ComboboxCommandProps) {
console.debug('ComboboxCommand options', options)
return (
{emptyMessage}
{options.map((option) => (
{
onValueChange?.(currentValue === value ? "" : currentValue)
onClose()
}}
>
{option.label}
))}
)
}
export function Combobox({
options,
value,
onValueChange,
onSearchChange,
placeholder = "请选择...",
searchPlaceholder = "搜索...",
emptyMessage = "未找到匹配项",
className,
disabled = false,
}: ComboboxProps) {
const [open, setOpen] = React.useState(false)
const handleClose = () => {
setOpen(false)
}
return (
)
}