import { orderBy, without } from 'lodash' import { Check, ChevronDown } from 'lucide-react' import { ReactNode, useState } from 'react' import { CommandEmpty_Shadcn_, CommandGroup_Shadcn_, CommandInput_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, Command_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_, Popover_Shadcn_, ScrollArea, cn, } from 'ui' import { BadgeDisabled, BadgeSelected } from './Badges' interface MultiSelectOption { id: string | number value: string name: string description?: string disabled: boolean } interface MultiSelectProps { value: string[] options: MultiSelectOption[] placeholder?: string | ReactNode searchPlaceholder?: string disabled?: boolean onChange?(x: string[]): void } /** * @deprecated Use ./multi-select instead */ export const MultiSelectV2 = ({ options, value, placeholder, searchPlaceholder = 'Search for option', disabled = false, onChange = () => {}, }: MultiSelectProps) => { const [open, setOpen] = useState(false) const [selected, setSelected] = useState(value || []) // Selected is `value` if defined, otherwise use local useState const selectedOptions = value || selected // Order the options so disabled items are at the beginning const formattedOptions = orderBy(options, ['disabled'], ['desc']) const checkIfActive = (option: MultiSelectOption) => { const isOptionSelected = (selectedOptions || []).find((x) => x === option.value) return isOptionSelected !== undefined } const handleChange = (option: MultiSelectOption) => { const _selected = selectedOptions const isActive = checkIfActive(option) const updatedPayload = isActive ? [...without(_selected, option.value)] : [..._selected.concat([option.value])] // Payload must always include disabled options const compulsoryOptions = options .filter((option) => option.disabled) .map((option) => option.name) const formattedPayload = [...new Set(updatedPayload.concat(compulsoryOptions))] setSelected(formattedPayload) onChange(formattedPayload) } return (
setOpen(true)} > {selectedOptions.length === 0 && placeholder && (
{placeholder}
)} {selectedOptions.map((value, idx) => { const id = `${value}-${idx}` const option = formattedOptions.find((x) => x.value === value) const isDisabled = option?.disabled ?? false if (!option) { return <> } else if (isDisabled) { return } else { return ( handleChange(option)} /> ) } })}
No options found 7 ? 'h-[210px]' : '')}> {formattedOptions?.map((option) => { const active = selectedOptions && selectedOptions.find((selected) => { return selected === option.value }) ? true : false return ( handleChange(option)} onSelect={() => handleChange(option)} >
{option.name} {active && }
) })}
) }