Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

21.8 KB
Raw Blame
// https://github.com/sersavan/shadcn-multi-select-component
// src/components/multi-select.tsx

import { cva, type VariantProps } from 'class-variance-authority';
import {
  CheckIcon,
  ChevronDown,
  TriangleAlert,
  WandSparkles,
  XCircle,
  XIcon,
} from 'lucide-react';
import * as React from 'react';

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';
import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';
import { t } from 'i18next';
import { isEmpty } from 'lodash';

export type MultiSelectOptionType = {
  label: React.ReactNode;
  value: string;
  disabled?: boolean;
  /**
   * When true, the option is always selected and cannot be deselected.
   * Unlike `disabled`, locked options are included in "Select All" and
   * preserved when clearing.
   */
  locked?: boolean;
  suffix?: React.ReactNode;
  icon?: React.ComponentType<{ className?: string }>;
};

export type MultiSelectGroupOptionType = {
  label: React.ReactNode;
  options: MultiSelectOptionType[];
};

/**
 * cmdk's default filter scores matches fuzzily and re-sorts the list by
 * score, which shuffles options out of their declared order as the user
 * types. A plain substring match scores every hit equally, so cmdk's
 * stable sort leaves the original order intact.
 */
function filterBySubstring(value: string, search: string) {
  return value.toLowerCase().includes(search.toLowerCase()) ? 1 : 0;
}

function MultiCommandItem({
  option,
  isSelected,
  toggleOption,
  optionTestId,
}: {
  option: MultiSelectOptionType;
  isSelected: boolean;
  toggleOption(value: string): void;
  optionTestId?: string;
}) {
  return (
    <CommandItem
      key={option.value}
      onSelect={() => {
        if (option.disabled) return false;
        toggleOption(option.value);
      }}
      data-testid={optionTestId}
      className={cn('cursor-pointer', {
        'cursor-not-allowed text-text-disabled': option.disabled,
      })}
    >
      <div
        className={cn(
          'mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary',
          isSelected ? 'bg-primary ' : 'opacity-50 [&_svg]:invisible',

          { 'text-primary-foreground': !option.disabled },
          { 'text-text-disabled': option.disabled },
        )}
      >
        <CheckIcon className="h-4 w-4" />
      </div>
      {option.icon && (
        <option.icon
          className={cn('mr-2 h-4 w-4 ', {
            'text-text-disabled': option.disabled,
            'text-muted-foreground': !option.disabled,
          })}
        />
      )}
      <span className={cn({ 'text-text-disabled': option.disabled })}>
        {option.label}
      </span>
      {option.suffix && (
        <span className={cn({ 'text-text-disabled': option.disabled })}>
          {option.suffix}
        </span>
      )}
    </CommandItem>
  );
}

/**
 * Variants for the multi-select component to handle different styles.
 * Uses class-variance-authority (cva) to define different styles based on "variant" prop.
 */
const multiSelectVariants = cva(
  'm-1 transition ease-in-out delay-150 hover:-translate-y-1 hover:scale-110 duration-300',
  {
    variants: {
      variant: {
        default:
          'border-foreground/10 text-foreground bg-card hover:bg-card/80',
        secondary:
          'border-foreground/10 bg-secondary text-secondary-foreground hover:bg-secondary/80',
        destructive:
          'border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80',
        inverted: 'inverted',
      },
    },
    defaultVariants: {
      variant: 'default',
    },
  },
);

/**
 * Props for MultiSelect component
 */
interface MultiSelectProps
  extends
    React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof multiSelectVariants> {
  /**
   * An array of option objects to be displayed in the multi-select component.
   * Each option object has a label, value, and an optional icon.
   */
  options: (MultiSelectGroupOptionType | MultiSelectOptionType)[];

  /**
   * Callback function triggered when the selected values change.
   * Receives an array of the new selected values.
   */
  onValueChange: (value: string[]) => void;

  /** The default selected values when the component mounts. */
  defaultValue?: string[];

  /**
   * Placeholder text to be displayed when no values are selected.
   * Optional, defaults to "Select options".
   */
  placeholder?: string;

  /**
   * Animation duration in seconds for the visual effects (e.g., bouncing badges).
   * Optional, defaults to 0 (no animation).
   */
  animation?: number;

  /**
   * Maximum number of items to display. Extra selected items will be summarized.
   * Optional, defaults to 3.
   */
  maxCount?: number;

  /**
   * The modality of the popover. When set to true, interaction with outside elements
   * will be disabled and only popover content will be visible to screen readers.
   * Optional, defaults to false.
   */
  modalPopover?: boolean;

  /**
   * If true, renders the multi-select component as a child of another component.
   * Optional, defaults to false.
   */
  asChild?: boolean;

  /**
   * Additional class names to apply custom styles to the multi-select component.
   * Optional, can be used to add custom styles.
   */
  className?: string;

  /**
   * If true, renders the multi-select component with a select all option.
   */
  showSelectAll?: boolean;
  popoverTestId?: string;
  optionTestIdPrefix?: string;
  searchValue?: string;
  onSearchChange?: (value: string) => void;
  isSearching?: boolean;
  shouldFilter?: boolean;
  onListScroll?: (e: React.UIEvent<HTMLDivElement>) => void;
  /**
   * Optional label resolver for a selected value that is not present in
   * `options` (e.g. while options are narrowed by a search keyword). When it
   * returns a label, the badge shows that label instead of falling back to
   * the raw value with a warning marker.
   */
  getOptionLabel?: (value: string) => string | undefined;
}

export const MultiSelect = React.forwardRef<
  HTMLButtonElement,
  MultiSelectProps
>(
  (
    {
      options,
      onValueChange,
      variant,
      defaultValue = [],
      placeholder = t('common.selectPlaceholder'),
      animation = 0,
      maxCount = 3,
      modalPopover = false,
      // asChild = false,
      className,
      showSelectAll = true,
      popoverTestId,
      optionTestIdPrefix,
      searchValue,
      onSearchChange,
      isSearching = false,
      shouldFilter,
      onListScroll,
      getOptionLabel,
      ...props
    },
    ref,
  ) => {
    const [selectedValues, setSelectedValues] =
      React.useState<string[]>(defaultValue);
    const [isPopoverOpen, setIsPopoverOpen] = React.useState(false);
    const [isAnimating, setIsAnimating] = React.useState(false);
    const triggerId = React.useId();

    React.useEffect(() => {
      if (isEmpty(selectedValues) && !isEmpty(props.value)) {
        setSelectedValues(props.value as string[]);
      }
    }, [props.value, selectedValues]);

    React.useEffect(() => {
      if (
        isEmpty(selectedValues) &&
        isEmpty(props.value) &&
        !isEmpty(defaultValue)
      ) {
        setSelectedValues(defaultValue);
      }
    }, [defaultValue, props.value, selectedValues]);

    const flatOptions = React.useMemo(() => {
      return options.flatMap((option) =>
        'options' in option ? option.options : [option],
      );
    }, [options]);

    // Remember the label of every option the component has seen, so a badge
    // keeps its label when the option later disappears from `options` (e.g.
    // the list is narrowed by a server-side search).
    const rememberedLabels = React.useRef(new Map<string, React.ReactNode>());
    React.useEffect(() => {
      flatOptions.forEach((option) => {
        rememberedLabels.current.set(option.value, option.label);
      });
    }, [flatOptions]);

    const disabledValueSet = React.useMemo(() => {
      return new Set(
        flatOptions
          .filter((option) => option.disabled)
          .map((option) => option.value),
      );
    }, [flatOptions]);

    const lockedValueSet = React.useMemo(() => {
      return new Set(
        flatOptions
          .filter((option) => option.locked)
          .map((option) => option.value),
      );
    }, [flatOptions]);

    const selectableValues = React.useMemo(() => {
      return flatOptions
        .filter((option) => !option.disabled || option.locked)
        .map((option) => option.value);
    }, [flatOptions]);

    const allSelectableSelected =
      selectableValues.length > 0 &&
      selectableValues.every((value) => selectedValues.includes(value));

    // A disabled option can't be picked in the dropdown, but a value that is
    // already selected must stay removable — e.g. a knowledge base that had
    // chunks when it was picked may have been emptied since.
    // Locked options cannot be removed.
    const removeValue = (value: string) => {
      if (lockedValueSet.has(value)) {
        return;
      }
      const newSelectedValues = selectedValues.filter((v) => v !== value);
      setSelectedValues(newSelectedValues);
      onValueChange(newSelectedValues);
    };

    const handleInputKeyDown = (
      event: React.KeyboardEvent<HTMLInputElement>,
    ) => {
      if (event.key === 'Enter') {
        setIsPopoverOpen(true);
      } else if (event.key === 'Backspace' && !event.currentTarget.value) {
        const newSelectedValues = [...selectedValues];
        newSelectedValues.pop();
        setSelectedValues(newSelectedValues);
        onValueChange(newSelectedValues);
      }
    };

    const toggleOption = (option: string) => {
      if (disabledValueSet.has(option)) {
        return;
      }
      // Locked options cannot be deselected
      if (lockedValueSet.has(option) && selectedValues.includes(option)) {
        return;
      }

      const newSelectedValues = selectedValues.includes(option)
        ? selectedValues.filter((value) => value !== option)
        : [...selectedValues, option];
      setSelectedValues(newSelectedValues);
      onValueChange(newSelectedValues);
    };

    const handleClear = () => {
      // Keep locked options that are currently selected
      const lockedSelectedValues = selectedValues.filter((value) =>
        lockedValueSet.has(value),
      );
      setSelectedValues(lockedSelectedValues);
      onValueChange(lockedSelectedValues);
    };

    const handleTogglePopover = () => {
      setIsPopoverOpen((prev) => !prev);
    };

    const clearExtraOptions = () => {
      const newSelectedValues = selectedValues.slice(0, maxCount);
      setSelectedValues(newSelectedValues);
      onValueChange(newSelectedValues);
    };

    const toggleAll = () => {
      if (allSelectableSelected) {
        handleClear();
      } else {
        setSelectedValues(selectableValues);
        onValueChange(selectableValues);
      }
    };

    return (
      <Popover
        open={isPopoverOpen}
        onOpenChange={setIsPopoverOpen}
        modal={modalPopover}
      >
        <PopoverTrigger asChild>
          <Button
            ref={ref}
            {...props}
            // Own the trigger id, like `SelectWithSearch` does. Radix's Slot
            // lets child props win, so this keeps shadcn's `FormControl` from
            // putting the form item id here — otherwise a `FormLabel`
            // (`<label htmlFor>`) would forward its clicks to this button and
            // clicking the field label would open the popover.
            id={triggerId}
            onClick={handleTogglePopover}
            className={cn(
              'flex w-full p-1 rounded-md border border-border-button min-h-10 h-auto placeholder:text-text-disabled items-center justify-between bg-bg-input focus-visible:bg-bg-input hover:bg-bg-input [&_svg]:pointer-events-auto',
              className,
            )}
          >
            {selectedValues.length > 0 ? (
              <div className="flex justify-between items-center w-full group">
                <div className="flex flex-wrap items-center">
                  {selectedValues?.slice(0, maxCount)?.map((value) => {
                    const option = flatOptions.find((o) => o.value === value);
                    // A selected value may be absent from `options` while the
                    // list is narrowed (e.g. by a search keyword); fall back
                    // to the explicit `getOptionLabel` resolver and then to
                    // the remembered label of a previously seen option so the
                    // badge stays readable.
                    const label =
                      option?.label ??
                      getOptionLabel?.(value) ??
                      rememberedLabels.current.get(value);
                    const IconComponent = option?.icon;
                    return (
                      <Badge
                        key={value}
                        variant="secondary"
                        className={cn(
                          isAnimating ? 'animate-bounce' : '',
                          'px-1',
                          multiSelectVariants({ variant }),
                        )}
                        style={{ animationDuration: `${animation}s` }}
                      >
                        <div className="flex justify-between items-center gap-1">
                          {IconComponent && (
                            <IconComponent className="h-4 w-4" />
                          )}
                          {/* A selected value with no known label (e.g. the
                              entity no longer exists) gets a warning marker and
                              falls back to the raw value so the badge stays
                              readable and removable. */}
                          {label == null && (
                            <TriangleAlert className="h-4 w-4 flex-shrink-0 text-text-disabled" />
                          )}
                          <div
                            className={cn(
                              'max-w-28 text-ellipsis overflow-hidden',
                              { 'text-text-disabled': label == null },
                            )}
                          >
                            {label ?? value}
                          </div>
                          <XCircle
                            className="h-4 w-4 cursor-pointer"
                            onClick={(event) => {
                              event.stopPropagation();
                              removeValue(value);
                            }}
                          />
                        </div>
                      </Badge>
                    );
                  })}
                  {selectedValues.length > maxCount && (
                    <Badge
                      className={cn(
                        'bg-transparent text-foreground border-foreground/1 hover:bg-transparent',
                        isAnimating ? 'animate-bounce' : '',
                        multiSelectVariants({ variant }),
                      )}
                      style={{ animationDuration: `${animation}s` }}
                    >
                      {`+ ${selectedValues.length - maxCount} more`}
                      <XCircle
                        className="ml-2 h-4 w-4 cursor-pointer"
                        onClick={(event) => {
                          event.stopPropagation();
                          clearExtraOptions();
                        }}
                      />
                    </Badge>
                  )}
                </div>
                <div className="flex items-center justify-between ">
                  <XIcon
                    className="h-4 mx-2 cursor-pointer text-text-secondary hidden group-hover:block"
                    onClick={(event) => {
                      event.stopPropagation();
                      handleClear();
                    }}
                  />
                  <Separator
                    orientation="vertical"
                    className="min-h-6 h-full hidden group-hover:flex"
                  />
                  <ChevronDown className="h-4 mx-2 cursor-pointer text-text-secondary" />
                </div>
              </div>
            ) : (
              <div className="flex items-center justify-between w-full mx-auto">
                <span className="text-sm text-text-secondary mx-3">
                  {placeholder}
                </span>
                <ChevronDown className="h-4 cursor-pointer text-text-secondary mx-2" />
              </div>
            )}
          </Button>
        </PopoverTrigger>
        <PopoverContent
          className="w-auto min-w-[var(--radix-popover-trigger-width)] p-0"
          align="start"
          onEscapeKeyDown={() => setIsPopoverOpen(false)}
          onFocusOutside={(event) => event.preventDefault()}
          data-testid={popoverTestId}
        >
          <Command
            className="p-5 pb-8"
            shouldFilter={shouldFilter}
            filter={filterBySubstring}
          >
            {((options && options.length > 0) || onSearchChange) && (
              <CommandInput
                placeholder={t('common.search') + '...'}
                onKeyDown={handleInputKeyDown}
                value={searchValue}
                onValueChange={onSearchChange}
              />
            )}
            <CommandList className="mt-2" onScroll={onListScroll}>
              <CommandEmpty>
                {isSearching ? t('common.searching') : t('common.noDataFound')}
              </CommandEmpty>
              <CommandGroup>
                {showSelectAll && options && options.length > 0 && (
                  <CommandItem
                    key="all"
                    onSelect={toggleAll}
                    className="cursor-pointer"
                  >
                    <div
                      className={cn(
                        'mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary',
                        allSelectableSelected
                          ? 'bg-primary text-primary-foreground'
                          : 'opacity-50 [&_svg]:invisible',
                      )}
                    >
                      <CheckIcon className="h-4 w-4" />
                    </div>
                    <span>({t('common.selectAll')})</span>
                  </CommandItem>
                )}
                {!options.some((x) => 'options' in x) &&
                  (options as unknown as MultiSelectOptionType[]).map(
                    (option, idx) => {
                      const isSelected = selectedValues.includes(option.value);
                      return (
                        <MultiCommandItem
                          option={option}
                          key={option.value}
                          isSelected={isSelected}
                          toggleOption={toggleOption}
                          optionTestId={
                            optionTestIdPrefix
                              ? `${optionTestIdPrefix}-option-${idx}`
                              : undefined
                          }
                        ></MultiCommandItem>
                      );
                    },
                  )}
              </CommandGroup>
              {options.every((x) => 'options' in x) &&
                options.map((x, idx) => (
                  <CommandGroup heading={x.label} key={idx}>
                    {x.options.map((option, optIdx) => {
                      const isSelected = selectedValues.includes(option.value);

                      return (
                        <MultiCommandItem
                          option={option}
                          key={option.value}
                          isSelected={isSelected}
                          toggleOption={toggleOption}
                          optionTestId={
                            optionTestIdPrefix
                              ? `${optionTestIdPrefix}-option-${optIdx}`
                              : undefined
                          }
                        ></MultiCommandItem>
                      );
                    })}
                  </CommandGroup>
                ))}
              <CommandGroup>
                <div className=" absolute bottom-1 left-1 right-1 flex items-center justify-between mx-5 bg-bg-base border-t border-border-button">
                  <CommandSeparator />
                  {selectedValues.length > 0 && (
                    <>
                      <CommandItem
                        onSelect={handleClear}
                        className="flex-1 justify-center cursor-pointer"
                      >
                        {t('common.clear')}
                      </CommandItem>
                      <Separator
                        orientation="vertical"
                        className="flex min-h-6 h-full"
                      />
                    </>
                  )}
                  {options && options.length > 0 && (
                    <CommandItem
                      onSelect={() => setIsPopoverOpen(false)}
                      className="flex-1 justify-center cursor-pointer max-w-full"
                    >
                      {t('common.close')}
                    </CommandItem>
                  )}
                </div>
              </CommandGroup>
            </CommandList>
          </Command>
        </PopoverContent>
        {animation > 0 && selectedValues.length > 0 && (
          <WandSparkles
            className={cn(
              'cursor-pointer my-2 text-foreground bg-background w-3 h-3',
              isAnimating ? '' : 'text-muted-foreground',
            )}
            onClick={() => setIsAnimating(!isAnimating)}
          />
        )}
      </Popover>
    );
  },
);

MultiSelect.displayName = 'MultiSelect';