2026-04-09 10:01:23 +08:00
|
|
|
"use client"
|
|
|
|
|
|
2026-05-25 12:51:41 +08:00
|
|
|
import { useState, type ReactNode } from "react"
|
2026-04-09 10:01:23 +08:00
|
|
|
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react"
|
|
|
|
|
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
2026-07-26 12:18:13 +08:00
|
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
2026-04-09 10:01:23 +08:00
|
|
|
import {
|
|
|
|
|
Command,
|
|
|
|
|
CommandEmpty,
|
|
|
|
|
CommandGroup,
|
|
|
|
|
CommandInput,
|
|
|
|
|
CommandItem,
|
|
|
|
|
CommandList,
|
|
|
|
|
} from "@/components/ui/command"
|
|
|
|
|
import {
|
|
|
|
|
Popover,
|
|
|
|
|
PopoverContent,
|
|
|
|
|
PopoverTrigger,
|
|
|
|
|
} from "@/components/ui/popover"
|
|
|
|
|
import { cn } from "@/lib/utils"
|
2026-05-25 12:06:15 +08:00
|
|
|
import { useI18n } from "@/i18n/provider"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
export type ComboboxOption = {
|
|
|
|
|
value: string
|
|
|
|
|
label: string
|
2026-06-29 18:37:31 +08:00
|
|
|
subtitle?: string
|
2026-06-28 17:53:56 +08:00
|
|
|
description?: string
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 12:18:13 +08:00
|
|
|
type CommonOptionComboboxProps = {
|
2026-04-09 10:01:23 +08:00
|
|
|
options: ComboboxOption[]
|
|
|
|
|
placeholder: string
|
|
|
|
|
searchPlaceholder?: string
|
|
|
|
|
emptyText?: string
|
|
|
|
|
disabled?: boolean
|
2026-06-28 19:25:58 +08:00
|
|
|
triggerClassName?: string
|
2026-06-30 13:34:42 +08:00
|
|
|
preserveExternalSelection?: boolean
|
2026-04-09 10:01:23 +08:00
|
|
|
renderOptionAction?: (option: ComboboxOption) => ReactNode
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 12:18:13 +08:00
|
|
|
type OptionComboboxProps = CommonOptionComboboxProps &
|
|
|
|
|
(
|
|
|
|
|
| {
|
|
|
|
|
multiple?: false
|
|
|
|
|
value: string
|
|
|
|
|
onChange: (value: string) => void
|
|
|
|
|
values?: never
|
|
|
|
|
onValuesChange?: never
|
|
|
|
|
}
|
|
|
|
|
| {
|
|
|
|
|
multiple: true
|
|
|
|
|
values: string[]
|
|
|
|
|
onValuesChange: (values: string[]) => void
|
|
|
|
|
value?: never
|
|
|
|
|
onChange?: never
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export function OptionCombobox(props: OptionComboboxProps) {
|
|
|
|
|
const {
|
2026-04-09 10:01:23 +08:00
|
|
|
options,
|
|
|
|
|
placeholder,
|
2026-05-25 12:06:15 +08:00
|
|
|
searchPlaceholder,
|
|
|
|
|
emptyText,
|
2026-04-09 10:01:23 +08:00
|
|
|
disabled = false,
|
2026-06-28 19:25:58 +08:00
|
|
|
triggerClassName,
|
2026-06-30 13:34:42 +08:00
|
|
|
preserveExternalSelection = false,
|
2026-04-09 10:01:23 +08:00
|
|
|
renderOptionAction,
|
2026-07-26 12:18:13 +08:00
|
|
|
} = props
|
2026-05-25 12:06:15 +08:00
|
|
|
const t = useI18n()
|
2026-05-25 12:51:41 +08:00
|
|
|
const [open, setOpen] = useState(false)
|
2026-07-26 12:18:13 +08:00
|
|
|
const selectedValues = props.multiple ? props.values : [props.value]
|
|
|
|
|
const selectedOptions = options.filter((option) =>
|
|
|
|
|
selectedValues.includes(option.value)
|
|
|
|
|
)
|
2026-04-09 10:01:23 +08:00
|
|
|
const selectedLabel =
|
2026-07-26 12:18:13 +08:00
|
|
|
selectedOptions.length === 0
|
|
|
|
|
? placeholder
|
|
|
|
|
: selectedOptions.length === 1
|
|
|
|
|
? selectedOptions[0].label
|
|
|
|
|
: `已选择 ${selectedOptions.length} 项`
|
|
|
|
|
|
|
|
|
|
function selectOption(optionValue: string) {
|
|
|
|
|
if (props.multiple) {
|
|
|
|
|
props.onValuesChange(
|
|
|
|
|
props.values.includes(optionValue)
|
|
|
|
|
? props.values.filter((value) => value !== optionValue)
|
|
|
|
|
: [...props.values, optionValue]
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
props.onChange(optionValue)
|
|
|
|
|
setOpen(false)
|
|
|
|
|
}
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-05-25 12:51:41 +08:00
|
|
|
<Popover open={open} onOpenChange={setOpen}>
|
2026-04-09 10:01:23 +08:00
|
|
|
<PopoverTrigger
|
|
|
|
|
render={
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
role="combobox"
|
2026-06-28 19:25:58 +08:00
|
|
|
className={cn("m-0 w-full justify-between font-normal", triggerClassName)}
|
2026-04-09 10:01:23 +08:00
|
|
|
disabled={disabled}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<span className="truncate">{selectedLabel}</span>
|
|
|
|
|
<ChevronsUpDownIcon className="ml-2 size-4 shrink-0 opacity-50" />
|
|
|
|
|
</PopoverTrigger>
|
2026-06-30 13:34:42 +08:00
|
|
|
<PopoverContent
|
|
|
|
|
className="w-(--radix-popover-trigger-width) p-0"
|
|
|
|
|
align="start"
|
|
|
|
|
data-workflow-preserve-selection={preserveExternalSelection ? true : undefined}
|
|
|
|
|
>
|
2026-04-09 10:01:23 +08:00
|
|
|
<Command>
|
2026-05-25 12:06:15 +08:00
|
|
|
<CommandInput placeholder={searchPlaceholder ?? t("common.searchKeyword")} />
|
2026-04-09 10:01:23 +08:00
|
|
|
<CommandList>
|
2026-05-25 12:06:15 +08:00
|
|
|
<CommandEmpty>{emptyText ?? t("common.emptyOptions")}</CommandEmpty>
|
2026-04-09 10:01:23 +08:00
|
|
|
<CommandGroup>
|
|
|
|
|
{options.map((option) => (
|
|
|
|
|
<CommandItem
|
|
|
|
|
key={option.value}
|
2026-06-29 18:37:31 +08:00
|
|
|
value={`${option.label} ${option.value} ${option.subtitle ?? ""} ${option.description ?? ""}`}
|
2026-07-26 12:18:13 +08:00
|
|
|
onSelect={() => selectOption(option.value)}
|
2026-04-09 10:01:23 +08:00
|
|
|
>
|
|
|
|
|
<div className="flex min-w-0 flex-1 items-center justify-between gap-2">
|
2026-06-28 17:53:56 +08:00
|
|
|
<div className="flex min-w-0 items-start">
|
2026-07-26 12:18:13 +08:00
|
|
|
{props.multiple ? (
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={selectedValues.includes(option.value)}
|
|
|
|
|
className="pointer-events-none mr-2 mt-0.5"
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<CheckIcon
|
|
|
|
|
className={cn(
|
|
|
|
|
"mr-2 mt-0.5 size-4 shrink-0",
|
|
|
|
|
option.value === props.value ? "opacity-100" : "opacity-0"
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-06-28 17:53:56 +08:00
|
|
|
<span className="min-w-0">
|
|
|
|
|
<span className="block truncate">{option.label}</span>
|
2026-06-29 18:37:31 +08:00
|
|
|
{option.subtitle ? (
|
|
|
|
|
<span className="mt-0.5 block truncate font-mono text-[11px] leading-4 text-slate-500">
|
|
|
|
|
{option.subtitle}
|
|
|
|
|
</span>
|
|
|
|
|
) : null}
|
2026-06-28 17:53:56 +08:00
|
|
|
{option.description ? (
|
|
|
|
|
<span className="mt-0.5 line-clamp-2 text-xs leading-4 text-muted-foreground">
|
|
|
|
|
{option.description}
|
|
|
|
|
</span>
|
|
|
|
|
) : null}
|
|
|
|
|
</span>
|
2026-04-09 10:01:23 +08:00
|
|
|
</div>
|
|
|
|
|
{renderOptionAction ? (
|
|
|
|
|
<div
|
|
|
|
|
className="shrink-0"
|
|
|
|
|
onMouseDown={(event) => event.preventDefault()}
|
|
|
|
|
onClick={(event) => event.stopPropagation()}
|
|
|
|
|
>
|
|
|
|
|
{renderOptionAction(option)}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
</CommandItem>
|
|
|
|
|
))}
|
|
|
|
|
</CommandGroup>
|
|
|
|
|
</CommandList>
|
|
|
|
|
</Command>
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
</Popover>
|
|
|
|
|
)
|
|
|
|
|
}
|