Skip to content

Commit

Permalink
filterState, sortState, and object assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
pheuter committed Jul 15, 2024
1 parent 1cbc624 commit 26bea0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-donkeys-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@careswitch/svelte-data-table': minor
---

Rename `filters` to `filterState`; introduce `sortState`, refactor filter mutators to reassign object
28 changes: 17 additions & 11 deletions src/lib/DataTable.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,18 @@ export class DataTable<T> {
* The current filter state for all columns.
* @returns {{ [K in keyof T]: Set<any> }} An object representing the filter state that maps column keys to filter values.
*/
get filters() {
get filterState() {
return this.#filterState;
}

/**
* The current sort state for the table.
* @returns {{ column: keyof T | null; direction: SortDirection }} An object representing the sort state with a column key and direction.
*/
get sortState() {
return this.#sortState;
}

/**
* The total number of pages based on the current filters and page size.
* @returns {number} The total number of pages.
Expand Down Expand Up @@ -293,7 +301,7 @@ export class DataTable<T> {
*/
setFilter = <K extends keyof T>(column: K, values: any[]) => {
this.#isFilterDirty = true;
this.#filterState[column] = new Set(values);
this.#filterState = { ...this.#filterState, [column]: new Set(values) };
this.#currentPage = 1;
};

Expand All @@ -303,7 +311,7 @@ export class DataTable<T> {
*/
clearFilter = (column: keyof T) => {
this.#isFilterDirty = true;
this.#filterState[column] = new Set();
this.#filterState = { ...this.#filterState, [column]: new Set() };
this.#currentPage = 1;
};

Expand All @@ -315,15 +323,13 @@ export class DataTable<T> {
*/
toggleFilter = <K extends keyof T>(column: K, value: any) => {
this.#isFilterDirty = true;
const currentFilter = this.#filterState[column];

if (currentFilter.has(value)) {
currentFilter.delete(value);
} else {
currentFilter.add(value);
}
this.#filterState = {
...this.#filterState,
[column]: this.isFilterActive(column, value)
? new Set([...this.#filterState[column]].filter((v) => v !== value))
: new Set([...this.#filterState[column], value])
};

this.#filterState[column] = new Set(currentFilter);
this.#currentPage = 1;
};

Expand Down

0 comments on commit 26bea0d

Please sign in to comment.