components/linyi/combobox.tsx
@base-ui/reactclass-variance-authoritylucide-react
"use client"; import * as React from "react";import { Combobox as BaseCombobox } from "@base-ui/react/combobox";import { cva, type VariantProps } from "class-variance-authority";import { Check, ChevronDown, Search, X } from "lucide-react";import { cn } from "@/lib/cn"; export const Combobox = BaseCombobox.Root;export const ComboboxValue = BaseCombobox.Value;export const ComboboxCollection = BaseCombobox.Collection;export const ComboboxGroup = BaseCombobox.Group;export const ComboboxRow = BaseCombobox.Row;export const useComboboxFilter = BaseCombobox.useFilter; export const comboboxInputGroupVariants = cva( [ "relative flex w-full items-center gap-1.5 rounded-control border text-fg transition-[border-color,background-color,box-shadow] duration-150", "focus-within:border-accent focus-within:ring-2 focus-within:ring-accent/20", "has-[:disabled]:opacity-45 has-[:disabled]:pointer-events-none", ].join(" "), { variants: { variant: { surface: "border-line-strong bg-surface hover:border-fg/30", well: "border-line-well bg-well hover:border-line-strong focus-within:bg-surface", }, size: { sm: "min-h-control-sm px-2.5 text-xs", md: "min-h-control-md px-3 text-xs", lg: "min-h-control-lg px-3.5 text-sm", }, invalid: { true: "border-danger focus-within:border-danger focus-within:ring-danger/20", false: "", }, }, defaultVariants: { variant: "surface", size: "md", invalid: false, }, }); export const comboboxPopupVariants = cva( [ "min-w-[var(--anchor-width)] max-w-[min(calc(100vw-24px),420px)] overflow-hidden rounded-[14px] border border-line-strong bg-surface/96 p-1.5 text-fg shadow-popover backdrop-blur-md outline-none", "origin-[var(--transform-origin)] transition-[opacity,transform] duration-150 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none", "data-[starting-style]:opacity-0 data-[starting-style]:scale-95", "data-[ending-style]:opacity-0 data-[ending-style]:scale-95", ].join(" ")); export const comboboxItemVariants = cva( [ "relative flex min-h-[34px] w-full cursor-pointer select-none items-center justify-between gap-2 rounded-[8px] px-2.5 py-1.5 text-xs text-fg outline-none transition-colors", "data-[highlighted]:bg-well data-[selected]:font-medium", "data-[disabled]:pointer-events-none data-[disabled]:opacity-40", ].join(" ")); export interface ComboboxInputGroupProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.InputGroup>, "className" >, VariantProps<typeof comboboxInputGroupVariants> { className?: string;} export function ComboboxInputGroup({ className, variant = "surface", size = "md", invalid = false, ...props}: ComboboxInputGroupProps) { return ( <BaseCombobox.InputGroup className={cn( comboboxInputGroupVariants({ variant, size, invalid }), className )} {...props} /> );} export interface ComboboxInputProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Input>, "className" > { className?: string; showSearchIcon?: boolean;} export function ComboboxInput({ className, showSearchIcon = false, onCompositionStart, onCompositionEnd, onKeyDown, ...props}: ComboboxInputProps) { const composingRef = React.useRef(false); const handleCompositionStart: NonNullable< ComboboxInputProps["onCompositionStart"] > = (e) => { composingRef.current = true; onCompositionStart?.(e); }; const handleCompositionEnd: NonNullable< ComboboxInputProps["onCompositionEnd"] > = (e) => { composingRef.current = false; onCompositionEnd?.(e); }; const handleKeyDown: NonNullable<ComboboxInputProps["onKeyDown"]> = (e) => { onKeyDown?.(e); const native = e.nativeEvent as KeyboardEvent; const isImeComposing = composingRef.current || native.isComposing || e.keyCode === 229; if (isImeComposing && e.key === "Enter") { // 中文输入法选词回车时阻止 Base UI 默认选中,防止误提交 e.preventBaseUIHandler(); } }; return ( <span className="flex min-w-0 flex-1 items-center gap-1.5"> {showSearchIcon && ( <Search className="h-3.5 w-3.5 flex-shrink-0 text-fg-muted" aria-hidden="true" /> )} <BaseCombobox.Input onCompositionStart={handleCompositionStart} onCompositionEnd={handleCompositionEnd} onKeyDown={handleKeyDown} className={cn( "w-full min-w-[4rem] flex-1 bg-transparent py-1 text-inherit text-fg placeholder:text-fg-muted outline-none", className )} {...props} /> </span> );} export interface ComboboxTriggerProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Trigger>, "className" > { className?: string;} export function ComboboxTrigger({ className, children, ...props}: ComboboxTriggerProps) { return ( <BaseCombobox.Trigger aria-label="展开选项列表" className={cn( "inline-flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-[6px] text-fg-muted transition-[color,background-color,transform] duration-150 cursor-pointer hover:bg-well hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/30", "data-[popup-open]:rotate-180", className )} {...props} > {children ?? <ChevronDown className="h-3.5 w-3.5" aria-hidden="true" />} </BaseCombobox.Trigger> );} export interface ComboboxClearProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Clear>, "className" > { className?: string;} export function ComboboxClear({ className, children, ...props}: ComboboxClearProps) { return ( <BaseCombobox.Clear aria-label="清空已选项" className={cn( "inline-flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-[6px] text-fg-muted transition-colors cursor-pointer hover:bg-well hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/30", className )} {...props} > {children ?? <X className="h-3.5 w-3.5" aria-hidden="true" />} </BaseCombobox.Clear> );} export interface ComboboxChipsProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Chips>, "className" >, VariantProps<typeof comboboxInputGroupVariants> { className?: string;} export function ComboboxChips({ className, variant = "surface", size = "md", invalid = false, ...props}: ComboboxChipsProps) { return ( <BaseCombobox.Chips className={cn( comboboxInputGroupVariants({ variant, size, invalid }), "flex-wrap py-1", className )} {...props} /> );} export interface ComboboxChipProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Chip>, "className" > { className?: string;} export function ComboboxChip({ className, children, ...props}: ComboboxChipProps) { return ( <BaseCombobox.Chip className={cn( "inline-flex max-w-full items-center gap-1 rounded-badge border border-line bg-surface-2 px-2 py-0.5 text-[11px] font-medium text-fg outline-none transition-colors", "focus-within:border-accent focus-within:ring-2 focus-within:ring-accent/20", className )} {...props} > {children} </BaseCombobox.Chip> );} export interface ComboboxChipRemoveProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.ChipRemove>, "className" > { className?: string;} export function ComboboxChipRemove({ className, children, ...props}: ComboboxChipRemoveProps) { return ( <BaseCombobox.ChipRemove aria-label="移除该项" className={cn( "inline-flex h-4 w-4 flex-shrink-0 items-center justify-center rounded-full text-fg-muted transition-colors cursor-pointer hover:bg-well hover:text-fg", className )} {...props} > {children ?? <X className="h-2.5 w-2.5" aria-hidden="true" />} </BaseCombobox.ChipRemove> );} export interface ComboboxContentProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Popup>, "className" > { className?: string; side?: React.ComponentProps<typeof BaseCombobox.Positioner>["side"]; align?: React.ComponentProps<typeof BaseCombobox.Positioner>["align"]; sideOffset?: number; anchor?: React.ComponentProps<typeof BaseCombobox.Positioner>["anchor"];} export function ComboboxContent({ className, side = "bottom", align = "start", sideOffset = 6, anchor, children, ...props}: ComboboxContentProps) { return ( <BaseCombobox.Portal> <BaseCombobox.Positioner side={side} align={align} sideOffset={sideOffset} anchor={anchor} className="z-[var(--z-popover)] outline-none" > <BaseCombobox.Popup className={cn(comboboxPopupVariants(), className)} {...props} > {children} </BaseCombobox.Popup> </BaseCombobox.Positioner> </BaseCombobox.Portal> );} export interface ComboboxListProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.List>, "className" > { className?: string;} export function ComboboxList({ className, ...props }: ComboboxListProps) { return ( <BaseCombobox.List className={cn( "max-h-[248px] overflow-y-auto space-y-0.5 outline-none data-[empty]:p-0", className )} {...props} /> );} export interface ComboboxItemProps extends Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Item>, "className" > { className?: string;} export function ComboboxItem({ className, children, ...props}: ComboboxItemProps) { return ( <BaseCombobox.Item className={cn(comboboxItemVariants(), className)} {...props} > <span className="min-w-0 flex-1 truncate">{children}</span> <BaseCombobox.ItemIndicator className="flex-shrink-0 text-accent"> <Check className="h-3.5 w-3.5" aria-hidden="true" /> </BaseCombobox.ItemIndicator> </BaseCombobox.Item> );} export function ComboboxGroupLabel({ className, ...props}: Omit< React.ComponentPropsWithRef<typeof BaseCombobox.GroupLabel>, "className"> & { className?: string }) { return ( <BaseCombobox.GroupLabel className={cn( "px-2.5 pb-1 pt-2 font-mono-num text-[10px] tracking-wide text-fg-muted select-none", className )} {...props} /> );} export function ComboboxEmpty({ className, children = "未找到匹配选项", ...props}: Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Empty>, "className"> & { className?: string }) { return ( <BaseCombobox.Empty className={cn( "px-3 py-5 text-center text-xs text-fg-muted empty:hidden", className )} {...props} > {children} </BaseCombobox.Empty> );} export function ComboboxStatus({ className, ...props}: Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Status>, "className"> & { className?: string }) { return ( <BaseCombobox.Status className={cn( "px-2.5 py-1.5 text-[11px] text-fg-muted empty:hidden", className )} {...props} /> );} export function ComboboxSeparator({ className, ...props}: Omit< React.ComponentPropsWithRef<typeof BaseCombobox.Separator>, "className"> & { className?: string }) { return ( <BaseCombobox.Separator className={cn("my-1 h-px bg-line", className)} {...props} /> );}