Skip to content

Commit

Permalink
Cleanup multiselect filter control
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Feb 15, 2024
1 parent b5c6083 commit 284e089
Showing 1 changed file with 9 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
SelectGroup,
SelectList,
SelectOption,
SelectOptionProps,
TextInputGroup,
TextInputGroupMain,
TextInputGroupUtilities,
Expand Down Expand Up @@ -49,7 +48,9 @@ export const MultiselectFilterControl = <TItem,>({
const [selectOptions, setSelectOptions] = React.useState<
FilterSelectOptionProps[]
>(Array.isArray(category.selectOptions) ? category.selectOptions : []);

const hasGroupings = !Array.isArray(selectOptions);

const flatOptions: FilterSelectOptionProps[] = !hasGroupings
? selectOptions
: (Object.values(selectOptions).flatMap(
Expand All @@ -70,13 +71,6 @@ export const MultiselectFilterControl = <TItem,>({
const textInputRef = React.useRef<HTMLInputElement>();
const [inputValue, setInputValue] = React.useState<string>("");

const getOptionKeyFromOptionValue = (
optionValue: string | SelectOptionProps
) => flatOptions.find((option) => option?.value === optionValue)?.key;

const getOptionValueFromOptionKey = (optionKey: string) =>
flatOptions.find(({ key }) => key === optionKey)?.value;

const onFilterClear = (chip: string | ToolbarChip) => {
const chipKey = typeof chip === "string" ? chip : chip.key;
const newFilterValue = filterValue
Expand All @@ -86,15 +80,12 @@ export const MultiselectFilterControl = <TItem,>({
setFilterValue(newFilterValue);
};

// Select expects "selections" to be an array of the "value" props from the relevant optionProps
const selections = filterValue?.map(getOptionValueFromOptionKey) ?? [];

/*
* Note: Chips can be a `ToolbarChip` or a plain `string`. Use a hack to split a
* selected option in 2 parts. Assuming the option is in the format "Group / Item"
* break the text and show a chip with the Item and the Group as a tooltip.
*/
const chips = selections.map((s, index) => {
const chips = filterValue?.map((s, index) => {
const chip: string = s?.toString() ?? "";
const idx = chip.indexOf(CHIP_BREAK_DELINEATOR);

Expand Down Expand Up @@ -213,7 +204,7 @@ export const MultiselectFilterControl = <TItem,>({
newSelectOptions = [
{
key: "no-results",
isDisabled: true,
isDisabled: false,
children: `No results found for "${inputValue}"`,
value: "No results",
},
Expand Down Expand Up @@ -253,27 +244,11 @@ export const MultiselectFilterControl = <TItem,>({
setSelectOptions(newSelectOptions);
setIsFilterDropdownOpen(true);

if (
isFilterDropdownOpen &&
selectedItem &&
selectedItem.value !== "no results"
) {
setInputValue("");

const newFilterValue = [...(filterValue || [])];
const optionValue = getOptionValueFromOptionKey(selectedItem.value);

if (newFilterValue.includes(optionValue)) {
const indexToRemove = newFilterValue.indexOf(optionValue);
newFilterValue.splice(indexToRemove, 1);
} else {
newFilterValue.push(optionValue);
}

setFilterValue(newFilterValue);
setIsFilterDropdownOpen(false);
if (!isFilterDropdownOpen) {
setIsFilterDropdownOpen((prev) => !prev);
} else if (selectedItem && selectedItem.value !== "No results") {
onSelect(selectedItem.value);
}

break;
case "Tab":
case "Escape":
Expand Down Expand Up @@ -303,7 +278,7 @@ export const MultiselectFilterControl = <TItem,>({
setIsFilterDropdownOpen(!isFilterDropdownOpen);
}}
isExpanded={isFilterDropdownOpen}
isDisabled={isDisabled || !selectOptions.length}
isDisabled={isDisabled || !category.selectOptions.length}
isFullWidth
>
<TextInputGroup isPlain>
Expand Down

0 comments on commit 284e089

Please sign in to comment.