Skip to content

The platform for continuously improving AI agents.

4.7 KB
Raw Blame
import { type ReactNode, useState } from "react";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
  Command,
  CommandEmpty,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command";
import { Icon } from "@/components/ui/icons";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { cn } from "@/lib/utils";

export type Option = { value: string; label: string; tag?: string };

export function SearchableSelect({
  options,
  value,
  onChange,
  placeholder = "Select…",
  searchPlaceholder = "Search…",
  ariaLabel,
  triggerClassName,
  renderOption,
  isLoading = false,
  disabled = false,
  emptyMessage = "No options available.",
}: {
  options: Option[];
  value: string;
  onChange: (v: string) => void;
  placeholder?: string;
  searchPlaceholder?: string;
  ariaLabel: string;
  triggerClassName?: string;
  renderOption?: (option: Option) => ReactNode;
  isLoading?: boolean;
  disabled?: boolean;
  emptyMessage?: ReactNode;
}) {
  const [open, setOpen] = useState(false);
  const [search, setSearch] = useState("");
  const selected = options.find((o) => o.value === value) ?? null;

  const handleOpenChange = (next: boolean) => {
    setOpen(next);
    if (!next) setSearch("");
  };
  const handleSelect = (v: string) => {
    onChange(v);
    setOpen(false);
    setSearch("");
  };

  return (
    <Popover onOpenChange={handleOpenChange} open={open}>
      <PopoverTrigger asChild>
        <Button
          aria-label={ariaLabel}
          className={cn("justify-between font-normal", triggerClassName)}
          disabled={disabled}
          type="button"
          variant="secondary"
        >
          <span className="flex min-w-0 items-center gap-1.5 truncate">
            {selected && renderOption ? (
              renderOption(selected)
            ) : (
              <span className="truncate">{selected?.label ?? placeholder}</span>
            )}
            {selected?.tag ? (
              <Badge className="shrink-0 tracking-normal" size="chip" variant="secondary">
                {selected.tag}
              </Badge>
            ) : null}
          </span>
          <Icon.chevronDown className="shrink-0 text-muted-foreground" />
        </Button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        className="w-(--radix-popover-trigger-width) min-w-56 max-w-[calc(100vw-2rem)] overflow-hidden p-0"
      >
        <Command>
          <CommandInput
            aria-label={searchPlaceholder}
            className="h-9 py-2 text-xs"
            onValueChange={setSearch}
            placeholder={searchPlaceholder}
            value={search}
          />
          <CommandList aria-label={ariaLabel} className="max-h-[280px] p-1">
            {isLoading ? (
              <p className="status-shimmer px-2 py-3 text-xs text-muted-foreground" role="status">
                Loading options
              </p>
            ) : (
              <>
                <CommandEmpty className="px-2 py-3 text-left text-xs">
                  {search.trim() ? `No matches for "${search.trim()}".` : emptyMessage}
                </CommandEmpty>
                {options.map((option) => (
                  <CommandItem
                    className={cn(
                      "w-full text-xs",
                      renderOption && "text-sm",
                      option.value === value && "font-medium text-primary"
                    )}
                    key={option.value}
                    keywords={[option.label, option.tag ?? ""]}
                    onSelect={() => handleSelect(option.value)}
                    value={option.value}
                  >
                    <Icon.success
                      aria-hidden
                      className={cn(
                        "size-4 shrink-0",
                        renderOption && "order-last ml-auto",
                        option.value !== value && "opacity-0"
                      )}
                    />
                    {renderOption ? (
                      renderOption(option)
                    ) : (
                      <span className="truncate">{option.label}</span>
                    )}
                    {option.tag && (
                      <Badge
                        className="ml-auto shrink-0 tracking-normal"
                        size="chip"
                        variant="secondary"
                      >
                        {option.tag}
                      </Badge>
                    )}
                  </CommandItem>
                ))}
              </>
            )}
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  );
}