@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
@@ -1,252 +0,0 @@
1
- <script>
2
- import { VirtualList } from 'svelte-virtuallists';
3
-
4
- // shared default no-op function (accepts any args) so prop types allow being called with a payload
5
- const DEFAULT_NOOP = (..._args) => {};
6
-
7
- // Default sort behavior: sort by the header string alphabetically, toggling asc/desc
8
- // and actually sort the items array
9
- function defaultSort(key) {
10
- if (sortKey === key) {
11
- sortDir = sortDir === 'asc' ? 'desc' : 'asc';
12
- } else {
13
- sortKey = key;
14
- sortDir = 'asc';
15
- }
16
-
17
- // Actually sort the items array
18
- items = [...items].sort((a, b) => {
19
- const aVal = a[key];
20
- const bVal = b[key];
21
-
22
- // Handle null/undefined values
23
- if (aVal == null && bVal == null) return 0;
24
- if (aVal == null) return 1;
25
- if (bVal == null) return -1;
26
-
27
- // Convert to strings for comparison
28
- const aStr = String(aVal);
29
- const bStr = String(bVal);
30
-
31
- // Alphabetical comparison
32
- const comparison = aStr.localeCompare(bStr, undefined, {
33
- numeric: true,
34
- sensitivity: 'base',
35
- });
36
-
37
- // Apply sort direction
38
- return sortDir === 'asc' ? comparison : -comparison;
39
- });
40
- }
41
-
42
- // Runic props: accept props via $props() so the component matches the
43
- // project's runes-style components. Include the `select` callback prop.
44
- let {
45
- items = [],
46
- visibleKeys = [],
47
- // treat sortKey/sortDir as normal props; parent may control them and
48
- // provide a `sort` callback to update them.
49
- sortKey = null,
50
- sortDir = 'asc',
51
- className = '',
52
- style = '',
53
- emptyMessage = 'No items to display.',
54
- colWidths = {},
55
- selected = null,
56
- // external selection callback with a safe default no-op
57
- selectCallback = DEFAULT_NOOP,
58
- // new prop name: sortCallback defaulting to `defaultSort` (alphabetical string sort)
59
- sortCallback = defaultSort,
60
- // New prop: allow consumers to disable virtualization (useful when paginating by fixed item counts)
61
- // If `virtualize` is false we ask the wrapped VirtualList to render as a normal list/table
62
- // by setting its `isDisabled` prop. Default: true (virtualization enabled).
63
- virtualize = true,
64
- // <slot> -> @render migration
65
- rowSnippet,
66
- } = $props();
67
-
68
- // expose selection via the callback prop (if provided)
69
- function selectItem(item, index) {
70
- selected = item;
71
- try {
72
- callSelectCallback({ item, index });
73
- } catch (err) {
74
- try {
75
- console.error('selectCallback threw:', err);
76
- } catch (e) {}
77
- }
78
- }
79
-
80
- // Wrapper that casts the possibly-typed callback to any before invoking so callers may pass a payload
81
- function callSelectCallback(payload) {
82
- try {
83
- /** @type {any} */ (selectCallback)(payload);
84
- } catch (err) {
85
- try {
86
- console.error('selectCallback threw:', err);
87
- } catch (e) {}
88
- }
89
- }
90
-
91
- // When header is clicked update the exported sortKey/sortDir so parents
92
- // using bind:sortKey / bind:sortDir receive updates (no event dispatching).
93
- function handleSort(key) {
94
- // Delegate sorting to the configured callback (by default `defaultSort` will run)
95
- try {
96
- sortCallback(key);
97
- } catch (err) {
98
- try {
99
- console.error('sortCallback threw:', err);
100
- } catch (e) {}
101
- }
102
- }
103
-
104
- // Normalize colWidths: handle object or array input, treat numeric as stretch weights.
105
- // NOTE: percentage strings (e.g. '30%') are no longer supported and will be ignored with a warning.
106
- function normalizeColWidths() {
107
- const map = {};
108
-
109
- // Build an ordered list of entries for visibleKeys
110
- const entries = visibleKeys.map((key, i) => {
111
- let v;
112
- if (Array.isArray(colWidths)) v = colWidths[i];
113
- else if (colWidths && typeof colWidths === 'object') v = colWidths[key];
114
- else v = undefined;
115
- return { key, v };
116
- });
117
-
118
- // Collect numeric stretch weights and pass-through non-percent strings
119
- const numeric = [];
120
- const otherStrings = [];
121
-
122
- for (const e of entries) {
123
- const v = e.v;
124
- if (typeof v === 'number' && !Number.isNaN(v)) {
125
- numeric.push({ key: e.key, w: v });
126
- } else if (typeof v === 'string') {
127
- const s = v.trim();
128
- if (s.endsWith('%')) {
129
- // Percent strings are intentionally unsupported now - ignore silently
130
- } else {
131
- // keep other string units (e.g., '120px')
132
- otherStrings.push({ key: e.key, v: s });
133
- }
134
- }
135
- }
136
-
137
- const totalWeight = numeric.reduce((s, r) => s + r.w, 0);
138
- const availablePct = 100;
139
-
140
- if (numeric.length > 0 && totalWeight > 0) {
141
- let acc = 0;
142
- numeric.forEach((n, idx) => {
143
- const pct =
144
- idx === numeric.length - 1
145
- ? Math.max(0, Math.round((availablePct - acc) * 100) / 100)
146
- : Math.round((n.w / totalWeight) * availablePct * 100) / 100;
147
- map[n.key] = `${pct}%`;
148
- acc += pct;
149
- });
150
- }
151
-
152
- // Copy through non-percent strings (e.g. '120px')
153
- for (const o of otherStrings) {
154
- map[o.key] = o.v;
155
- }
156
-
157
- return map;
158
- }
159
-
160
- // Compute column width by key (fallback to even distribution)
161
- function calcColWidth(key, index) {
162
- try {
163
- const norm = normalizeColWidths();
164
- if (norm && norm[key] !== undefined) return String(norm[key]);
165
-
166
- // Fallbacks: accept string widths (e.g. '120px') passed directly by the user.
167
- // Numeric values should be provided as stretch weights and will be handled
168
- // by normalizeColWidths(); if we encounter numeric values here, warn and ignore them.
169
- if (Array.isArray(colWidths) && colWidths[index] !== undefined) {
170
- const v = colWidths[index];
171
- if (typeof v === 'string') {
172
- const s = v.trim();
173
- if (!s.endsWith('%')) {
174
- return v; // e.g. '120px'
175
- }
176
- // percent strings are ignored silently
177
- }
178
- }
179
- if (typeof colWidths === 'object' && colWidths[key] !== undefined) {
180
- const v = colWidths[key];
181
- if (typeof v === 'string') {
182
- const s = v.trim();
183
- if (!s.endsWith('%')) {
184
- return v; // e.g. '120px'
185
- }
186
- // percent strings are ignored silently
187
- }
188
- }
189
- } catch (err) {
190
- // ignore and fall through to defaults
191
- }
192
- const n = Math.max(1, visibleKeys.length);
193
- const pct = Math.floor(100 / n);
194
- return pct + '%';
195
- }
196
- </script>
197
-
198
- <section class="virtual-data-table {className}" {style}>
199
- {#if items && items.length > 0}
200
- <!-- When `virtualize` is false we instruct VirtualList to render as a normal list/table
201
- by setting `isDisabled`. This ensures pagination based on item counts shows the
202
- exact number of rows expected (useful when rows have variable height). -->
203
- <VirtualList
204
- {items}
205
- isTable
206
- isDisabled={!virtualize}
207
- class="datatable-table max-w-full max-h-full overflow-auto"
208
- >
209
- <!-- colgroup enforces the column widths so table-layout: fixed distributes as intended -->
210
- {#snippet header()}
211
- <colgroup>
212
- {#each visibleKeys as key, i}
213
- <col style="width: {calcColWidth(key, i)}" />
214
- {/each}
215
- </colgroup>
216
- <thead class="sticky-header">
217
- <tr>
218
- {#each visibleKeys as key, i}
219
- <th
220
- style="width: {calcColWidth(key, i)}"
221
- class="cursor-pointer select-none"
222
- onclick={() => handleSort(key)}
223
- aria-sort={sortKey === key
224
- ? sortDir === 'asc'
225
- ? 'ascending'
226
- : 'descending'
227
- : 'none'}
228
- >
229
- <div class="inline-flex items-center gap-1">
230
- <span>{key}</span>
231
- {#if sortKey === key}
232
- <span aria-hidden="true">{sortDir === 'asc' ? '▲' : '▼'}</span>
233
- {/if}
234
- </div>
235
- </th>
236
- {/each}
237
- </tr>
238
- </thead>
239
- {/snippet}
240
-
241
- {#snippet vl_slot({ index, item })}
242
- {@render rowSnippet({ item, index, select: () => selectItem(item, index), selected })}
243
- {/snippet}
244
- </VirtualList>
245
- {:else}
246
- <div class="p-4 text-center text-slate-500">{emptyMessage}</div>
247
- {/if}
248
- </section>
249
-
250
- <style>
251
- @import '../lib/dist/styles.css';
252
- </style>