Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

8.9 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { useControllableState } from "@radix-ui/react-use-controllable-state";
import { Check, ChevronDownIcon, XCircle } from "lucide-react";
import React, { createContext, useCallback, useMemo } from "react";
import { cn } from "../../utils/cn";
import { Functions } from "../../utils/functions";
import { Badge } from "./badge";
import {
  Command,
  CommandEmpty,
  CommandInput,
  CommandItem,
  CommandList,
} from "./command";
import { Popover, PopoverContent, PopoverTrigger } from "./popover";

interface ComboboxContextValue {
  isSelected: (value: unknown) => boolean;
  onSelect: (value: unknown) => void;
}

export const ComboboxContext = createContext<ComboboxContextValue>({
  isSelected: () => false,
  onSelect: Functions.NOOP,
});

interface ComboboxCommonProps<TValue> {
  children: React.ReactNode;
  displayValue?: (item: TValue) => string;
  placeholder?: string;
  open?: boolean;
  defaultOpen?: boolean;
  onOpenChange?: (open: boolean) => void;
  inputPlaceholder?: string;
  search?: string;
  onSearchChange?: (search: string) => void;
  emptyState?: React.ReactNode;
  className?: string;
  id?: string;
  keepPopoverOpenOnSelect?: boolean;
  disabled?: boolean;
  /** Override the trigger contents with a node (e.g. a compact chip summary). */
  renderValue?: (value: TValue[] | TValue | null) => React.ReactNode;
}

type ComboboxFilterProps =
  | {
      shouldFilter?: true;
      filterFn?: React.ComponentProps<typeof Command>["filter"];
    }
  | {
      shouldFilter: false;
      filterFn?: never;
    };

type ComboboxValueProps<TValue> =
  | {
      multiple?: false;
      chips?: false;
      chipsClassName?: never;
      value?: TValue | null;
      defaultValue?: TValue | null;
      onValueChange?: (value: TValue | null) => void;
    }
  | {
      multiple: true;
      chips?: boolean;
      chipsClassName?: string;
      value?: TValue[] | null;
      defaultValue?: TValue[] | null;
      onValueChange?: (value: TValue[] | null) => void;
    };

export type ComboboxProps<TValue> = ComboboxCommonProps<TValue> &
  ComboboxValueProps<TValue> &
  ComboboxFilterProps;

export const Combobox = <TValue,>({
  children,
  displayValue,
  className,
  placeholder,
  value: valueProp,
  defaultValue,
  onValueChange,
  multiple = false,
  shouldFilter = true,
  filterFn,
  open: openProp,
  defaultOpen,
  onOpenChange,
  inputPlaceholder = "Search...",
  search,
  onSearchChange,
  emptyState = "Nothing found.",
  chips = false,
  chipsClassName,
  keepPopoverOpenOnSelect,
  id,
  disabled = false,
  renderValue,
  ...rest
}: ComboboxProps<TValue>) => {
  const [open = false, setOpen] = useControllableState({
    prop: openProp,
    defaultProp: defaultOpen ?? false,
    onChange: onOpenChange,
  });
  const [value, setValue] = useControllableState({
    prop: valueProp,
    defaultProp: defaultValue,
    onChange: (state) => {
      onValueChange?.(state as unknown as TValue & TValue[]);
    },
  });

  const isSelected = useCallback(
    (selectedValue: unknown) => {
      if (Array.isArray(value)) {
        return value.includes(selectedValue as TValue);
      }
      return value === selectedValue;
    },
    [value],
  );

  const handleSelect = useCallback(
    (selectedValue: unknown) => {
      let newValue: TValue | TValue[] | null = selectedValue as TValue;

      if (multiple) {
        if (Array.isArray(value)) {
          if (value.includes(newValue)) {
            const newArr = value.filter((val) => val !== selectedValue);
            newValue = newArr.length > 0 ? newArr : [];
          } else {
            newValue = [...value, newValue];
          }
        } else {
          newValue = [newValue];
        }
      } else if (value === selectedValue) {
        newValue = null;
      }

      setValue(newValue);
      const keepOpen = keepPopoverOpenOnSelect ?? multiple;
      if (!keepOpen) {
        setOpen(false);
      }
    },
    [keepPopoverOpenOnSelect, multiple, value, setValue, setOpen],
  );

  const renderValueLabel = (): string => {
    // If we show chips, we don't want to change the placeholder
    if (multiple && chips && placeholder) {
      return placeholder;
    }

    if (value != null) {
      if (Array.isArray(value)) {
        if (value.length === 0) {
          return placeholder ?? "--";
        }
        if (value.length === 1 && displayValue !== undefined) {
          return displayValue(value[0]);
        }
        return `${value.length} selected`;
      }
      if (displayValue !== undefined) {
        return displayValue(value as unknown as TValue);
      }
      return placeholder ?? "--";
    }
    return placeholder ?? "--";
  };

  const comboboxContextValue: ComboboxContextValue = useMemo(
    () => ({
      isSelected,
      onSelect: handleSelect,
    }),
    [isSelected, handleSelect],
  );

  return (
    <div className={cn("relative")} {...rest}>
      <Popover
        open={open}
        onOpenChange={(v) => {
          if (disabled && v) {
            return;
          }
          setOpen(v);
        }}
      >
        <PopoverTrigger asChild={true}>
          <button
            id={id}
            type="button"
            className={cn(
              "flex h-6 w-fit mb-1 shadow-xs-solid items-center justify-between rounded-sm border border-input bg-transparent px-2 text-sm font-prose ring-offset-background placeholder:text-muted-foreground hover:shadow-sm-solid focus-visible:outline-hidden focus-visible:ring-1 focus-visible:ring-ring focus-visible:border-primary focus-visible:shadow-md-solid",
              disabled && "cursor-not-allowed opacity-50",
              className,
            )}
            aria-expanded={open}
            aria-disabled={disabled}
          >
            <span className="truncate flex-1 min-w-0">
              {renderValue ? renderValue(value ?? null) : renderValueLabel()}
            </span>
            <ChevronDownIcon className="ml-3 w-4 h-4 opacity-50 shrink-0" />
          </button>
        </PopoverTrigger>
        <PopoverContent
          className="w-full min-w-(--radix-popover-trigger-width) p-0"
          align="start"
        >
          <Command filter={filterFn} shouldFilter={shouldFilter}>
            <CommandInput
              placeholder={inputPlaceholder}
              rootClassName={"px-1 h-8"}
              autoFocus={true}
              value={search}
              onValueChange={onSearchChange}
            />
            <CommandList className="max-h-60 py-.5">
              <CommandEmpty>{emptyState}</CommandEmpty>
              <ComboboxContext value={comboboxContextValue}>
                {children}
              </ComboboxContext>
            </CommandList>
          </Command>
        </PopoverContent>
      </Popover>
      {multiple && chips && (
        <div className={cn("flex flex-col gap-1 items-start", chipsClassName)}>
          {Array.isArray(value) &&
            value.map((val) => {
              if (val == null) {
                return null;
              }
              return (
                <Badge key={String(val)} variant="secondary">
                  {displayValue?.(val) ?? String(val)}
                  <XCircle
                    onClick={() => {
                      if (disabled) {
                        return;
                      }
                      handleSelect(val);
                    }}
                    className={cn(
                      "w-3 h-3 opacity-50 hover:opacity-100 ml-1 cursor-pointer",
                      disabled && "pointer-events-none",
                    )}
                  />
                </Badge>
              );
            })}
        </div>
      )}
    </div>
  );
};

interface ComboboxItemOptions<TValue> {
  value: TValue;
}

export interface ComboboxItemProps<TValue>
  extends
    ComboboxItemOptions<TValue>,
    Omit<
      React.ComponentProps<typeof CommandItem>,
      keyof ComboboxItemOptions<TValue> | "onSelect" | "role"
    > {
  onSelect?: (value: TValue) => void;
}

export const ComboboxItem = React.forwardRef(
  <TValue extends string | number | { value: string }>(
    {
      children,
      className,
      value,
      onSelect,
      disabled,
    }: ComboboxItemProps<TValue>,
    ref: React.Ref<HTMLDivElement>,
  ) => {
    const valueAsString =
      typeof value === "object" && "value" in value
        ? value.value
        : String(value);
    const context = React.use(ComboboxContext);
    const isOptionSelected = context.isSelected(value);

    return (
      <CommandItem
        ref={ref}
        className={cn("pl-6 m-1 py-1", className)}
        role="option"
        aria-selected={isOptionSelected}
        value={valueAsString}
        disabled={disabled}
        onSelect={() => {
          context.onSelect(value);
          onSelect?.(value);
        }}
      >
        {isOptionSelected && <Check className="absolute left-1 h-4 w-4" />}
        {children}
      </CommandItem>
    );
  },
);
ComboboxItem.displayName = "ComboboxItem";