@@ -180,10 +180,18 @@ export class DataTable<T> {
180
180
* The current filter state for all columns.
181
181
* @returns {{ [K in keyof T]: Set<any> } } An object representing the filter state that maps column keys to filter values.
182
182
*/
183
- get filters ( ) {
183
+ get filterState ( ) {
184
184
return this . #filterState;
185
185
}
186
186
187
+ /**
188
+ * The current sort state for the table.
189
+ * @returns {{ column: keyof T | null; direction: SortDirection } } An object representing the sort state with a column key and direction.
190
+ */
191
+ get sortState ( ) {
192
+ return this . #sortState;
193
+ }
194
+
187
195
/**
188
196
* The total number of pages based on the current filters and page size.
189
197
* @returns {number } The total number of pages.
@@ -293,7 +301,7 @@ export class DataTable<T> {
293
301
*/
294
302
setFilter = < K extends keyof T > ( column : K , values : any [ ] ) => {
295
303
this . #isFilterDirty = true ;
296
- this . #filterState[ column ] = new Set ( values ) ;
304
+ this . #filterState = { ... this . #filterState , [ column ] : new Set ( values ) } ;
297
305
this . #currentPage = 1 ;
298
306
} ;
299
307
@@ -303,7 +311,7 @@ export class DataTable<T> {
303
311
*/
304
312
clearFilter = ( column : keyof T ) => {
305
313
this . #isFilterDirty = true ;
306
- this . #filterState[ column ] = new Set ( ) ;
314
+ this . #filterState = { ... this . #filterState , [ column ] : new Set ( ) } ;
307
315
this . #currentPage = 1 ;
308
316
} ;
309
317
@@ -315,15 +323,13 @@ export class DataTable<T> {
315
323
*/
316
324
toggleFilter = < K extends keyof T > ( column : K , value : any ) => {
317
325
this . #isFilterDirty = true ;
318
- const currentFilter = this . #filterState[ column ] ;
319
-
320
- if ( currentFilter . has ( value ) ) {
321
- currentFilter . delete ( value ) ;
322
- } else {
323
- currentFilter . add ( value ) ;
324
- }
326
+ this . #filterState = {
327
+ ...this . #filterState,
328
+ [ column ] : this . isFilterActive ( column , value )
329
+ ? new Set ( [ ...this . #filterState[ column ] ] . filter ( ( v ) => v !== value ) )
330
+ : new Set ( [ ...this . #filterState[ column ] , value ] )
331
+ } ;
325
332
326
- this . #filterState[ column ] = new Set ( currentFilter ) ;
327
333
this . #currentPage = 1 ;
328
334
} ;
329
335
0 commit comments