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,47 @@ 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 ( ) || [ null , null ] ) as [
157
+ number | null ,
158
+ number | null
159
+ ] ;
160
+
161
+ return {
162
+ annotation : undefined ,
163
+ isDisabled,
164
+ key : column . id ,
165
+ label : getColumnHeader ( column ) ,
166
+ max : minMax ?. [ 1 ] || Infinity ,
167
+ min : minMax ?. [ 0 ] || - Infinity ,
168
+ selectedMax : filterValue [ 1 ] ,
169
+ selectedMin : filterValue [ 0 ] ,
170
+ ...categoryConfig ,
171
+ } ;
172
+ }
173
+
174
+ /**
175
+ * Adapter for TanStack column to select category view.
129
176
* @param column - Column.
130
177
* @param categoryConfig - Category config.
131
- * @returns Category view.
178
+ * @returns Select category view.
132
179
*/
133
- function mapColumnToCategoryView < T extends RowData > (
180
+ function mapColumnToSelectCategoryView < T extends RowData > (
134
181
column : Column < T > ,
135
182
categoryConfig ?: CategoryConfig
136
- ) : CategoryView {
183
+ ) : SelectCategoryView {
137
184
const facetedUniqueValues = column . getFacetedUniqueValues ( ) ;
138
185
const isDisabled = facetedUniqueValues . size === 0 ;
139
186
const values = mapColumnToSelectCategoryValueView ( column ) ;
0 commit comments