"use client" import type { ReactNode } from "react" import { CheckIcon, ChevronsUpDownIcon } from "lucide-react" 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 { cn } from "@/lib/utils" export type ComboboxOption = { value: string label: string } type OptionComboboxProps = { value: string options: ComboboxOption[] placeholder: string searchPlaceholder?: string emptyText?: string disabled?: boolean onChange: (value: string) => void renderOptionAction?: (option: ComboboxOption) => ReactNode } export function OptionCombobox({ value, options, placeholder, searchPlaceholder = "请输入关键字搜索", emptyText = "没有可选项", disabled = false, onChange, renderOptionAction, }: OptionComboboxProps) { const selectedLabel = options.find((option) => option.value === value)?.label ?? placeholder return ( } > {selectedLabel} {emptyText} {options.map((option) => ( onChange(option.value)} >
{option.label}
{renderOptionAction ? (
event.preventDefault()} onClick={(event) => event.stopPropagation()} > {renderOptionAction(option)}
) : null}
))}
) }