Skip to content

✨ Refine tag name filter #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
116 changes: 90 additions & 26 deletions client/src/app/components/FilterToolbar/MultiselectFilterControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ToolbarChip,
ToolbarFilter,
} from "@patternfly/react-core";
import { CaretDownIcon, CaretRightIcon } from "@patternfly/react-icons";
import { IFilterControlProps } from "./FilterControl";
import {
FilterSelectOptionProps,
Expand Down Expand Up @@ -229,6 +230,24 @@ export const MultiselectFilterControl = <TItem,>({
const withGroupPrefix = (group: string) =>
group === category.title ? group : `${category.title}/${group}`;

const groups = Array.from(
new Set(
filteredOptions
.filter((item) => item && item.groupLabel)
.map((item) => item.groupLabel)
)
);

const [openGroups, setOpenGroups] = React.useState<string[]>([]);

const toggleGroup = (groupLabel: string) => {
setOpenGroups((prev) =>
prev.includes(groupLabel)
? prev.filter((label) => label !== groupLabel)
: [...prev, groupLabel]
);
};

return (
<>
{
Expand All @@ -250,35 +269,80 @@ export const MultiselectFilterControl = <TItem,>({
onSelect={(_, selection) => onSelect(selection as string)}
isOpen={isFilterDropdownOpen}
>
<SelectList id={withPrefix("select-typeahead-listbox")}>
{filteredOptions.map(
({ groupLabel, label, value, optionProps = {} }, index) => (
{groups && groups.length > 0 && (
<SelectList id={withPrefix("select-typeahead-listbox")}>
{groups.map((groupLabel) => (
<React.Fragment key={groupLabel}>
<div
onClick={(event) => {
toggleGroup(groupLabel!);
}}
>
<SelectOption
isDisabled
hasCheckbox={false}
>
{openGroups.includes(groupLabel!) ? (
<CaretDownIcon />
) : (
<CaretRightIcon />
)}
<strong>{groupLabel}</strong>
</SelectOption>
</div>
{openGroups.includes(groupLabel!) &&
filteredOptions
.filter((option) => option.groupLabel === groupLabel)
.map(({ label, value, optionProps = {} }, index) => (
<SelectOption
{...optionProps}
{...(!optionProps.isDisabled && {
hasCheckbox: true,
})}
key={value}
id={withPrefix(`option-${index}`)}
value={value}
isFocused={focusedItemIndex === index}
isSelected={filterValue?.includes(value)}
>
{label ?? value}
</SelectOption>
))}
</React.Fragment>
))}
</SelectList>
)}
{(!groups || groups.length == 0) && (
<SelectList id={withPrefix("select-typeahead-listbox")}>
{filteredOptions.map(
({ groupLabel, label, value, optionProps = {} }, index) => (
<SelectOption
{...optionProps}
{...(!optionProps.isDisabled && { hasCheckbox: true })}
key={value}
id={withPrefix(`option-${index}`)}
value={value}
isFocused={focusedItemIndex === index}
isSelected={filterValue?.includes(value)}
>
{!!groupLabel && <Label>{groupLabel}</Label>}{" "}
{label ?? value}
</SelectOption>
)
)}
{filteredOptions.length === 0 && (
<SelectOption
{...optionProps}
{...(!optionProps.isDisabled && { hasCheckbox: true })}
key={value}
id={withPrefix(`option-${index}`)}
value={value}
isFocused={focusedItemIndex === index}
isSelected={filterValue?.includes(value)}
isDisabled
hasCheckbox={false}
key={NO_RESULTS}
value={NO_RESULTS}
isSelected={false}
>
{!!groupLabel && <Label>{groupLabel}</Label>}{" "}
{label ?? value}
{`No results found for "${inputValue}"`}
</SelectOption>
)
)}
{filteredOptions.length === 0 && (
<SelectOption
isDisabled
hasCheckbox={false}
key={NO_RESULTS}
value={NO_RESULTS}
isSelected={false}
>
{`No results found for "${inputValue}"`}
</SelectOption>
)}
</SelectList>
)}
</SelectList>
)}
</Select>
</ToolbarFilter>
}
Expand Down
Loading