From 28a0db5b20ec35d76514ea40be8ad3d022cd4ca4 Mon Sep 17 00:00:00 2001 From: Sandwich <299465+dskvr@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:26:29 +0100 Subject: [PATCH 1/3] add SortState type, getSortState and setSortState --- src/lib/DataTable.svelte.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/lib/DataTable.svelte.ts b/src/lib/DataTable.svelte.ts index b13172a..9d10416 100644 --- a/src/lib/DataTable.svelte.ts +++ b/src/lib/DataTable.svelte.ts @@ -23,6 +23,8 @@ type TableConfig = { initialFilters?: { [id: string]: any[] }; }; +type StoreState = { columnId: string | null; direction: SortDirection } + /** * Represents a data table with sorting, filtering, and pagination capabilities. * @template T The type of data items in the table. @@ -33,7 +35,7 @@ export class DataTable { #originalData = $state([]); #currentPage = $state(1); - #sortState = $state<{ columnId: string | null; direction: SortDirection }>({ + #sortState = $state({ columnId: null, direction: null }); @@ -158,6 +160,15 @@ export class DataTable { this.#isSortDirty = false; } + setSortState(value: StoreState) { + this.#sortState = value; + this.#applySort(); + } + + getSortState(): StoreState { + return this.#sortState + } + /** * Gets or sets the base data rows without any filtering or sorting applied. * @returns {T[]} An array of all rows. From 5471c467984b2e5138907985c9e81ebd1c26d0ac Mon Sep 17 00:00:00 2001 From: Sandwich <299465+dskvr@users.noreply.github.com> Date: Thu, 19 Dec 2024 00:18:27 +0100 Subject: [PATCH 2/3] change method names add docs --- src/lib/DataTable.svelte.ts | 12 +++++++++--- tsconfig.json | 3 ++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib/DataTable.svelte.ts b/src/lib/DataTable.svelte.ts index 9d10416..aac1229 100644 --- a/src/lib/DataTable.svelte.ts +++ b/src/lib/DataTable.svelte.ts @@ -159,13 +159,19 @@ export class DataTable { } this.#isSortDirty = false; } - - setSortState(value: StoreState) { + /** + * Sets the current sort state for the table. + */ + setCurrentSortState(value: StoreState) { this.#sortState = value; this.#applySort(); } - getSortState(): StoreState { + /** + * Gets the current sort state for the table. + * @returns {StoreState} The current sort state. + */ + getCurrentSortState(): StoreState { return this.#sortState } diff --git a/tsconfig.json b/tsconfig.json index 6f788f1..08734ff 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "sourceMap": true, "strict": true, "module": "NodeNext", - "moduleResolution": "NodeNext" + "moduleResolution": "NodeNext", + "useDefineForClassFields": true } } From b9e715c3588c7848fd6f6259cad0008c96941784 Mon Sep 17 00:00:00 2001 From: Sandwich <299465+dskvr@users.noreply.github.com> Date: Thu, 19 Dec 2024 00:20:34 +0100 Subject: [PATCH 3/3] format SortState type --- src/lib/DataTable.svelte.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/DataTable.svelte.ts b/src/lib/DataTable.svelte.ts index aac1229..34dd2cc 100644 --- a/src/lib/DataTable.svelte.ts +++ b/src/lib/DataTable.svelte.ts @@ -23,7 +23,10 @@ type TableConfig = { initialFilters?: { [id: string]: any[] }; }; -type StoreState = { columnId: string | null; direction: SortDirection } +type StoreState = { + columnId: string | null; + direction: SortDirection +} /** * Represents a data table with sorting, filtering, and pagination capabilities.