|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { useTranslation } from 'src/internal/i18n'; |
| 3 | +import { localeCompare } from 'src/utils/helpers'; |
| 4 | + |
| 5 | +import { |
| 6 | + Select, |
| 7 | + SelectOption, |
| 8 | + SelectOptionObject, |
| 9 | + SelectVariant, |
| 10 | + ToolbarChip, |
| 11 | + ToolbarFilter, |
| 12 | +} from '@patternfly/react-core'; |
| 13 | + |
| 14 | +import { FilterTypeProps } from './types'; |
| 15 | + |
| 16 | +/** |
| 17 | + * One label may map to multiple enum ids due to translation or by design (i.e. "Unknown") |
| 18 | + * Aggregate enums with the same label and display them as a single option. |
| 19 | + * |
| 20 | + * @returns { uniqueEnumLabels, onUniqueFilterUpdate, selectedUniqueEnumLabels }; |
| 21 | + */ |
| 22 | +export const useUnique = ({ |
| 23 | + supportedEnumValues, |
| 24 | + onSelectedEnumIdsChange, |
| 25 | + selectedEnumIds, |
| 26 | +}: { |
| 27 | + supportedEnumValues: { |
| 28 | + id: string; |
| 29 | + toLabel(t: (key: string) => string): string; |
| 30 | + }[]; |
| 31 | + onSelectedEnumIdsChange: (values: string[]) => void; |
| 32 | + selectedEnumIds: string[]; |
| 33 | +}): { |
| 34 | + uniqueEnumLabels: string[]; |
| 35 | + onUniqueFilterUpdate: (selectedEnumLabels: string[]) => void; |
| 36 | + selectedUniqueEnumLabels: string[]; |
| 37 | +} => { |
| 38 | + const { t, i18n } = useTranslation(); |
| 39 | + |
| 40 | + const translatedEnums = useMemo( |
| 41 | + () => |
| 42 | + supportedEnumValues.map((it) => ({ |
| 43 | + // fallback to ID |
| 44 | + label: it.toLabel?.(t) ?? it.id, |
| 45 | + id: it.id, |
| 46 | + })), |
| 47 | + |
| 48 | + [supportedEnumValues], |
| 49 | + ); |
| 50 | + |
| 51 | + // group filters with the same label |
| 52 | + const labelToIds = useMemo( |
| 53 | + () => |
| 54 | + translatedEnums.reduce((acc, { label, id }) => { |
| 55 | + acc[label] = [...(acc?.[label] ?? []), id]; |
| 56 | + return acc; |
| 57 | + }, {}), |
| 58 | + [translatedEnums], |
| 59 | + ); |
| 60 | + |
| 61 | + // for easy reverse lookup |
| 62 | + const idToLabel = useMemo( |
| 63 | + () => |
| 64 | + translatedEnums.reduce((acc, { label, id }) => { |
| 65 | + acc[id] = label; |
| 66 | + return acc; |
| 67 | + }, {}), |
| 68 | + [translatedEnums], |
| 69 | + ); |
| 70 | + |
| 71 | + const uniqueEnumLabels = useMemo( |
| 72 | + () => |
| 73 | + Object.entries(labelToIds) |
| 74 | + .map(([label]) => label) |
| 75 | + .sort((a, b) => localeCompare(a, b, i18n.resolvedLanguage)), |
| 76 | + [labelToIds], |
| 77 | + ); |
| 78 | + |
| 79 | + const onUniqueFilterUpdate = useMemo( |
| 80 | + () => |
| 81 | + (labels: string[]): void => |
| 82 | + onSelectedEnumIdsChange(labels.flatMap((label) => labelToIds[label] ?? [])), |
| 83 | + [onSelectedEnumIdsChange, labelToIds], |
| 84 | + ); |
| 85 | + |
| 86 | + const selectedUniqueEnumLabels = useMemo( |
| 87 | + () => [...new Set(selectedEnumIds.map((id) => idToLabel[id]).filter(Boolean))] as string[], |
| 88 | + [selectedEnumIds, idToLabel], |
| 89 | + ); |
| 90 | + |
| 91 | + return { uniqueEnumLabels, onUniqueFilterUpdate, selectedUniqueEnumLabels }; |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Select one or many enum values from the list. |
| 96 | + * FilterTypeProps are interpeted as follows: |
| 97 | + * 1) selectedFilters - selected enum IDs (not translated constant identifiers) |
| 98 | + * 2) onFilterUpdate - accepts the list of selected enum IDs |
| 99 | + * 3) supportedValues - supported enum values |
| 100 | + */ |
| 101 | +export const EnumFilter = ({ |
| 102 | + selectedFilters: selectedEnumIds = [], |
| 103 | + onFilterUpdate: onSelectedEnumIdsChange, |
| 104 | + supportedValues: supportedEnumValues = [], |
| 105 | + title, |
| 106 | + placeholderLabel, |
| 107 | + filterId, |
| 108 | + showFilter, |
| 109 | +}: FilterTypeProps) => { |
| 110 | + const [isExpanded, setExpanded] = useState(false); |
| 111 | + const { uniqueEnumLabels, onUniqueFilterUpdate, selectedUniqueEnumLabels } = useUnique({ |
| 112 | + supportedEnumValues, |
| 113 | + onSelectedEnumIdsChange, |
| 114 | + selectedEnumIds, |
| 115 | + }); |
| 116 | + |
| 117 | + const deleteFilter = (label: string | ToolbarChip | SelectOptionObject): void => |
| 118 | + onUniqueFilterUpdate(selectedUniqueEnumLabels.filter((filterLabel) => filterLabel !== label)); |
| 119 | + |
| 120 | + const hasFilter = (label: string | SelectOptionObject): boolean => |
| 121 | + !!selectedUniqueEnumLabels.find((filterLabel) => filterLabel === label); |
| 122 | + |
| 123 | + const addFilter = (label: string | SelectOptionObject): void => { |
| 124 | + if (typeof label === 'string') { |
| 125 | + onUniqueFilterUpdate([...selectedUniqueEnumLabels, label]); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + return ( |
| 130 | + <ToolbarFilter |
| 131 | + key={filterId} |
| 132 | + chips={selectedUniqueEnumLabels} |
| 133 | + deleteChip={(category, option) => deleteFilter(option)} |
| 134 | + deleteChipGroup={() => onUniqueFilterUpdate([])} |
| 135 | + categoryName={title} |
| 136 | + showToolbarItem={showFilter} |
| 137 | + > |
| 138 | + <Select |
| 139 | + variant={SelectVariant.checkbox} |
| 140 | + aria-label={placeholderLabel} |
| 141 | + onSelect={(event, option, isPlaceholder) => { |
| 142 | + if (isPlaceholder) { |
| 143 | + return; |
| 144 | + } |
| 145 | + hasFilter(option) ? deleteFilter(option) : addFilter(option); |
| 146 | + }} |
| 147 | + selections={selectedUniqueEnumLabels} |
| 148 | + placeholderText={placeholderLabel} |
| 149 | + isOpen={isExpanded} |
| 150 | + onToggle={setExpanded} |
| 151 | + > |
| 152 | + {uniqueEnumLabels.map((label) => ( |
| 153 | + <SelectOption key={label} value={label} /> |
| 154 | + ))} |
| 155 | + </Select> |
| 156 | + </ToolbarFilter> |
| 157 | + ); |
| 158 | +}; |
0 commit comments