|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { Dropdown, DropdownItem, MenuToggle, ToolbarItem, ToolbarToggleGroup } from "@patternfly/react-core"; |
| 4 | +import { FilterIcon } from "@patternfly/react-icons"; |
| 5 | + |
| 6 | +import type { IFilterCategory } from "@app/hooks/TableControls/filtering/types"; |
| 7 | + |
| 8 | +export interface IFilterToolbarProps<TItem, TFilterCategoryKey extends string> { |
| 9 | + currentFilterCategoryKey?: TFilterCategoryKey; |
| 10 | + setCurrentFilterCategoryKey: (value: TFilterCategoryKey) => void; |
| 11 | + categoryTitles: Record<TFilterCategoryKey, string>; |
| 12 | + filterCategories: IFilterCategory<TItem, TFilterCategoryKey>[]; |
| 13 | + showFilterDropdown?: boolean; |
| 14 | + isDisabled?: boolean; |
| 15 | + children?: React.ReactNode; |
| 16 | +} |
| 17 | + |
| 18 | +export const FilterToolbar = <TItem, TFilterCategoryKey extends string>({ |
| 19 | + currentFilterCategoryKey, |
| 20 | + setCurrentFilterCategoryKey, |
| 21 | + filterCategories, |
| 22 | + categoryTitles, |
| 23 | + showFilterDropdown, |
| 24 | + isDisabled, |
| 25 | + children, |
| 26 | +}: IFilterToolbarProps<TItem, TFilterCategoryKey>) => { |
| 27 | + const [isCategoryDropdownOpen, setIsCategoryDropdownOpen] = React.useState(false); |
| 28 | + |
| 29 | + const onCategorySelect = (category: IFilterCategory<TItem, TFilterCategoryKey>) => { |
| 30 | + setCurrentFilterCategoryKey(category.categoryKey); |
| 31 | + setIsCategoryDropdownOpen(false); |
| 32 | + }; |
| 33 | + |
| 34 | + const currentFilterCategory = filterCategories.find((category) => category.categoryKey === currentFilterCategoryKey); |
| 35 | + |
| 36 | + const renderDropdownItems = () => { |
| 37 | + return filterCategories.map((category) => ( |
| 38 | + <DropdownItem |
| 39 | + id={`filter-category-${category.categoryKey}`} |
| 40 | + key={category.categoryKey} |
| 41 | + onClick={() => onCategorySelect(category)} |
| 42 | + > |
| 43 | + {categoryTitles[category.categoryKey]} |
| 44 | + </DropdownItem> |
| 45 | + )); |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <ToolbarToggleGroup |
| 50 | + variant="filter-group" |
| 51 | + toggleIcon={<FilterIcon />} |
| 52 | + breakpoint="2xl" |
| 53 | + gap={showFilterDropdown ? { default: "gapMd" } : undefined} |
| 54 | + > |
| 55 | + {showFilterDropdown && ( |
| 56 | + <ToolbarItem> |
| 57 | + <Dropdown |
| 58 | + toggle={(toggleRef) => ( |
| 59 | + <MenuToggle |
| 60 | + id="filtered-by" |
| 61 | + ref={toggleRef} |
| 62 | + onClick={() => setIsCategoryDropdownOpen(!isCategoryDropdownOpen)} |
| 63 | + isDisabled={isDisabled} |
| 64 | + > |
| 65 | + <FilterIcon /> {currentFilterCategory ? categoryTitles[currentFilterCategory.categoryKey] : null} |
| 66 | + </MenuToggle> |
| 67 | + )} |
| 68 | + isOpen={isCategoryDropdownOpen} |
| 69 | + > |
| 70 | + {renderDropdownItems()} |
| 71 | + </Dropdown> |
| 72 | + </ToolbarItem> |
| 73 | + )} |
| 74 | + {children} |
| 75 | + </ToolbarToggleGroup> |
| 76 | + ); |
| 77 | +}; |
0 commit comments