1
1
import { Column , RowData , Table } from "@tanstack/react-table" ;
2
+ import { isRangeCategoryConfig } from "../../../../../../common/categories/config/range/typeGuards" ;
2
3
import { CategoryConfig } from "../../../../../../common/categories/config/types" ;
4
+ import { RangeCategoryView } from "../../../../../../common/categories/views/range/types" ;
3
5
import { CategoryView } from "../../../../../../common/categories/views/types" ;
4
- import { SelectCategoryValueView } from "../../../../../../common/entities" ;
6
+ import {
7
+ SelectCategoryValueView ,
8
+ SelectCategoryView ,
9
+ } from "../../../../../../common/entities" ;
5
10
import { CategoryGroup } from "../../../../../../config/entities" ;
6
11
import { getColumnHeader } from "../../../../../Table/common/utils" ;
7
12
import { getSortedFacetedValues } from "../../../../../Table/featureOptions/facetedColumn/utils" ;
@@ -113,7 +118,17 @@ function mapCategoryFilter<T extends RowData>(
113
118
const column = table . getColumn ( categoryConfig . key ) ;
114
119
if ( ! column ) return acc ;
115
120
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
+
117
132
return [ ...acc , categoryView ] ;
118
133
} ,
119
134
[ ]
@@ -125,15 +140,44 @@ function mapCategoryFilter<T extends RowData>(
125
140
}
126
141
127
142
/**
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.
129
173
* @param column - Column.
130
174
* @param categoryConfig - Category config.
131
- * @returns Category view.
175
+ * @returns Select category view.
132
176
*/
133
- function mapColumnToCategoryView < T extends RowData > (
177
+ function mapColumnToSelectCategoryView < T extends RowData > (
134
178
column : Column < T > ,
135
179
categoryConfig ?: CategoryConfig
136
- ) : CategoryView {
180
+ ) : SelectCategoryView {
137
181
const facetedUniqueValues = column . getFacetedUniqueValues ( ) ;
138
182
const isDisabled = facetedUniqueValues . size === 0 ;
139
183
const values = mapColumnToSelectCategoryValueView ( column ) ;
0 commit comments