import * as React from 'react';
import { css } from '@emotion/css';
import { CloseButton, Combobox, Group, Input, Pill, PillsInput, ScrollArea, Stack, Text, Tooltip, useCombobox } from '@mantine/core';
import { ColumnInfo, EColumnTypes, VisColumn } from '../interfaces';
export function MultiSelect({
callback,
columns,
currentSelected,
columnType = null,
label = '',
}: {
callback: (value: ColumnInfo[]) => void;
columns: VisColumn[];
currentSelected: ColumnInfo[];
columnType?: EColumnTypes;
label?: string;
}) {
const filteredColumns = React.useMemo(() => {
return columns.filter((c) => (columnType ? c.type === columnType : true));
}, [columnType, columns]);
const combobox = useCombobox({
onDropdownClose: () => combobox.resetSelectedOption(),
onDropdownOpen: () => combobox.updateSelectedOptionIndex('active'),
});
const handleValueRemove = (id: string) => {
callback(currentSelected.filter((s) => s.id !== id));
};
const handleValueSelect = (id: string) => {
const itemToAdd = filteredColumns.find((c) => c.info.id === id);
currentSelected.find((c) => c.id === id) ? handleValueRemove(itemToAdd.info.id) : callback([...currentSelected, itemToAdd.info]);
};
const handleValueRemoveAll = () => {
callback([]);
};
const options = filteredColumns
.filter((c) => !currentSelected.map((s) => s.id).includes(c.info.id))
.map((item) => {
return (
{item.info.name}
{item.info.description}
}
>
{currentSelected.map((c) => c.id).includes(item.info.id)}
{item.info.name}
{item.info.description}
);
});
const values = currentSelected.map((item) => (
{item.name}
{item.description}
}
>
{
handleValueRemove(item.id);
}}
h={item.description ? 35 : 20}
style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}
>
{item.name}
{item.description ? (
{item.description}
) : null}
));
return (
}
label={!label ? `${columnType} columns` : label}
pointer
onClick={() => combobox.toggleDropdown()}
>
{values.length > 0 ? values : Select columns}
combobox.closeDropdown()}
onKeyDown={(event) => {
if (event.key === 'Backspace') {
event.preventDefault();
handleValueRemove(currentSelected[currentSelected.length - 1].id);
}
}}
/>
{options.length === 0 ? All options selected : options}
);
}