sveltacular 1.0.27 → 1.0.28
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/dist/tables/data-grid-actions-cell.svelte +4 -20
- package/dist/tables/data-grid-actions-cell.svelte.d.ts +28 -22
- package/dist/tables/data-grid-cell.svelte +7 -7
- package/dist/tables/data-grid-cell.svelte.d.ts +33 -14
- package/dist/tables/data-grid-row.svelte +9 -25
- package/dist/tables/data-grid-row.svelte.d.ts +38 -33
- package/dist/tables/data-grid-type-example.svelte +152 -0
- package/dist/tables/data-grid-type-example.svelte.d.ts +18 -0
- package/dist/tables/data-grid.svelte +16 -27
- package/dist/tables/data-grid.svelte.d.ts +47 -44
- package/dist/tables/example-status-cell.svelte +6 -11
- package/dist/tables/example-status-cell.svelte.d.ts +3 -1
- package/dist/tables/index.d.ts +1 -0
- package/dist/types/data.d.ts +14 -0
- package/package.json +1 -1
|
@@ -1,27 +1,11 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import type { JsonObject } from '../types/data.js';
|
|
1
|
+
<script lang="ts" generics="T extends JsonObject">
|
|
2
|
+
import type { JsonObject, DataGridActions } from '../types/data.js';
|
|
3
3
|
import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
|
|
4
4
|
import TableCell from './table-cell.svelte';
|
|
5
5
|
import Button from '../forms/button/button.svelte';
|
|
6
6
|
import DropdownButton from '../navigation/dropdown-button/dropdown-button.svelte';
|
|
7
7
|
import DropdownItem from '../generic/dropdown-item/dropdown-item.svelte';
|
|
8
8
|
|
|
9
|
-
interface Action {
|
|
10
|
-
text: string;
|
|
11
|
-
variant?: ButtonVariant;
|
|
12
|
-
href?: (row: JsonObject) => string;
|
|
13
|
-
onClick?: (row: JsonObject) => unknown;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface Actions {
|
|
17
|
-
text?: string;
|
|
18
|
-
type?: 'buttons' | 'dropdown';
|
|
19
|
-
variant?: ButtonVariant | 'default';
|
|
20
|
-
size?: FormFieldSizeOptions;
|
|
21
|
-
align?: 'left' | 'center' | 'right';
|
|
22
|
-
items: Action[];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
9
|
let {
|
|
26
10
|
actions,
|
|
27
11
|
row,
|
|
@@ -29,8 +13,8 @@
|
|
|
29
13
|
actionButtonSize = 'sm',
|
|
30
14
|
actionAlign = 'center'
|
|
31
15
|
}: {
|
|
32
|
-
actions:
|
|
33
|
-
row:
|
|
16
|
+
actions: DataGridActions<T>;
|
|
17
|
+
row: T;
|
|
34
18
|
actionButtonVariant?: ButtonVariant;
|
|
35
19
|
actionButtonSize?: FormFieldSizeOptions;
|
|
36
20
|
actionAlign?: 'left' | 'center' | 'right';
|
|
@@ -1,26 +1,32 @@
|
|
|
1
|
-
import type { JsonObject } from '../types/data.js';
|
|
1
|
+
import type { JsonObject, DataGridActions } from '../types/data.js';
|
|
2
2
|
import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
declare function $$render<T extends JsonObject>(): {
|
|
4
|
+
props: {
|
|
5
|
+
actions: DataGridActions<T>;
|
|
6
|
+
row: T;
|
|
7
|
+
actionButtonVariant?: ButtonVariant;
|
|
8
|
+
actionButtonSize?: FormFieldSizeOptions;
|
|
9
|
+
actionAlign?: "left" | "center" | "right";
|
|
10
|
+
};
|
|
11
|
+
exports: {};
|
|
12
|
+
bindings: "";
|
|
13
|
+
slots: {};
|
|
14
|
+
events: {};
|
|
15
|
+
};
|
|
16
|
+
declare class __sveltets_Render<T extends JsonObject> {
|
|
17
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
18
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
19
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
20
|
+
bindings(): "";
|
|
21
|
+
exports(): {};
|
|
8
22
|
}
|
|
9
|
-
interface
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
items: Action[];
|
|
23
|
+
interface $$IsomorphicComponent {
|
|
24
|
+
new <T extends JsonObject>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
25
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
26
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
27
|
+
<T extends JsonObject>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
28
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
16
29
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
row: JsonObject;
|
|
20
|
-
actionButtonVariant?: ButtonVariant;
|
|
21
|
-
actionButtonSize?: FormFieldSizeOptions;
|
|
22
|
-
actionAlign?: 'left' | 'center' | 'right';
|
|
23
|
-
};
|
|
24
|
-
declare const DataGridActionsCell: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
25
|
-
type DataGridActionsCell = ReturnType<typeof DataGridActionsCell>;
|
|
30
|
+
declare const DataGridActionsCell: $$IsomorphicComponent;
|
|
31
|
+
type DataGridActionsCell<T extends JsonObject> = InstanceType<typeof DataGridActionsCell<T>>;
|
|
26
32
|
export default DataGridActionsCell;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<script lang="ts">
|
|
1
|
+
<script lang="ts" generics="T extends JsonObject">
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
import type { ColumnDef, JsonObject } from '../types/data.js';
|
|
4
4
|
import TableCell from './table-cell.svelte';
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
formatArrayCell
|
|
13
13
|
} from './cell-renderers.js';
|
|
14
14
|
|
|
15
|
-
interface CellContext<
|
|
16
|
-
row:
|
|
15
|
+
interface CellContext<TRow extends JsonObject = JsonObject> {
|
|
16
|
+
row: TRow;
|
|
17
17
|
value: unknown;
|
|
18
|
-
column: ColumnDef<
|
|
18
|
+
column: ColumnDef<TRow>;
|
|
19
19
|
rowIndex: number;
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
cellSnippet = undefined,
|
|
27
27
|
width = undefined
|
|
28
28
|
}: {
|
|
29
|
-
row:
|
|
30
|
-
column: ColumnDef
|
|
29
|
+
row: T;
|
|
30
|
+
column: ColumnDef<T>;
|
|
31
31
|
rowIndex: number;
|
|
32
|
-
cellSnippet?: Snippet<[CellContext]>;
|
|
32
|
+
cellSnippet?: Snippet<[CellContext<T>]>;
|
|
33
33
|
width?: number | string;
|
|
34
34
|
} = $props();
|
|
35
35
|
|
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import type { ColumnDef, JsonObject } from '../types/data.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
declare function $$render<T extends JsonObject>(): {
|
|
4
|
+
props: {
|
|
5
|
+
row: T;
|
|
6
|
+
column: ColumnDef<T>;
|
|
7
|
+
rowIndex: number;
|
|
8
|
+
cellSnippet?: Snippet<[{
|
|
9
|
+
row: T;
|
|
10
|
+
value: unknown;
|
|
11
|
+
column: ColumnDef<T>;
|
|
12
|
+
rowIndex: number;
|
|
13
|
+
}]>;
|
|
14
|
+
width?: number | string;
|
|
15
|
+
};
|
|
16
|
+
exports: {};
|
|
17
|
+
bindings: "";
|
|
18
|
+
slots: {};
|
|
19
|
+
events: {};
|
|
15
20
|
};
|
|
16
|
-
declare
|
|
17
|
-
|
|
21
|
+
declare class __sveltets_Render<T extends JsonObject> {
|
|
22
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
23
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
24
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
25
|
+
bindings(): "";
|
|
26
|
+
exports(): {};
|
|
27
|
+
}
|
|
28
|
+
interface $$IsomorphicComponent {
|
|
29
|
+
new <T extends JsonObject>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
30
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
31
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
32
|
+
<T extends JsonObject>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
33
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
34
|
+
}
|
|
35
|
+
declare const DataGridCell: $$IsomorphicComponent;
|
|
36
|
+
type DataGridCell<T extends JsonObject> = InstanceType<typeof DataGridCell<T>>;
|
|
18
37
|
export default DataGridCell;
|
|
@@ -1,35 +1,19 @@
|
|
|
1
|
-
<script lang="ts">
|
|
1
|
+
<script lang="ts" generics="T extends JsonObject">
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
|
-
import type { ColumnDef, JsonObject } from '../types/data.js';
|
|
3
|
+
import type { ColumnDef, JsonObject, DataGridActions } from '../types/data.js';
|
|
4
4
|
import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
|
|
5
5
|
import TableRow from './table-row.svelte';
|
|
6
6
|
import TableSelectionCell from './table-selection-cell.svelte';
|
|
7
7
|
import DataGridCell from './data-grid-cell.svelte';
|
|
8
8
|
import DataGridActionsCell from './data-grid-actions-cell.svelte';
|
|
9
9
|
|
|
10
|
-
interface CellContext<
|
|
11
|
-
row:
|
|
10
|
+
interface CellContext<TRow extends JsonObject = JsonObject> {
|
|
11
|
+
row: TRow;
|
|
12
12
|
value: unknown;
|
|
13
|
-
column: ColumnDef<
|
|
13
|
+
column: ColumnDef<TRow>;
|
|
14
14
|
rowIndex: number;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
interface Action {
|
|
18
|
-
text: string;
|
|
19
|
-
variant?: ButtonVariant;
|
|
20
|
-
href?: (row: JsonObject) => string;
|
|
21
|
-
onClick?: (row: JsonObject) => unknown;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface Actions {
|
|
25
|
-
text?: string;
|
|
26
|
-
type?: 'buttons' | 'dropdown';
|
|
27
|
-
variant?: ButtonVariant | 'default';
|
|
28
|
-
size?: FormFieldSizeOptions;
|
|
29
|
-
align?: 'left' | 'center' | 'right';
|
|
30
|
-
items: Action[];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
17
|
let {
|
|
34
18
|
row,
|
|
35
19
|
rowIndex,
|
|
@@ -42,13 +26,13 @@
|
|
|
42
26
|
actionButtonSize = 'sm',
|
|
43
27
|
actionAlign = 'center'
|
|
44
28
|
}: {
|
|
45
|
-
row:
|
|
29
|
+
row: T;
|
|
46
30
|
rowIndex: number;
|
|
47
|
-
visibleCols: ColumnDef[];
|
|
31
|
+
visibleCols: ColumnDef<T>[];
|
|
48
32
|
hasSelectionCol?: boolean;
|
|
49
33
|
hasActionCol?: boolean;
|
|
50
|
-
actions?:
|
|
51
|
-
cells?: Record<string, Snippet<[CellContext]>>;
|
|
34
|
+
actions?: DataGridActions<T>;
|
|
35
|
+
cells?: Record<string, Snippet<[CellContext<T>]>>;
|
|
52
36
|
actionButtonVariant?: ButtonVariant;
|
|
53
37
|
actionButtonSize?: FormFieldSizeOptions;
|
|
54
38
|
actionAlign?: 'left' | 'center' | 'right';
|
|
@@ -1,38 +1,43 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
-
import type { ColumnDef, JsonObject } from '../types/data.js';
|
|
2
|
+
import type { ColumnDef, JsonObject, DataGridActions } from '../types/data.js';
|
|
3
3
|
import type { ButtonVariant, FormFieldSizeOptions } from '../types/form.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
declare function $$render<T extends JsonObject>(): {
|
|
5
|
+
props: {
|
|
6
|
+
row: T;
|
|
7
|
+
rowIndex: number;
|
|
8
|
+
visibleCols: ColumnDef<T>[];
|
|
9
|
+
hasSelectionCol?: boolean;
|
|
10
|
+
hasActionCol?: boolean;
|
|
11
|
+
actions?: DataGridActions<T>;
|
|
12
|
+
cells?: Record<string, Snippet<[{
|
|
13
|
+
row: T;
|
|
14
|
+
value: unknown;
|
|
15
|
+
column: ColumnDef<T>;
|
|
16
|
+
rowIndex: number;
|
|
17
|
+
}]>>;
|
|
18
|
+
actionButtonVariant?: ButtonVariant;
|
|
19
|
+
actionButtonSize?: FormFieldSizeOptions;
|
|
20
|
+
actionAlign?: "left" | "center" | "right";
|
|
21
|
+
};
|
|
22
|
+
exports: {};
|
|
23
|
+
bindings: "";
|
|
24
|
+
slots: {};
|
|
25
|
+
events: {};
|
|
26
|
+
};
|
|
27
|
+
declare class __sveltets_Render<T extends JsonObject> {
|
|
28
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
29
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
30
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
31
|
+
bindings(): "";
|
|
32
|
+
exports(): {};
|
|
15
33
|
}
|
|
16
|
-
interface
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
items: Action[];
|
|
34
|
+
interface $$IsomorphicComponent {
|
|
35
|
+
new <T extends JsonObject>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
36
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
37
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
38
|
+
<T extends JsonObject>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
39
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
23
40
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
rowIndex: number;
|
|
27
|
-
visibleCols: ColumnDef[];
|
|
28
|
-
hasSelectionCol?: boolean;
|
|
29
|
-
hasActionCol?: boolean;
|
|
30
|
-
actions?: Actions;
|
|
31
|
-
cells?: Record<string, Snippet<[CellContext]>>;
|
|
32
|
-
actionButtonVariant?: ButtonVariant;
|
|
33
|
-
actionButtonSize?: FormFieldSizeOptions;
|
|
34
|
-
actionAlign?: 'left' | 'center' | 'right';
|
|
35
|
-
};
|
|
36
|
-
declare const DataGridRow: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
37
|
-
type DataGridRow = ReturnType<typeof DataGridRow>;
|
|
41
|
+
declare const DataGridRow: $$IsomorphicComponent;
|
|
42
|
+
type DataGridRow<T extends JsonObject> = InstanceType<typeof DataGridRow<T>>;
|
|
38
43
|
export default DataGridRow;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* This file demonstrates the type-safe usage of DataGrid with generics.
|
|
4
|
+
* It shows how TypeScript will catch errors at compile time.
|
|
5
|
+
*/
|
|
6
|
+
import DataGrid from './data-grid.svelte';
|
|
7
|
+
import type { ColumnDef, JsonObject, DataGridActions } from '../types/data.js';
|
|
8
|
+
|
|
9
|
+
// Define a strongly-typed interface for our data
|
|
10
|
+
interface Product extends JsonObject {
|
|
11
|
+
id: number;
|
|
12
|
+
name: string;
|
|
13
|
+
price: number;
|
|
14
|
+
category: string;
|
|
15
|
+
inStock: boolean;
|
|
16
|
+
rating: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Sample data with the Product type
|
|
20
|
+
const products: Product[] = [
|
|
21
|
+
{
|
|
22
|
+
id: 1,
|
|
23
|
+
name: 'Laptop',
|
|
24
|
+
price: 1299.99,
|
|
25
|
+
category: 'Electronics',
|
|
26
|
+
inStock: true,
|
|
27
|
+
rating: 4.5
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
id: 2,
|
|
31
|
+
name: 'Coffee Mug',
|
|
32
|
+
price: 12.99,
|
|
33
|
+
category: 'Kitchen',
|
|
34
|
+
inStock: true,
|
|
35
|
+
rating: 4.8
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: 3,
|
|
39
|
+
name: 'Desk Chair',
|
|
40
|
+
price: 299.99,
|
|
41
|
+
category: 'Furniture',
|
|
42
|
+
inStock: false,
|
|
43
|
+
rating: 4.2
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
// Type-safe column definitions
|
|
48
|
+
// The 'key' property must match a property in the Product interface
|
|
49
|
+
const columns: ColumnDef<Product>[] = [
|
|
50
|
+
{ key: 'name', label: 'Product Name', type: 'text', sortable: true },
|
|
51
|
+
{ key: 'category', label: 'Category', type: 'text', sortable: true },
|
|
52
|
+
{ key: 'price', label: 'Price', type: 'currency', sortable: true },
|
|
53
|
+
{ key: 'inStock', label: 'In Stock', type: 'check', align: 'center' },
|
|
54
|
+
{ key: 'rating', label: 'Rating', type: 'number', sortable: true }
|
|
55
|
+
// TypeScript error if you try to use a key that doesn't exist:
|
|
56
|
+
// { key: 'invalidKey', label: 'Invalid', type: 'text' } // ❌ Error!
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
// Type-safe actions with typed row parameter
|
|
60
|
+
const actions: DataGridActions<Product> = {
|
|
61
|
+
type: 'dropdown',
|
|
62
|
+
items: [
|
|
63
|
+
{
|
|
64
|
+
text: 'View Details',
|
|
65
|
+
href: (row) => {
|
|
66
|
+
// row is typed as Product, so we get autocomplete and type checking
|
|
67
|
+
return `/products/${row.id}`;
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
text: 'Edit',
|
|
72
|
+
onClick: (row) => {
|
|
73
|
+
// row.name, row.price, etc. are all typed correctly
|
|
74
|
+
console.log(`Editing ${row.name} (${row.category})`);
|
|
75
|
+
// row.invalidProperty would be a TypeScript error ❌
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
text: 'Toggle Stock',
|
|
80
|
+
onClick: (row) => {
|
|
81
|
+
// Type-safe property access
|
|
82
|
+
console.log(`Toggling stock for ${row.name}: ${!row.inStock}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Type-safe selection handler
|
|
89
|
+
function handleSelectionChange(selectedProducts: Product[]) {
|
|
90
|
+
// selectedProducts is typed as Product[], not JsonObject[]
|
|
91
|
+
selectedProducts.forEach((product) => {
|
|
92
|
+
console.log(`Selected: ${product.name} - $${product.price}`);
|
|
93
|
+
// Full type safety and autocomplete here
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Type-safe format function
|
|
98
|
+
const columnsWithFormat: ColumnDef<Product>[] = [
|
|
99
|
+
{
|
|
100
|
+
key: 'name',
|
|
101
|
+
label: 'Product',
|
|
102
|
+
type: 'text',
|
|
103
|
+
format: (value: string, row: Product) => {
|
|
104
|
+
// row is typed as Product
|
|
105
|
+
return `${value} (${row.category})`;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
key: 'price',
|
|
110
|
+
label: 'Price',
|
|
111
|
+
type: 'currency',
|
|
112
|
+
format: (value: number, row: Product) => {
|
|
113
|
+
// value is typed as number, row is typed as Product
|
|
114
|
+
const discount = row.inStock ? 0 : 0.1;
|
|
115
|
+
return `$${(value * (1 - discount)).toFixed(2)}`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
];
|
|
119
|
+
</script>
|
|
120
|
+
|
|
121
|
+
<!-- Example 1: Basic type-safe DataGrid -->
|
|
122
|
+
<DataGrid rows={products} cols={columns} />
|
|
123
|
+
|
|
124
|
+
<!-- Example 2: With actions (type-safe) -->
|
|
125
|
+
<DataGrid rows={products} cols={columns} {actions} />
|
|
126
|
+
|
|
127
|
+
<!-- Example 3: With selection (type-safe callback) -->
|
|
128
|
+
<DataGrid
|
|
129
|
+
rows={products}
|
|
130
|
+
cols={columns}
|
|
131
|
+
selectionMode="multi"
|
|
132
|
+
onSelectionChange={handleSelectionChange}
|
|
133
|
+
/>
|
|
134
|
+
|
|
135
|
+
<!-- Example 4: With custom formatting (type-safe) -->
|
|
136
|
+
<DataGrid rows={products} cols={columnsWithFormat} />
|
|
137
|
+
|
|
138
|
+
<!--
|
|
139
|
+
Type Safety Benefits Demonstrated:
|
|
140
|
+
|
|
141
|
+
1. Column keys are validated against the Product interface
|
|
142
|
+
2. Action callbacks receive typed Product objects
|
|
143
|
+
3. Selection callbacks receive Product[] arrays
|
|
144
|
+
4. Format functions have typed parameters
|
|
145
|
+
5. Link generators have typed row parameters
|
|
146
|
+
6. Custom components receive typed CellRendererProps<Product>
|
|
147
|
+
|
|
148
|
+
Try these to see TypeScript errors:
|
|
149
|
+
- Add a column with key: 'nonExistentField'
|
|
150
|
+
- Access row.invalidProperty in an action callback
|
|
151
|
+
- Try to assign wrong type to rows prop
|
|
152
|
+
-->
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
2
|
+
new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
|
|
3
|
+
$$bindings?: Bindings;
|
|
4
|
+
} & Exports;
|
|
5
|
+
(internal: unknown, props: {
|
|
6
|
+
$$events?: Events;
|
|
7
|
+
$$slots?: Slots;
|
|
8
|
+
}): Exports & {
|
|
9
|
+
$set?: any;
|
|
10
|
+
$on?: any;
|
|
11
|
+
};
|
|
12
|
+
z_$$bindings?: Bindings;
|
|
13
|
+
}
|
|
14
|
+
declare const DataGridTypeExample: $$__sveltets_2_IsomorphicComponent<Record<string, never>, {
|
|
15
|
+
[evt: string]: CustomEvent<any>;
|
|
16
|
+
}, {}, {}, string>;
|
|
17
|
+
type DataGridTypeExample = InstanceType<typeof DataGridTypeExample>;
|
|
18
|
+
export default DataGridTypeExample;
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
<script lang="ts">
|
|
1
|
+
<script lang="ts" generics="T extends JsonObject">
|
|
2
2
|
import TableCell from './table-cell.svelte';
|
|
3
3
|
import TableHeaderCell from './table-header-cell.svelte';
|
|
4
4
|
import TableHeader from './table-header.svelte';
|
|
5
5
|
import Table from './table.svelte';
|
|
6
6
|
import TableSelectionHeaderCell from './table-selection-header-cell.svelte';
|
|
7
7
|
import DataGridRow from './data-grid-row.svelte';
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
ColumnDef,
|
|
10
|
+
JsonObject,
|
|
11
|
+
PaginationProperties,
|
|
12
|
+
DataGridActions
|
|
13
|
+
} from '../types/data.js';
|
|
9
14
|
import Empty from '../generic/empty/empty.svelte';
|
|
10
15
|
import Icon from '../icons/icon.svelte';
|
|
11
16
|
import Pagination from '../navigation/pagination/pagination.svelte';
|
|
@@ -18,29 +23,13 @@
|
|
|
18
23
|
|
|
19
24
|
type PaginationEvent = (pagination: PaginationProperties) => void;
|
|
20
25
|
|
|
21
|
-
interface CellContext<
|
|
22
|
-
row:
|
|
26
|
+
interface CellContext<TRow extends JsonObject = JsonObject> {
|
|
27
|
+
row: TRow;
|
|
23
28
|
value: unknown;
|
|
24
|
-
column: ColumnDef<
|
|
29
|
+
column: ColumnDef<TRow>;
|
|
25
30
|
rowIndex: number;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
|
-
interface Action {
|
|
29
|
-
text: string;
|
|
30
|
-
variant?: ButtonVariant;
|
|
31
|
-
href?: (row: JsonObject) => string;
|
|
32
|
-
onClick?: (row: JsonObject) => unknown;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface Actions {
|
|
36
|
-
text?: string;
|
|
37
|
-
type?: 'buttons' | 'dropdown';
|
|
38
|
-
variant?: ButtonVariant | 'default';
|
|
39
|
-
size?: FormFieldSizeOptions;
|
|
40
|
-
align?: 'left' | 'center' | 'right';
|
|
41
|
-
items: Action[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
33
|
let {
|
|
45
34
|
captionSide = 'top' as 'top' | 'bottom',
|
|
46
35
|
captionAlign = 'center' as 'left' | 'center' | 'right',
|
|
@@ -64,20 +53,20 @@
|
|
|
64
53
|
}: {
|
|
65
54
|
captionSide?: 'top' | 'bottom';
|
|
66
55
|
captionAlign?: 'left' | 'center' | 'right';
|
|
67
|
-
rows?:
|
|
68
|
-
cols: ColumnDef[];
|
|
56
|
+
rows?: T[];
|
|
57
|
+
cols: ColumnDef<T>[];
|
|
69
58
|
pagination?: PaginationProperties;
|
|
70
|
-
actions?:
|
|
59
|
+
actions?: DataGridActions<T>;
|
|
71
60
|
stickyHeader?: boolean;
|
|
72
61
|
enableSorting?: boolean;
|
|
73
62
|
selectionMode?: 'none' | 'single' | 'multi';
|
|
74
63
|
rowIdKey?: string;
|
|
75
64
|
onPageChange?: PaginationEvent | null;
|
|
76
65
|
onSort?: (column: string, direction: 'asc' | 'desc') => void;
|
|
77
|
-
onSelectionChange?: (selectedRows:
|
|
66
|
+
onSelectionChange?: (selectedRows: T[]) => void;
|
|
78
67
|
selectedCount?: number;
|
|
79
68
|
children?: Snippet;
|
|
80
|
-
cells?: Record<string, Snippet<[CellContext]>>;
|
|
69
|
+
cells?: Record<string, Snippet<[CellContext<T>]>>;
|
|
81
70
|
virtualScroll?: boolean;
|
|
82
71
|
rowHeight?: number;
|
|
83
72
|
maxHeight?: string;
|
|
@@ -173,7 +162,7 @@
|
|
|
173
162
|
|
|
174
163
|
// Virtual scrolling setup (only when pagination is disabled)
|
|
175
164
|
let tbodyRef: HTMLElement | null = null;
|
|
176
|
-
let virtual = $state<ReturnType<typeof useVirtualList<
|
|
165
|
+
let virtual = $state<ReturnType<typeof useVirtualList<T>> | null>(null);
|
|
177
166
|
|
|
178
167
|
// Initialize virtual list
|
|
179
168
|
$effect(() => {
|
|
@@ -1,48 +1,51 @@
|
|
|
1
|
-
import type { ColumnDef, JsonObject, PaginationProperties } from '../types/data.js';
|
|
1
|
+
import type { ColumnDef, JsonObject, PaginationProperties, DataGridActions } from '../types/data.js';
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
declare function $$render<T extends JsonObject>(): {
|
|
4
|
+
props: {
|
|
5
|
+
captionSide?: "top" | "bottom";
|
|
6
|
+
captionAlign?: "left" | "center" | "right";
|
|
7
|
+
rows?: T[];
|
|
8
|
+
cols: ColumnDef<T>[];
|
|
9
|
+
pagination?: PaginationProperties;
|
|
10
|
+
actions?: DataGridActions<T>;
|
|
11
|
+
stickyHeader?: boolean;
|
|
12
|
+
enableSorting?: boolean;
|
|
13
|
+
selectionMode?: "none" | "single" | "multi";
|
|
14
|
+
rowIdKey?: string;
|
|
15
|
+
onPageChange?: ((pagination: PaginationProperties) => void) | null;
|
|
16
|
+
onSort?: (column: string, direction: "asc" | "desc") => void;
|
|
17
|
+
onSelectionChange?: (selectedRows: T[]) => void;
|
|
18
|
+
selectedCount?: number;
|
|
19
|
+
children?: Snippet;
|
|
20
|
+
cells?: Record<string, Snippet<[{
|
|
21
|
+
row: T;
|
|
22
|
+
value: unknown;
|
|
23
|
+
column: ColumnDef<T>;
|
|
24
|
+
rowIndex: number;
|
|
25
|
+
}]>>;
|
|
26
|
+
virtualScroll?: boolean;
|
|
27
|
+
rowHeight?: number;
|
|
28
|
+
maxHeight?: string;
|
|
29
|
+
};
|
|
30
|
+
exports: {};
|
|
31
|
+
bindings: "selectedCount";
|
|
32
|
+
slots: {};
|
|
33
|
+
events: {};
|
|
34
|
+
};
|
|
35
|
+
declare class __sveltets_Render<T extends JsonObject> {
|
|
36
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
37
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
38
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
39
|
+
bindings(): "selectedCount";
|
|
40
|
+
exports(): {};
|
|
16
41
|
}
|
|
17
|
-
interface
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
items: Action[];
|
|
42
|
+
interface $$IsomorphicComponent {
|
|
43
|
+
new <T extends JsonObject>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
44
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
45
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
46
|
+
<T extends JsonObject>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
47
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
24
48
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
captionAlign?: 'left' | 'center' | 'right';
|
|
28
|
-
rows?: JsonObject[];
|
|
29
|
-
cols: ColumnDef[];
|
|
30
|
-
pagination?: PaginationProperties;
|
|
31
|
-
actions?: Actions;
|
|
32
|
-
stickyHeader?: boolean;
|
|
33
|
-
enableSorting?: boolean;
|
|
34
|
-
selectionMode?: 'none' | 'single' | 'multi';
|
|
35
|
-
rowIdKey?: string;
|
|
36
|
-
onPageChange?: PaginationEvent | null;
|
|
37
|
-
onSort?: (column: string, direction: 'asc' | 'desc') => void;
|
|
38
|
-
onSelectionChange?: (selectedRows: JsonObject[]) => void;
|
|
39
|
-
selectedCount?: number;
|
|
40
|
-
children?: Snippet;
|
|
41
|
-
cells?: Record<string, Snippet<[CellContext]>>;
|
|
42
|
-
virtualScroll?: boolean;
|
|
43
|
-
rowHeight?: number;
|
|
44
|
-
maxHeight?: string;
|
|
45
|
-
};
|
|
46
|
-
declare const DataGrid: import("svelte").Component<$$ComponentProps, {}, "selectedCount">;
|
|
47
|
-
type DataGrid = ReturnType<typeof DataGrid>;
|
|
49
|
+
declare const DataGrid: $$IsomorphicComponent;
|
|
50
|
+
type DataGrid<T extends JsonObject> = InstanceType<typeof DataGrid<T>>;
|
|
48
51
|
export default DataGrid;
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { CellRendererProps } from '../types/data.js';
|
|
3
|
-
import Pill from '../generic/pill/pill.svelte';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
let {
|
|
5
|
+
row
|
|
6
|
+
}: CellRendererProps<{
|
|
7
|
+
name: string;
|
|
8
|
+
}> = $props();
|
|
8
9
|
</script>
|
|
9
10
|
|
|
10
|
-
{
|
|
11
|
-
<Pill variant="positive" compact label="✓ Active" />
|
|
12
|
-
{:else if value === 'completed'}
|
|
13
|
-
<Pill variant="standard" compact label="✓ Completed" />
|
|
14
|
-
{:else}
|
|
15
|
-
<Pill variant="negative" compact label="⚠ On Hold" />
|
|
16
|
-
{/if}
|
|
11
|
+
<div>foobar {row.name}</div>
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { CellRendererProps } from '../types/data.js';
|
|
2
|
-
declare const ExampleStatusCell: import("svelte").Component<CellRendererProps<
|
|
2
|
+
declare const ExampleStatusCell: import("svelte").Component<CellRendererProps<{
|
|
3
|
+
name: string;
|
|
4
|
+
}>, {}, "">;
|
|
3
5
|
type ExampleStatusCell = ReturnType<typeof ExampleStatusCell>;
|
|
4
6
|
export default ExampleStatusCell;
|
package/dist/tables/index.d.ts
CHANGED
|
@@ -9,3 +9,4 @@ export { createTableContext, getTableContext, TableContext } from './table-conte
|
|
|
9
9
|
export type { TableContextConfig } from './table-context.svelte.js';
|
|
10
10
|
export { formatCell, formatTextCell, formatNumberCell, formatCurrencyCell, formatDateCell, formatDateTimeCell, formatBooleanCell, formatEmailCell, formatArrayCell, formatCustomCell, getCellValue, getCellLink, getCellAlignment, getCellTypeClass, sortRows, compareValues } from './cell-renderers.js';
|
|
11
11
|
export type { ArrayCellResult, CellRenderContext } from './cell-renderers.js';
|
|
12
|
+
export type { CellRendererProps } from '../types/data.js';
|
package/dist/types/data.d.ts
CHANGED
|
@@ -95,4 +95,18 @@ export interface SelectionState<T = string | number> {
|
|
|
95
95
|
selectedIds: Set<T>;
|
|
96
96
|
lastSelectedIndex: number | null;
|
|
97
97
|
}
|
|
98
|
+
export interface DataGridAction<T extends JsonObject = JsonObject> {
|
|
99
|
+
text: string;
|
|
100
|
+
variant?: string;
|
|
101
|
+
href?: (row: T) => string;
|
|
102
|
+
onClick?: (row: T) => unknown;
|
|
103
|
+
}
|
|
104
|
+
export interface DataGridActions<T extends JsonObject = JsonObject> {
|
|
105
|
+
text?: string;
|
|
106
|
+
type?: 'buttons' | 'dropdown';
|
|
107
|
+
variant?: string;
|
|
108
|
+
size?: string;
|
|
109
|
+
align?: 'left' | 'center' | 'right';
|
|
110
|
+
items: DataGridAction<T>[];
|
|
111
|
+
}
|
|
98
112
|
export {};
|