import React, { HTMLAttributes } from "react"; import { useMultipleSelection } from "downshift"; import classNames from "classnames"; import { Field } from ":/components/Forms/Field"; import { LabelledBox } from ":/components/Forms/LabelledBox"; import { Button } from ":/components/Button"; import { useCunningham } from ":/components/Provider"; import { Option, SelectProps } from ":/components/Forms/Select"; import { getOptionsFilter, optionToValue, renderOption, } from ":/components/Forms/Select/mono-common"; import { SelectedOption } from ":/components/Forms/Select/utils"; /** * This method returns a comparator that can be used to filter out options for multi select. * For an option to be visible it must: * - Match the input value in terms of search * - Not be selected already * * @param selectedOptions * @param inputValue */ export function getMultiOptionsFilter( selectedOptions: Option[], inputValue?: string, ) { const optionsFilter = getOptionsFilter(inputValue); return (option: Option) => { return ( !selectedOptions.find( (selectedOption) => optionToValue(selectedOption) === optionToValue(option), ) && optionsFilter(option) ); }; } export type SubProps = Omit & { onChange?: (event: { target: { value: string[] } }) => void; onSelectedItemsChange: (selectedItems: Option[]) => void; selectedItems: Option[]; }; export const SelectMultiAux = ({ options, labelAsPlaceholder, selectedItems, clearable = true, disabled, hideLabel, name, downshiftReturn, useMultipleSelectionReturn, children, ...props }: SubProps & { options: Option[]; labelAsPlaceholder: boolean; selectedItems: Option[]; clearable?: boolean; downshiftReturn: { isOpen: boolean; getLabelProps: any; toggleButtonProps: any; getMenuProps: any; getItemProps: any; highlightedIndex: number; wrapperProps?: HTMLAttributes; }; useMultipleSelectionReturn: ReturnType>; }) => { const { t } = useCunningham(); const labelProps = downshiftReturn.getLabelProps(); return (
0, }, )} >
{selectedItems.map((selectedItem, index) => ( ))}
{clearable && !disabled && selectedItems.length > 0 && ( <>
{selectedItems.map((selectedItemForRender, index) => { return (
); })} {children}
    {downshiftReturn.isOpen && ( <> {options.map((option, index) => { const isActive = index === downshiftReturn.highlightedIndex; return (
  • {renderOption(option)}
  • ); })} {options.length === 0 && (
  • {t("components.forms.select.menu_empty_placeholder")}
  • )} )}
); };