@emamid/svelte-data-table 0.0.8 → 0.0.11
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 +67 -0
- package/dist/DataTable.doc.d.ts +9 -0
- package/dist/DataTable.doc.js +9 -0
- package/dist/DataTable.svelte +17 -3
- package/dist/DataTable.svelte.d.ts +2 -0
- package/dist/DataTableHeaderCell.svelte +6 -3
- package/dist/DataTableHeaderCell.svelte.d.ts +2 -0
- package/dist/common.d.ts +4 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
## Svelte Data Table based on [Flowbite Svelte](https://flowbite-svelte.com/), [Flowbite Icons](https://flowbite.com/icons/), and [Tailwind](https://tailwindcss.com/)
|
|
2
|
+
|
|
3
|
+
This data table is designed for viewing and editing data in an array of objects. It's built using the Flowbite Svelte library's Table components and works in conjunction with it, with support for custom cell editors, moving focus between cells with tab and enter, column sorting (simple and custom), conditional CSS, and more.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
To add to an existing Svelte app, first install Tailwind if you haven't already:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
> npx svelte-add@latest tailwindcss
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Then install the Flowbite components and icons along with the data table component.
|
|
14
|
+
```bash
|
|
15
|
+
> npm i flowbite-svelte flowbite flowbite-svelte-icons @emamid/svelte-data-table
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Basic example
|
|
19
|
+
|
|
20
|
+
```html
|
|
21
|
+
<script lang="ts">
|
|
22
|
+
import DataTable from '@emamid/svelte-data-table';
|
|
23
|
+
import type { ColumnConfig } from '@emamid/svelte-data-table';
|
|
24
|
+
|
|
25
|
+
const columns: ColumnConfig[] = [
|
|
26
|
+
{ canSort: true, key: 'firstName', title: 'First Name' },
|
|
27
|
+
{ canSort: true, key: 'lastName', title: 'Last Name' },
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const items: any[] = [
|
|
31
|
+
{ firstName: 'Bilbo', lastName: 'Baggins'},
|
|
32
|
+
{ firstName: 'Frodo', lastName: 'Baggins'},
|
|
33
|
+
{ firstName: 'Samwise', lastName: 'Gamgee'},
|
|
34
|
+
{ firstName: 'Meriadoc', lastName: 'Brandybuck'},
|
|
35
|
+
{ firstName: 'Peregrin', lastName: 'Took'},
|
|
36
|
+
];
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<main>
|
|
40
|
+
<DataTable
|
|
41
|
+
{columns}
|
|
42
|
+
{items}
|
|
43
|
+
/>
|
|
44
|
+
</main>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Examples for most of the component's features are under /examples . You can find the documentation [here](https://emamid.github.io/svelte-data-table/).
|
|
48
|
+
|
|
49
|
+
#### Example 1 - Basic data table
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
#### Example 2 - Table with cell focus components
|
|
53
|
+

|
|
54
|
+
|
|
55
|
+
#### Example 3 - Table with cell focus Select components, cell renderers, and custom sort
|
|
56
|
+

|
|
57
|
+
|
|
58
|
+
#### Example 4 - Table with actions and cell view components
|
|
59
|
+

|
|
60
|
+
|
|
61
|
+
#### Example 5 - Table with custom cell component
|
|
62
|
+

|
|
63
|
+
|
|
64
|
+
#### Example 6 - Table with cell and row class getter functions
|
|
65
|
+

|
|
66
|
+
|
|
67
|
+
#### Kitchen Sink example - All of the above
|
package/dist/DataTable.doc.d.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* @property {SortFunction} [sortFunction] - Function for more complex-sorting.
|
|
8
8
|
* @property {boolean} [reverseSort] - True when sorting is reversed.
|
|
9
9
|
* @property {string} [sortColumnID] - The id (or key) property of the column being sorted by.
|
|
10
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortAscendingIcon] - Svelte component class to be displayed in columns that are sorting ascended. Defaults to AngleDownSolid from {@link https://flowbite.com/icons/}
|
|
11
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortDescendingIcon] - Svelte component class to be displayed in columns that are sorting descended. Defaults to AngleUpSolid from {@link https://flowbite.com/icons/}
|
|
10
12
|
* @property {string} [itemKey] - Property to use when distinguish between rows. If not populated, the array index is used.
|
|
11
13
|
* @property {EnterAction} [enterAction] - Determines what pressing the enter key in a focused cell does. Can be 'next', 'down', or 'stay'. Default is 'next'. If 'next', enter will act the same as tab. If 'down', focus will move down to the row below the current one. If 'stay', the enter key will not cause movement.
|
|
12
14
|
* @property {string} [divClassAppend] - Classes to be appended to Table.divClass .
|
|
@@ -26,6 +28,13 @@
|
|
|
26
28
|
* @property {string} [trClassAppend] - Classes to be appended to TableBodyRow.class . trClassGetter can further modify this.
|
|
27
29
|
* @property {string} [trClassOverride] - Classes to replace TableBodyRow.class with. trClassGetter can further modify this.
|
|
28
30
|
* @property {RowClassFunction} [trClassGetter] - Calculates the CSS classes for a TableBodyRow .
|
|
31
|
+
* @property {boolean} [striped] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
32
|
+
* @property {boolean} [hoverable] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
33
|
+
* @property {boolean} [noborder] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
34
|
+
* @property {boolean} [shadow] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
35
|
+
* @property {string} [color] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
36
|
+
* @property {string} [customColor] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
37
|
+
|
|
29
38
|
* @fires action
|
|
30
39
|
* @fires cellChanged
|
|
31
40
|
* @fires cellClicked
|
package/dist/DataTable.doc.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* @property {SortFunction} [sortFunction] - Function for more complex-sorting.
|
|
9
9
|
* @property {boolean} [reverseSort] - True when sorting is reversed.
|
|
10
10
|
* @property {string} [sortColumnID] - The id (or key) property of the column being sorted by.
|
|
11
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortAscendingIcon] - Svelte component class to be displayed in columns that are sorting ascended. Defaults to AngleDownSolid from {@link https://flowbite.com/icons/}
|
|
12
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortDescendingIcon] - Svelte component class to be displayed in columns that are sorting descended. Defaults to AngleUpSolid from {@link https://flowbite.com/icons/}
|
|
11
13
|
* @property {string} [itemKey] - Property to use when distinguish between rows. If not populated, the array index is used.
|
|
12
14
|
* @property {EnterAction} [enterAction] - Determines what pressing the enter key in a focused cell does. Can be 'next', 'down', or 'stay'. Default is 'next'. If 'next', enter will act the same as tab. If 'down', focus will move down to the row below the current one. If 'stay', the enter key will not cause movement.
|
|
13
15
|
* @property {string} [divClassAppend] - Classes to be appended to Table.divClass .
|
|
@@ -27,6 +29,13 @@
|
|
|
27
29
|
* @property {string} [trClassAppend] - Classes to be appended to TableBodyRow.class . trClassGetter can further modify this.
|
|
28
30
|
* @property {string} [trClassOverride] - Classes to replace TableBodyRow.class with. trClassGetter can further modify this.
|
|
29
31
|
* @property {RowClassFunction} [trClassGetter] - Calculates the CSS classes for a TableBodyRow .
|
|
32
|
+
* @property {boolean} [striped] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
33
|
+
* @property {boolean} [hoverable] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
34
|
+
* @property {boolean} [noborder] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
35
|
+
* @property {boolean} [shadow] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
36
|
+
* @property {string} [color] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
37
|
+
* @property {string} [customColor] - Passes through to on {@link https://flowbite-svelte.com/docs/components/table}
|
|
38
|
+
|
|
30
39
|
* @fires action
|
|
31
40
|
* @fires cellChanged
|
|
32
41
|
* @fires cellClicked
|
package/dist/DataTable.svelte
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script>import { createEventDispatcher } from "svelte";
|
|
2
2
|
import { Table, TableBody, TableBodyRow, TableHead } from "flowbite-svelte";
|
|
3
|
+
import { AngleDownSolid, AngleUpSolid } from "flowbite-svelte-icons";
|
|
3
4
|
import DataTableDataCell from "./DataTableDataCell.svelte";
|
|
4
5
|
import DataTableHeaderCell from "./DataTableHeaderCell.svelte";
|
|
5
6
|
import { joinClasses } from "./common.js";
|
|
@@ -9,6 +10,8 @@ export let sortKey = "";
|
|
|
9
10
|
export let sortFunction = null;
|
|
10
11
|
export let reverseSort = false;
|
|
11
12
|
export let sortColumnID = null;
|
|
13
|
+
export let sortAscendingIcon = AngleDownSolid;
|
|
14
|
+
export let sortDescendingIcon = AngleUpSolid;
|
|
12
15
|
export let itemKey = null;
|
|
13
16
|
export let enterAction = "next";
|
|
14
17
|
export let divClassAppend = "";
|
|
@@ -46,14 +49,23 @@ let sortedItems = [];
|
|
|
46
49
|
let focusedColumnKeyID = null;
|
|
47
50
|
let focusedItemKey = null;
|
|
48
51
|
const sortBySortKey = (a, b) => {
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
let aValue = a[sortKey];
|
|
53
|
+
let bValue = b[sortKey];
|
|
54
|
+
if (aValue === bValue) {
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
51
57
|
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
52
58
|
return aValue.localeCompare(bValue);
|
|
53
59
|
}
|
|
54
|
-
if (
|
|
60
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
55
61
|
return aValue - bValue;
|
|
56
62
|
}
|
|
63
|
+
if (typeof aValue === "number" || typeof bValue === "number") {
|
|
64
|
+
return (aValue || 0) - (bValue || 0);
|
|
65
|
+
}
|
|
66
|
+
if (typeof aValue === "string" || typeof bValue === "string") {
|
|
67
|
+
return (aValue || "").localeCompare(bValue || "");
|
|
68
|
+
}
|
|
57
69
|
return JSON.stringify(aValue).localeCompare(JSON.stringify(bValue));
|
|
58
70
|
};
|
|
59
71
|
$: {
|
|
@@ -200,6 +212,8 @@ const rowClicked = (item) => {
|
|
|
200
212
|
{column}
|
|
201
213
|
isSorted={!!sortColumnID && getColumnID(column) === sortColumnID}
|
|
202
214
|
{reverseSort}
|
|
215
|
+
defaultSortAscendingIcon={sortAscendingIcon}
|
|
216
|
+
defaultSortDescendingIcon={sortDescendingIcon}
|
|
203
217
|
{thClass}
|
|
204
218
|
on:click={() => headerClicked(column)}
|
|
205
219
|
/>
|
|
@@ -8,6 +8,8 @@ declare const __propDef: {
|
|
|
8
8
|
sortFunction?: SortFunction | undefined | null;
|
|
9
9
|
reverseSort?: boolean | undefined;
|
|
10
10
|
sortColumnID?: string | null | undefined;
|
|
11
|
+
sortAscendingIcon?: ConstructorOfATypedSvelteComponent | undefined;
|
|
12
|
+
sortDescendingIcon?: ConstructorOfATypedSvelteComponent | undefined;
|
|
11
13
|
itemKey?: string | null | undefined;
|
|
12
14
|
enterAction?: EnterAction | undefined;
|
|
13
15
|
divClassAppend?: string | undefined;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
<script>import { TableHeadCell } from "flowbite-svelte";
|
|
2
|
-
import { AngleDownSolid, AngleUpSolid } from "flowbite-svelte-icons";
|
|
3
2
|
import { joinClasses } from "./common.js";
|
|
4
3
|
export let column;
|
|
5
4
|
export let reverseSort;
|
|
6
5
|
export let isSorted;
|
|
6
|
+
export let defaultSortAscendingIcon;
|
|
7
|
+
export let defaultSortDescendingIcon;
|
|
7
8
|
export let thClass;
|
|
9
|
+
const sortAscendingIcon = column.sortAscendingIcon || defaultSortAscendingIcon;
|
|
10
|
+
const sortDescendingIcon = column.sortDescendingIcon || defaultSortDescendingIcon;
|
|
8
11
|
</script>
|
|
9
12
|
|
|
10
13
|
<TableHeadCell
|
|
@@ -13,9 +16,9 @@ export let thClass;
|
|
|
13
16
|
>
|
|
14
17
|
{#if isSorted}
|
|
15
18
|
{#if reverseSort}
|
|
16
|
-
<
|
|
19
|
+
<svelte:component this={sortAscendingIcon} class="inline h-3 w-3" />
|
|
17
20
|
{:else}
|
|
18
|
-
<
|
|
21
|
+
<svelte:component this={sortDescendingIcon} class="inline h-3 w-3" />
|
|
19
22
|
{/if}
|
|
20
23
|
{/if}
|
|
21
24
|
<span>{column.title}</span>
|
|
@@ -5,6 +5,8 @@ declare const __propDef: {
|
|
|
5
5
|
column: InternalColumnConfig;
|
|
6
6
|
reverseSort: boolean;
|
|
7
7
|
isSorted: boolean;
|
|
8
|
+
defaultSortAscendingIcon: ConstructorOfATypedSvelteComponent;
|
|
9
|
+
defaultSortDescendingIcon: ConstructorOfATypedSvelteComponent;
|
|
8
10
|
thClass: string;
|
|
9
11
|
};
|
|
10
12
|
events: {
|
package/dist/common.d.ts
CHANGED
|
@@ -70,6 +70,8 @@ b: any) => number;
|
|
|
70
70
|
* @property {string} [thClassOverride] - Class to replace existing CSS for header cells.
|
|
71
71
|
* @property {boolean} [canFocus] - True if cells in this column can be focused.
|
|
72
72
|
* @property {boolean} [canSort] - If true, clicking the column's header will set sorting to sortFunction or sortKey, or reverse it if already set.
|
|
73
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortAscendingIcon] - Svelte component class to be displayed in the column is sorting ascended (overrides table).
|
|
74
|
+
* @property {ConstructorOfATypedSvelteComponent} [sortDescendingIcon] - Svelte component class to be displayed in the column is sorting descended (overrides table).
|
|
73
75
|
* @property {SortFunction} [sortFunction] - Comparison function for complex sorting.
|
|
74
76
|
* @property {string} [sortKey] - Item property to sort by, if sortFunction is not defined.
|
|
75
77
|
*/
|
|
@@ -93,6 +95,8 @@ export interface ColumnConfig {
|
|
|
93
95
|
canSort?: boolean;
|
|
94
96
|
sortFunction?: SortFunction;
|
|
95
97
|
sortKey?: string;
|
|
98
|
+
sortAscendingIcon?: ConstructorOfATypedSvelteComponent;
|
|
99
|
+
sortDescendingIcon?: ConstructorOfATypedSvelteComponent;
|
|
96
100
|
}
|
|
97
101
|
/**
|
|
98
102
|
* Returns the a class string for a data row.
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emamid/svelte-data-table",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"homepage": "
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"homepage": "https://github.com/emamid/svelte-data-table",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "vite dev",
|
|
7
7
|
"build": "vite build && npm run package",
|
|
8
8
|
"preview": "vite preview",
|
|
9
|
-
"package": "svelte-kit sync && svelte-package && publint",
|
|
9
|
+
"package": "svelte-kit sync && svelte-package && npm run jsdoc && publint",
|
|
10
10
|
"prepublishOnly": "npm run package",
|
|
11
11
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
12
12
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|