Skip to content

Commit 26bea0d

Browse files
committed
filterState, sortState, and object assignment
1 parent 1cbc624 commit 26bea0d

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

.changeset/violet-donkeys-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@careswitch/svelte-data-table': minor
3+
---
4+
5+
Rename `filters` to `filterState`; introduce `sortState`, refactor filter mutators to reassign object

src/lib/DataTable.svelte.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,18 @@ export class DataTable<T> {
180180
* The current filter state for all columns.
181181
* @returns {{ [K in keyof T]: Set<any> }} An object representing the filter state that maps column keys to filter values.
182182
*/
183-
get filters() {
183+
get filterState() {
184184
return this.#filterState;
185185
}
186186

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+
187195
/**
188196
* The total number of pages based on the current filters and page size.
189197
* @returns {number} The total number of pages.
@@ -293,7 +301,7 @@ export class DataTable<T> {
293301
*/
294302
setFilter = <K extends keyof T>(column: K, values: any[]) => {
295303
this.#isFilterDirty = true;
296-
this.#filterState[column] = new Set(values);
304+
this.#filterState = { ...this.#filterState, [column]: new Set(values) };
297305
this.#currentPage = 1;
298306
};
299307

@@ -303,7 +311,7 @@ export class DataTable<T> {
303311
*/
304312
clearFilter = (column: keyof T) => {
305313
this.#isFilterDirty = true;
306-
this.#filterState[column] = new Set();
314+
this.#filterState = { ...this.#filterState, [column]: new Set() };
307315
this.#currentPage = 1;
308316
};
309317

@@ -315,15 +323,13 @@ export class DataTable<T> {
315323
*/
316324
toggleFilter = <K extends keyof T>(column: K, value: any) => {
317325
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+
};
325332

326-
this.#filterState[column] = new Set(currentFilter);
327333
this.#currentPage = 1;
328334
};
329335

0 commit comments

Comments
 (0)