Skip to content

Commit 80a093f

Browse files
Fran McDadefrano-m
authored andcommitted
feat: update columnfiltersadapter to handle range category filtering (#634)
1 parent 23c54e9 commit 80a093f

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { RangeCategoryView } from "../../views/range/types";
2+
import { VIEW_KIND } from "../../views/types";
3+
import { CategoryConfig } from "../types";
4+
5+
/**
6+
* Determine if the given category config is a range category.
7+
* @param categoryConfig - Category config.
8+
* @returns True if the category config is a range category, false otherwise.
9+
*/
10+
export function isRangeCategoryConfig(
11+
categoryConfig: CategoryConfig
12+
): categoryConfig is RangeCategoryView {
13+
return categoryConfig.viewKind === VIEW_KIND.RANGE;
14+
}

src/components/Filter/components/adapters/tanstack/ColumnFiltersAdapter/columnFiltersAdapter.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RowData } from "@tanstack/react-table";
22
import { useCallback } from "react";
3+
import { VIEW_KIND } from "../../../../../../common/categories/views/types";
34
import {
45
CategoryKey,
56
CategoryValueKey,
@@ -22,13 +23,23 @@ export const ColumnFiltersAdapter = <T extends RowData>({
2223
categoryKey: CategoryKey | ClearAll,
2324
selectedCategoryValue: CategoryValueKey,
2425
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- `selected` is not required by TanStack adapter.
25-
_selected: boolean
26+
_selected: boolean,
27+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- `categorySection` is not required by TanStack adapter.
28+
_categorySection?: string,
29+
viewKind?: VIEW_KIND
2630
) => {
2731
if (categoryKey === CLEAR_ALL) {
2832
table.resetColumnFilters();
2933
return;
3034
}
3135

36+
// Range filters use direct value assignment (e.g., [min, max] tuple for numeric ranges).
37+
if (viewKind === VIEW_KIND.RANGE) {
38+
table.getColumn(categoryKey)?.setFilterValue(selectedCategoryValue);
39+
return;
40+
}
41+
42+
// Select filters use an updater function to toggle individual values in/out of the filter array.
3243
table
3344
.getColumn(categoryKey)
3445
?.setFilterValue(updater(selectedCategoryValue));

src/components/Filter/components/adapters/tanstack/ColumnFiltersAdapter/utils.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Column, RowData, Table } from "@tanstack/react-table";
2+
import { isRangeCategoryConfig } from "../../../../../../common/categories/config/range/typeGuards";
23
import { CategoryConfig } from "../../../../../../common/categories/config/types";
4+
import { RangeCategoryView } from "../../../../../../common/categories/views/range/types";
35
import { CategoryView } from "../../../../../../common/categories/views/types";
4-
import { SelectCategoryValueView } from "../../../../../../common/entities";
6+
import {
7+
SelectCategoryValueView,
8+
SelectCategoryView,
9+
} from "../../../../../../common/entities";
510
import { CategoryGroup } from "../../../../../../config/entities";
611
import { getColumnHeader } from "../../../../../Table/common/utils";
712
import { getSortedFacetedValues } from "../../../../../Table/featureOptions/facetedColumn/utils";
@@ -113,7 +118,17 @@ function mapCategoryFilter<T extends RowData>(
113118
const column = table.getColumn(categoryConfig.key);
114119
if (!column) return acc;
115120
if (!column.getCanFilter()) return acc;
116-
const categoryView = mapColumnToCategoryView(column, categoryConfig);
121+
122+
let categoryView: CategoryView;
123+
124+
if (isRangeCategoryConfig(categoryConfig)) {
125+
// Build range category view.
126+
categoryView = mapColumnToRangeCategoryView(column, categoryConfig);
127+
} else {
128+
// Build select category view.
129+
categoryView = mapColumnToSelectCategoryView(column, categoryConfig);
130+
}
131+
117132
return [...acc, categoryView];
118133
},
119134
[]
@@ -125,15 +140,44 @@ function mapCategoryFilter<T extends RowData>(
125140
}
126141

127142
/**
128-
* Adapter for TanStack column to category view.
143+
* Adapter for TanStack column to range category view.
144+
* @param column - Column.
145+
* @param categoryConfig - Category config.
146+
* @returns Range category view.
147+
*/
148+
function mapColumnToRangeCategoryView<T extends RowData>(
149+
column: Column<T>,
150+
categoryConfig?: CategoryConfig
151+
): RangeCategoryView {
152+
const minMax = column.getFacetedMinMaxValues();
153+
const isDisabled = !minMax;
154+
155+
// Selected values for the column.
156+
const filterValue = (column.getFilterValue() || []) as [number, number];
157+
158+
return {
159+
annotation: undefined,
160+
isDisabled,
161+
key: column.id,
162+
label: getColumnHeader(column),
163+
max: minMax?.[1] || Infinity,
164+
min: minMax?.[0] || -Infinity,
165+
selectedMax: filterValue[1],
166+
selectedMin: filterValue[0],
167+
...categoryConfig,
168+
};
169+
}
170+
171+
/**
172+
* Adapter for TanStack column to select category view.
129173
* @param column - Column.
130174
* @param categoryConfig - Category config.
131-
* @returns Category view.
175+
* @returns Select category view.
132176
*/
133-
function mapColumnToCategoryView<T extends RowData>(
177+
function mapColumnToSelectCategoryView<T extends RowData>(
134178
column: Column<T>,
135179
categoryConfig?: CategoryConfig
136-
): CategoryView {
180+
): SelectCategoryView {
137181
const facetedUniqueValues = column.getFacetedUniqueValues();
138182
const isDisabled = facetedUniqueValues.size === 0;
139183
const values = mapColumnToSelectCategoryValueView(column);

0 commit comments

Comments
 (0)