@zhangt58/svelte-vtable 0.1.4 → 0.3.1

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.
Files changed (36) hide show
  1. package/README.md +177 -44
  2. package/dist/components/DataTable.svelte +489 -0
  3. package/dist/components/DataTable.svelte.d.ts +31 -0
  4. package/dist/components/DataTableControls.svelte +576 -0
  5. package/dist/components/DataTableControls.svelte.d.ts +39 -0
  6. package/dist/components/DataTableFilters.svelte +215 -0
  7. package/dist/components/DataTableFilters.svelte.d.ts +37 -0
  8. package/dist/components/FiltersModal.svelte +65 -0
  9. package/dist/components/FiltersModal.svelte.d.ts +25 -0
  10. package/dist/components/PaginationBar.svelte +86 -0
  11. package/dist/components/PaginationBar.svelte.d.ts +15 -0
  12. package/dist/components/filters/FilterColumn.svelte +53 -0
  13. package/dist/components/filters/FilterColumn.svelte.d.ts +19 -0
  14. package/dist/components/filters/FilterDateRange.svelte +341 -0
  15. package/dist/components/filters/FilterDateRange.svelte.d.ts +19 -0
  16. package/dist/components/filters/FilterValueList.svelte +437 -0
  17. package/dist/components/filters/FilterValueList.svelte.d.ts +33 -0
  18. package/dist/components/filters/useFilterState.svelte.d.ts +25 -0
  19. package/dist/components/filters/useFilterState.svelte.js +132 -0
  20. package/dist/components/flowbite/DataTableControls.svelte +525 -0
  21. package/dist/components/flowbite/DataTableControls.svelte.d.ts +39 -0
  22. package/{src/components → dist/components/flowbite}/FiltersModal.svelte +5 -15
  23. package/dist/components/flowbite/FiltersModal.svelte.d.ts +25 -0
  24. package/dist/filterUtils.d.ts +76 -0
  25. package/{src/lib → dist}/filterUtils.js +48 -46
  26. package/dist/index.d.ts +60 -0
  27. package/dist/index.flowbite.d.ts +3 -0
  28. package/dist/index.flowbite.js +9 -0
  29. package/dist/index.js +49 -0
  30. package/{src/lib → dist}/styles.css +26 -3
  31. package/package.json +38 -17
  32. package/src/components/DataTable.svelte +0 -252
  33. package/src/components/DataTableControls.svelte +0 -500
  34. package/src/components/DataTableFilters.svelte +0 -1225
  35. package/src/lib/dist/styles.css +0 -273
  36. package/src/lib/index.js +0 -16
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Utility functions for working with DataTableFilters
3
+ */
4
+ /**
5
+ * Extract unique values and their counts from an array of objects for a given key
6
+ * @param {Array} data - Array of data objects
7
+ * @param {string} key - The property key to extract unique values from
8
+ * @returns {{uniqueValues: Array, counts: Object}} Object with unique values array and counts map
9
+ */
10
+ export function getUniqueValuesWithCounts(data: any[], key: string): {
11
+ uniqueValues: any[];
12
+ counts: any;
13
+ };
14
+ /**
15
+ * Build column filters configuration from data and column definitions.
16
+ * Columns with `filterType: 'none'` are excluded from the returned array.
17
+ * @param {Array} data - Array of data objects
18
+ * @param {import('./index.js').ColumnDef[]} columns - Array of ColumnDef objects.
19
+ * @returns {Array} Column filters configuration for DataTableFilters
20
+ */
21
+ export function buildColumnFilters(data: any[], columns: import("./index.js").ColumnDef[]): any[];
22
+ /**
23
+ * Check whether a value falls within a date/datetime range.
24
+ * Both `from` and `to` are optional; omitting one leaves that end open.
25
+ * Date-only strings (YYYY-MM-DD) are parsed as **local** midnight (i.e. they
26
+ * are treated as `<date>T00:00:00` in the local timezone, not UTC). A date-only
27
+ * `to` value is inclusive through the end of that calendar day (23:59:59.999
28
+ * local time).
29
+ * @param {any} value - The item's date value (string, Date, or number)
30
+ * @param {string|null|undefined} from - Range start (ISO date or datetime-local string)
31
+ * @param {string|null|undefined} to - Range end (ISO date or datetime-local string)
32
+ * @returns {boolean}
33
+ */
34
+ export function matchesDateRange(value: any, from: string | null | undefined, to: string | null | undefined): boolean;
35
+ /**
36
+ * Apply filters to data with OR logic within columns and AND logic across columns.
37
+ * Supports both value-selection filters (array) and date/datetime range filters
38
+ * (object with optional `from` and `to` string fields).
39
+ * @param {Array} data - Array of data objects to filter
40
+ * @param {Object} activeFilters - Active filters: { columnKey: string[] | {from?, to?} }
41
+ * @returns {Array} Filtered data array
42
+ */
43
+ export function applyFilters(data: any[], activeFilters: any): any[];
44
+ /**
45
+ * Check if any filters are currently active
46
+ * @param {Object} activeFilters - Active filters object
47
+ * @returns {boolean} True if any filters are active
48
+ */
49
+ export function hasActiveFilters(activeFilters: any): boolean;
50
+ /**
51
+ * Count total number of active filter selections.
52
+ * Each active date range counts as 1.
53
+ * @param {Object} activeFilters - Active filters object
54
+ * @returns {number} Total count of active filter values/ranges
55
+ */
56
+ export function countActiveFilters(activeFilters: any): number;
57
+ /**
58
+ * Clear all filters
59
+ * @param {Array} columnFilters - Column filters configuration
60
+ * @returns {Object} Cleared filters object (arrays for value columns, empty objects for date ranges)
61
+ */
62
+ export function clearAllFilters(columnFilters: any[]): any;
63
+ /**
64
+ * Export active filters as URL search params.
65
+ * Date range filters are serialised as `<key>_from` and `<key>_to` params.
66
+ * @param {Object} activeFilters - Active filters object
67
+ * @returns {URLSearchParams} URL search params object
68
+ */
69
+ export function filtersToSearchParams(activeFilters: any): URLSearchParams;
70
+ /**
71
+ * Import active filters from URL search params
72
+ * @param {URLSearchParams|string} searchParams - URL search params or query string
73
+ * @param {Array} columnFilters - Column filters configuration (to validate keys)
74
+ * @returns {Object} Active filters object
75
+ */
76
+ export function searchParamsToFilters(searchParams: URLSearchParams | string, columnFilters: any[]): any;
@@ -25,64 +25,66 @@ export function getUniqueValuesWithCounts(data, key) {
25
25
  }
26
26
 
27
27
  /**
28
- * Build column filters configuration from data and column definitions
28
+ * Build column filters configuration from data and column definitions.
29
+ * Columns with `filterType: 'none'` are excluded from the returned array.
29
30
  * @param {Array} data - Array of data objects
30
- * @param {Array} columns - Array of column definitions {key, label, type?}
31
- * Supported types: 'value' (default), 'daterange', 'datetimerange'
31
+ * @param {import('./index.js').ColumnDef[]} columns - Array of ColumnDef objects.
32
32
  * @returns {Array} Column filters configuration for DataTableFilters
33
33
  */
34
34
  export function buildColumnFilters(data, columns) {
35
- return columns.map((col) => {
36
- const isDateRange = col.type === 'daterange' || col.type === 'datetimerange';
37
- if (isDateRange) {
38
- // Scan data to find the min/max timestamps for this column so the UI can
39
- // offer a dual-range slider and Earliest/Latest shortcut buttons.
40
- let minMs = Infinity;
41
- let maxMs = -Infinity;
42
- for (const item of data) {
43
- const v = item[col.key];
44
- if (v === null || v === undefined || v === '') continue;
45
- const str = String(v);
46
- const d = /^\d{4}-\d{2}-\d{2}$/.test(str) ? new Date(str + 'T00:00:00') : new Date(str);
47
- if (!isNaN(d.getTime())) {
48
- if (d.getTime() < minMs) minMs = d.getTime();
49
- if (d.getTime() > maxMs) maxMs = d.getTime();
35
+ const isDateRangeType = (ft) => ft === 'daterange' || ft === 'datetimerange';
36
+ return columns
37
+ .filter((col) => col.filterType !== 'none')
38
+ .map((col) => {
39
+ if (isDateRangeType(col.filterType)) {
40
+ // Scan data to find the min/max timestamps for this column so the UI can
41
+ // offer a dual-range slider and Earliest/Latest shortcut buttons.
42
+ let minMs = Infinity;
43
+ let maxMs = -Infinity;
44
+ for (const item of data) {
45
+ const v = item[col.key];
46
+ if (v === null || v === undefined || v === '') continue;
47
+ const str = String(v);
48
+ const d = /^\d{4}-\d{2}-\d{2}$/.test(str) ? new Date(str + 'T00:00:00') : new Date(str);
49
+ if (!isNaN(d.getTime())) {
50
+ if (d.getTime() < minMs) minMs = d.getTime();
51
+ if (d.getTime() > maxMs) maxMs = d.getTime();
52
+ }
50
53
  }
51
- }
52
54
 
53
- const hasRange = isFinite(minMs) && isFinite(maxMs);
55
+ const hasRange = isFinite(minMs) && isFinite(maxMs);
54
56
 
55
- // Format as the string expected by <input type="date"> or <input type="datetime-local">
56
- const pad = (n) => String(n).padStart(2, '0');
57
- const fmtDate = (ms) => {
58
- const d = new Date(ms);
59
- return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
60
- };
61
- const fmtDatetime = (ms) => {
62
- const d = new Date(ms);
63
- return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
64
- };
65
- const fmt = col.type === 'datetimerange' ? fmtDatetime : fmtDate;
57
+ // Format as the string expected by <input type="date"> or <input type="datetime-local">
58
+ const pad = (n) => String(n).padStart(2, '0');
59
+ const fmtDate = (ms) => {
60
+ const d = new Date(ms);
61
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
62
+ };
63
+ const fmtDatetime = (ms) => {
64
+ const d = new Date(ms);
65
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
66
+ };
67
+ const fmt = col.filterType === 'datetimerange' ? fmtDatetime : fmtDate;
66
68
 
69
+ return {
70
+ key: col.key,
71
+ label: col.label || col.key,
72
+ type: col.filterType,
73
+ uniqueValues: [],
74
+ counts: {},
75
+ minValue: hasRange ? fmt(minMs) : null,
76
+ maxValue: hasRange ? fmt(maxMs) : null,
77
+ };
78
+ }
79
+ const { uniqueValues, counts } = getUniqueValuesWithCounts(data, col.key);
67
80
  return {
68
81
  key: col.key,
69
82
  label: col.label || col.key,
70
- type: col.type,
71
- uniqueValues: [],
72
- counts: {},
73
- minValue: hasRange ? fmt(minMs) : null,
74
- maxValue: hasRange ? fmt(maxMs) : null,
83
+ type: col.filterType || 'value',
84
+ uniqueValues,
85
+ counts,
75
86
  };
76
- }
77
- const { uniqueValues, counts } = getUniqueValuesWithCounts(data, col.key);
78
- return {
79
- key: col.key,
80
- label: col.label || col.key,
81
- type: col.type || 'value',
82
- uniqueValues,
83
- counts,
84
- };
85
- });
87
+ });
86
88
  }
87
89
 
88
90
  /**
@@ -0,0 +1,60 @@
1
+ export { default as DataTableControls } from "./components/DataTableControls.svelte";
2
+ export { default as DataTableFilters } from "./components/DataTableFilters.svelte";
3
+ export { default as DataTable } from "./components/DataTable.svelte";
4
+ export { default as FilterColumn } from "./components/filters/FilterColumn.svelte";
5
+ export { default as FilterValueList } from "./components/filters/FilterValueList.svelte";
6
+ export { default as FilterDateRange } from "./components/filters/FilterDateRange.svelte";
7
+ export { useFilterState } from "./components/filters/useFilterState.svelte.js";
8
+ /**
9
+ * Unified column definition for DataTable.
10
+ * Pass an array of `ColumnDef` objects as the `columns` prop to configure
11
+ * visible keys, widths, sorting, filters, and custom snippets from a single place.
12
+ */
13
+ export type ColumnDef<T extends Record<string, unknown> = Record<string, unknown>> = {
14
+ /**
15
+ * - Data key. Must match a property on the row item.
16
+ */
17
+ key: string;
18
+ /**
19
+ * - Display label for the column header. Defaults to `key`.
20
+ */
21
+ label?: string;
22
+ /**
23
+ * - Column width: a stretch weight (number) or a
24
+ * CSS width value such as `'120px'`. Defaults to `1` (equal stretch).
25
+ */
26
+ width?: number | string;
27
+ /**
28
+ * - Whether clicking the column header sorts the
29
+ * table. Defaults to `true`.
30
+ */
31
+ sortable?: boolean;
32
+ /**
33
+ * Filter UI to render for this column:
34
+ * - `'value'` — checkbox list of unique values (default)
35
+ * - `'daterange'` — date-only range picker
36
+ * - `'datetimerange'` — datetime range picker
37
+ * - `'none'` — no filter UI for this column
38
+ */
39
+ filterType?: "value" | "daterange" | "datetimerange" | "none";
40
+ /**
41
+ * Custom Svelte 5 snippet rendered inside the `<th>`. Receives
42
+ * `{ key, label, sortKey, sortDir }`.
43
+ */
44
+ headerSnippet?: import("svelte").Snippet<[{
45
+ key: string;
46
+ label: string;
47
+ sortKey: string | null;
48
+ sortDir: string;
49
+ }]>;
50
+ /**
51
+ * Custom Svelte 5 snippet rendered inside each `<td>` for this column.
52
+ * Receives `{ item, value, index }`. When omitted the raw value is rendered.
53
+ */
54
+ cellSnippet?: import("svelte").Snippet<[{
55
+ item: T;
56
+ value: unknown;
57
+ index: number;
58
+ }]>;
59
+ };
60
+ export { getUniqueValuesWithCounts, buildColumnFilters, applyFilters, matchesDateRange, hasActiveFilters, countActiveFilters, clearAllFilters, filtersToSearchParams, searchParamsToFilters } from "./filterUtils.js";
@@ -0,0 +1,3 @@
1
+ export { default as DataTableControls } from "./components/flowbite/DataTableControls.svelte";
2
+ export { default as FiltersModal } from "./components/flowbite/FiltersModal.svelte";
3
+ export * from "./index.js";
@@ -0,0 +1,9 @@
1
+ // Flowbite-flavoured adapter components.
2
+ // Import from '@zhangt58/svelte-vtable/flowbite' to use these.
3
+ // Requires flowbite-svelte and flowbite-svelte-icons as peer dependencies.
4
+ export { default as DataTableControls } from './components/flowbite/DataTableControls.svelte';
5
+ export { default as FiltersModal } from './components/flowbite/FiltersModal.svelte';
6
+
7
+ // Re-export everything from the headless entrypoint so consumers can import
8
+ // DataTable, DataTableFilters, and utilities from a single path.
9
+ export * from './index.js';
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ export { default as DataTableControls } from './components/DataTableControls.svelte';
2
+ export { default as DataTableFilters } from './components/DataTableFilters.svelte';
3
+ export { default as DataTable } from './components/DataTable.svelte';
4
+
5
+ // Filter sub-components (individually importable)
6
+ export { default as FilterColumn } from './components/filters/FilterColumn.svelte';
7
+ export { default as FilterValueList } from './components/filters/FilterValueList.svelte';
8
+ export { default as FilterDateRange } from './components/filters/FilterDateRange.svelte';
9
+ export { useFilterState } from './components/filters/useFilterState.svelte.js';
10
+
11
+ // Export filter utility functions
12
+ export {
13
+ getUniqueValuesWithCounts,
14
+ buildColumnFilters,
15
+ applyFilters,
16
+ matchesDateRange,
17
+ hasActiveFilters,
18
+ countActiveFilters,
19
+ clearAllFilters,
20
+ filtersToSearchParams,
21
+ searchParamsToFilters,
22
+ } from './filterUtils.js';
23
+
24
+ /**
25
+ * Unified column definition for DataTable.
26
+ * Pass an array of `ColumnDef` objects as the `columns` prop to configure
27
+ * visible keys, widths, sorting, filters, and custom snippets from a single place.
28
+ *
29
+ * @template {Record<string, unknown>} [T=Record<string, unknown>]
30
+ * @typedef {Object} ColumnDef
31
+ * @property {string} key - Data key. Must match a property on the row item.
32
+ * @property {string} [label] - Display label for the column header. Defaults to `key`.
33
+ * @property {number|string} [width] - Column width: a stretch weight (number) or a
34
+ * CSS width value such as `'120px'`. Defaults to `1` (equal stretch).
35
+ * @property {boolean} [sortable] - Whether clicking the column header sorts the
36
+ * table. Defaults to `true`.
37
+ * @property {'value'|'daterange'|'datetimerange'|'none'} [filterType]
38
+ * Filter UI to render for this column:
39
+ * - `'value'` — checkbox list of unique values (default)
40
+ * - `'daterange'` — date-only range picker
41
+ * - `'datetimerange'` — datetime range picker
42
+ * - `'none'` — no filter UI for this column
43
+ * @property {import('svelte').Snippet<[{key: string, label: string, sortKey: string|null, sortDir: string}]>} [headerSnippet]
44
+ * Custom Svelte 5 snippet rendered inside the `<th>`. Receives
45
+ * `{ key, label, sortKey, sortDir }`.
46
+ * @property {import('svelte').Snippet<[{item: T, value: unknown, index: number}]>} [cellSnippet]
47
+ * Custom Svelte 5 snippet rendered inside each `<td>` for this column.
48
+ * Receives `{ item, value, index }`. When omitted the raw value is rendered.
49
+ */
@@ -1,3 +1,17 @@
1
+ /* ── Theming contract — override these on :root or any wrapper element ── */
2
+ :root {
3
+ --vtable-color-accent: #16a34a; /* green-600 */
4
+ --vtable-color-accent-light: #dcfce7; /* green-100 */
5
+ --vtable-color-accent-border: #22c55e; /* green-500 */
6
+ --vtable-color-accent-text: #15803d; /* green-700 */
7
+
8
+ --vtable-color-danger: #dc2626; /* red-600 */
9
+ --vtable-color-info: #3b82f6; /* blue-500 */
10
+
11
+ --vtable-radius: 0.375rem; /* rounded-md */
12
+ --vtable-row-height: 2.25rem; /* default row height hint for virtual scroll */
13
+ }
14
+
1
15
  .virtual-data-table {
2
16
  display: block;
3
17
  min-width: 0;
@@ -156,6 +170,15 @@
156
170
  }
157
171
 
158
172
  /* Shared control styles (inputs, pagination, column filters) used by page components */
173
+
174
+ /* Accent-color focus ring for search input and per-page select — respects --vtable-color-accent */
175
+ :global(.vtable-search-input:focus),
176
+ :global(.vtable-perpage-select:focus) {
177
+ border-color: var(--vtable-color-accent-border);
178
+ box-shadow: 0 0 0 2px color-mix(in srgb, var(--vtable-color-accent) 20%, transparent);
179
+ outline: none;
180
+ }
181
+
159
182
  :global(.datatable-input) {
160
183
  padding: 0.375rem 0.75rem;
161
184
  font-size: 0.95rem;
@@ -220,11 +243,11 @@
220
243
  color: #fff;
221
244
  }
222
245
 
223
- /* Force the active/current page to use the green tokens (override flowbite's primary/orange).
246
+ /* Force the active/current page to use the accent tokens (override flowbite's primary/orange).
224
247
  Intentionally specific to reliably win over third-party rules. */
225
248
  :global([data-pagination-id] ul li [aria-current='page']) {
226
- background-color: #dcfce7 !important; /* bg-green-100 */
227
- color: #16a34a !important; /* text-green-600 */
249
+ background-color: var(--vtable-color-accent-light) !important;
250
+ color: var(--vtable-color-accent) !important;
228
251
  border-color: transparent !important;
229
252
  }
230
253
 
package/package.json CHANGED
@@ -1,26 +1,43 @@
1
1
  {
2
2
  "name": "@zhangt58/svelte-vtable",
3
- "version": "0.1.4",
3
+ "version": "0.3.1",
4
4
  "description": "A Svelte 5 library for virtualized data tables with sorting, selection, and pagination controls.",
5
5
  "type": "module",
6
- "svelte": "./src/lib/index.js",
7
- "main": "./src/lib/index.js",
8
- "module": "./src/lib/index.js",
6
+ "sideEffects": [
7
+ "*.css"
8
+ ],
9
+ "svelte": "./dist/index.js",
10
+ "main": "./dist/index.js",
11
+ "module": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
9
13
  "exports": {
10
14
  ".": {
11
- "svelte": "./src/lib/index.js",
12
- "import": "./src/lib/index.js",
13
- "default": "./src/lib/index.js"
15
+ "svelte": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./controls": {
20
+ "svelte": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ },
24
+ "./flowbite": {
25
+ "svelte": "./dist/index.flowbite.js",
26
+ "types": "./dist/index.flowbite.d.ts",
27
+ "import": "./dist/index.flowbite.js"
14
28
  },
15
- "./styles.css": "./src/lib/dist/styles.css"
29
+ "./styles.css": "./dist/styles.css"
16
30
  },
17
31
  "scripts": {
18
- "check": "svelte-check --tsconfig ./jsconfig.json",
32
+ "check": "svelte-check --tsconfig ./tsconfig.json",
19
33
  "build:styles": "node ./scripts/build-styles.cjs",
20
- "prepare": "npm run build:styles",
21
- "prepublishOnly": "npm run build:styles && npm run check",
34
+ "build": "svelte-package -i src/lib",
35
+ "prepare": "npm run build:styles && npm run build",
36
+ "prepublishOnly": "npm run build && npm run check && npm run test",
22
37
  "format": "prettier --write .",
23
- "format:check": "prettier --check ."
38
+ "format:check": "prettier --check .",
39
+ "test": "vitest run",
40
+ "test:watch": "vitest"
24
41
  },
25
42
  "keywords": [
26
43
  "svelte",
@@ -43,25 +60,29 @@
43
60
  },
44
61
  "homepage": "https://github.com/zhangt58/svelte-vtable#readme",
45
62
  "peerDependencies": {
46
- "flowbite-svelte": "^1.28.1",
47
- "flowbite-svelte-icons": "^3.0.0",
48
63
  "svelte": "^5.43.5",
49
64
  "svelte-virtuallists": "^1.4.2",
50
65
  "tailwindcss": "^4.1.17"
51
66
  },
67
+ "optionalPeerDependencies": {
68
+ "flowbite-svelte": "^1.28.1",
69
+ "flowbite-svelte-icons": "^3.0.0"
70
+ },
52
71
  "devDependencies": {
72
+ "@sveltejs/package": "^2.5.7",
53
73
  "@sveltejs/vite-plugin-svelte": "^6.2.1",
54
74
  "flowbite-svelte": "^1.28.1",
55
75
  "flowbite-svelte-icons": "^3.0.0",
76
+ "prettier": "^3.7.4",
77
+ "prettier-plugin-svelte": "^3.4.1",
56
78
  "svelte": "^5.0.0",
57
79
  "svelte-check": "^4.3.4",
58
80
  "svelte-virtuallists": "^1.4.2",
59
81
  "vite": "^7.2.2",
60
- "prettier": "^3.7.4",
61
- "prettier-plugin-svelte": "^3.4.1"
82
+ "vitest": "^2.1.9"
62
83
  },
63
84
  "files": [
64
- "src",
85
+ "dist",
65
86
  "README.md",
66
87
  "LICENSE"
67
88
  ]