@kayord/ui 1.0.11 → 1.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -200,3 +200,15 @@ pnpm i
200
200
  }
201
201
  }
202
202
  ```
203
+
204
+ ## Data Table Types
205
+
206
+ ```ts
207
+ // Add to app.d.ts
208
+ import { CustomOptions, CustomColumnMeta } from "@kayord/ui";
209
+
210
+ declare module "@tanstack/table-core" {
211
+ interface ColumnMeta<TData extends RowData, TValue> extends CustomColumnMeta {}
212
+ interface TableOptionsResolved<TData extends RowData> extends CustomOptions {}
213
+ }
214
+ ```
@@ -12,7 +12,7 @@
12
12
  import DataTableHeader from "./DataTableHeader.svelte";
13
13
  import VisibilitySelect from "./VisibilitySelect.svelte";
14
14
  import DataTableFooter from "./DataTableFooter.svelte";
15
- import { goto } from "$app/navigation";
15
+ import { beforeNavigate, goto } from "$app/navigation";
16
16
  import {
17
17
  decodeColumnFilters,
18
18
  decodeGlobalFilter,
@@ -57,6 +57,45 @@
57
57
 
58
58
  const tableStore = new TableStore();
59
59
  const isPaginationEnabled = table.options.getPaginationRowModel !== undefined;
60
+
61
+ // Load Default Values from Page Params
62
+ onMount(() => {
63
+ if (table.options.useURLSearchParams) {
64
+ table.setPageIndex(decodePageIndex());
65
+ table.setSorting(decodeSorting() ?? []);
66
+ table.setGlobalFilter(decodeGlobalFilter());
67
+ table.setColumnFilters(decodeColumnFilters() ?? []);
68
+ }
69
+ });
70
+
71
+ // Reset pageIndex
72
+ beforeNavigate((navigation) => {
73
+ if (table.options.useURLSearchParams) {
74
+ if (Number(navigation.to?.url.searchParams.get("page") ?? "0") > 0) {
75
+ if (
76
+ navigation.from?.url.searchParams.get("sort") != navigation.to?.url.searchParams.get("sort") ||
77
+ navigation.from?.url.searchParams.get("globalFilter") !=
78
+ navigation.to?.url.searchParams.get("globalFilter") ||
79
+ navigation.from?.url.searchParams.get("columnFilters") != navigation.to?.url.searchParams.get("columnFilters")
80
+ ) {
81
+ table.resetPageIndex();
82
+ }
83
+ }
84
+ }
85
+ });
86
+
87
+ // Set URL Page Params
88
+ $effect(() => {
89
+ if (table.options.useURLSearchParams) {
90
+ const params = encodeTableState(table.getState());
91
+ goto(params, {
92
+ replaceState: true,
93
+ keepFocus: true,
94
+ noScroll: true,
95
+ });
96
+ }
97
+ });
98
+
60
99
  let end: HTMLElement | undefined = $state();
61
100
  </script>
62
101
 
@@ -2,3 +2,4 @@ export { default as DataTable } from "./DataTable.svelte";
2
2
  export { createShadTable } from "./shad-table.svelte";
3
3
  export { max, min, sum, uniqueCount } from "./data-table-utils";
4
4
  export { decodeColumnFilters, decodeGlobalFilter, decodePageIndex, decodeSorting, decodeTableState, encodeColumnFilters, encodeGlobalFilter, encodePageIndex, encodeSorting, encodeTableState, } from "./table-search-params";
5
+ export type { CustomColumnMeta, CustomOptions } from "./types";
@@ -9,6 +9,9 @@ export function createShadTable(shadOptions) {
9
9
  if ((shadOptions.enablePaging ?? true) == false) {
10
10
  shadOptions.manualPagination = true;
11
11
  }
12
+ if (shadOptions.useURLSearchParams) {
13
+ shadOptions.autoResetPageIndex = false;
14
+ }
12
15
  const options = shadOptions;
13
16
  // Features
14
17
  // Sorting
@@ -3,16 +3,16 @@ export declare const encodeSorting: (state: Partial<TableState>) => string;
3
3
  export declare const decodeSorting: () => {
4
4
  id: string;
5
5
  desc: boolean;
6
- }[];
6
+ }[] | undefined;
7
7
  export declare const encodeGlobalFilter: (state: Partial<TableState>) => any;
8
- export declare const decodeGlobalFilter: () => string;
8
+ export declare const decodeGlobalFilter: () => string | undefined;
9
9
  export declare const encodePageIndex: (state: Partial<TableState>) => string;
10
10
  export declare const decodePageIndex: () => number;
11
11
  export declare const encodeColumnFilters: (state: Partial<TableState>) => string;
12
12
  export declare const decodeColumnFilters: () => {
13
13
  id: string;
14
14
  value: any;
15
- }[];
15
+ }[] | undefined;
16
16
  interface Options {
17
17
  sorting?: boolean;
18
18
  globalFilter?: boolean;
@@ -9,16 +9,17 @@ export const encodeSorting = (state) => {
9
9
  return state.sorting?.map((s) => `${s.desc ? "-" : ""}${s.id}`).join(",") ?? "";
10
10
  };
11
11
  export const decodeSorting = () => {
12
- return (page.url.searchParams
12
+ return page.url.searchParams
13
13
  .get("sort")
14
14
  ?.split(",")
15
- .map((s) => ({ id: s[0] === "-" ? s.slice(1) : s.slice(0), desc: s[0] === "-" })) ?? []);
15
+ .map((s) => ({ id: s[0] === "-" ? s.slice(1) : s.slice(0), desc: s[0] === "-" }));
16
16
  };
17
17
  export const encodeGlobalFilter = (state) => {
18
18
  return state.globalFilter;
19
19
  };
20
20
  export const decodeGlobalFilter = () => {
21
- return page.url.searchParams.get("globalFilter") ?? "";
21
+ const globalFilter = page.url.searchParams.get("globalFilter");
22
+ return globalFilter != null ? globalFilter : undefined;
22
23
  };
23
24
  export const encodePageIndex = (state) => {
24
25
  return state.pagination?.pageIndex?.toString() ?? "";
@@ -32,7 +33,7 @@ export const encodeColumnFilters = (state) => {
32
33
  .join(",") ?? "");
33
34
  };
34
35
  export const decodeColumnFilters = () => {
35
- return (page.url.searchParams
36
+ return page.url.searchParams
36
37
  .get("columnFilters")
37
38
  ?.split(",")
38
39
  .map((v) => {
@@ -46,7 +47,7 @@ export const decodeColumnFilters = () => {
46
47
  value: stringValue === "undefined" ? undefined : JSON.parse(decodeURIComponent(stringValue)),
47
48
  };
48
49
  })
49
- .filter((x) => x !== null) ?? []);
50
+ .filter((x) => x !== null);
50
51
  };
51
52
  export const decodeTableState = () => {
52
53
  return {
@@ -0,0 +1,6 @@
1
+ export interface CustomOptions {
2
+ useURLSearchParams?: boolean;
3
+ }
4
+ export interface CustomColumnMeta {
5
+ className?: string;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kayord/ui",
3
3
  "private": false,
4
- "version": "1.0.11",
4
+ "version": "1.0.13",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -49,7 +49,7 @@
49
49
  "devDependencies": {
50
50
  "@lucide/svelte": "^0.523.0",
51
51
  "@sveltejs/adapter-auto": "^6.0.1",
52
- "@sveltejs/kit": "^2.22.1",
52
+ "@sveltejs/kit": "^2.22.2",
53
53
  "@sveltejs/package": "^2.3.12",
54
54
  "@sveltejs/vite-plugin-svelte": "^5.1.0",
55
55
  "@tailwindcss/vite": "^4.1.10",