import React, { HTMLAttributes, useRef } 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, } from ":/components/Forms/Select/mono-common"; import { SelectedItems } from ":/components/Forms/Select/multi-selected-items"; import { SelectMultiMenu } from ":/components/Forms/Select/multi-menu"; /** * 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 interface SelectMultiAuxProps extends 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>; } export const SelectMultiAux = ({ children, ...props }: SelectMultiAuxProps) => { const { t } = useCunningham(); const labelProps = props.downshiftReturn.getLabelProps(); const ref = useRef(null); // We need to remove onBlur from toggleButtonProps because it triggers a menu closing each time // we tick a checkbox using the monoline style. const { onBlur, ...toggleProps } = props.downshiftReturn.toggleButtonProps; return ( <>
0, "c__select--monoline": props.monoline, "c__select--multiline": !props.monoline, } )} >
{props.selectedItems.map((selectedItem, index) => ( ))}
{props.clearable && !props.disabled && props.selectedItems.length > 0 && ( <>
{children}
); };