@zhangt58/svelte-vtable 0.1.4 → 0.2.0
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 +138 -42
- package/package.json +1 -1
- package/src/components/DataTable.svelte +111 -129
- package/src/components/DataTableControls.svelte +54 -284
- package/src/components/DataTableFilters.svelte +96 -1121
- package/src/components/FiltersModal.svelte +3 -13
- package/src/components/PaginationBar.svelte +86 -0
- package/src/components/filters/FilterColumn.svelte +46 -0
- package/src/components/filters/FilterDateRange.svelte +341 -0
- package/src/components/filters/FilterValueList.svelte +419 -0
- package/src/components/filters/useFilterState.svelte.js +132 -0
- package/src/lib/dist/styles.css +17 -3
- package/src/lib/filterUtils.js +48 -46
- package/src/lib/index.js +33 -0
- package/src/lib/styles.css +17 -3
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { Badge } from 'flowbite-svelte';
|
|
3
|
+
import { ArrowDownOutline } from 'flowbite-svelte-icons';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Renders the checkbox value-list for a single filter column, including:
|
|
7
|
+
* - per-column search bar
|
|
8
|
+
* - sort buttons (name / number / count)
|
|
9
|
+
* - check-all toolbar (All / Invert / None)
|
|
10
|
+
* - checkbox list with optional virtualization and load-on-demand
|
|
11
|
+
*
|
|
12
|
+
* Props:
|
|
13
|
+
* column — column config { key, label, uniqueValues, counts, ... }
|
|
14
|
+
* selections — current selected values array for this column
|
|
15
|
+
* showCounts — whether to show value counts next to each option
|
|
16
|
+
* virtualThreshold — render virtual list when options exceed this count
|
|
17
|
+
* virtualItemHeight — px height of each checkbox row (for virtual layout)
|
|
18
|
+
* virtualOverscan — extra rows to render above/below the visible window
|
|
19
|
+
* populateThreshold — hide options and show "Load options" when count exceeds this
|
|
20
|
+
* ontoggle — callback(value) when a checkbox is toggled
|
|
21
|
+
* oncheckall — callback(values) when "All" is clicked
|
|
22
|
+
* oninvert — callback(values) when "Invert" is clicked
|
|
23
|
+
* onchecknone — callback() when "None" is clicked
|
|
24
|
+
* onsortchange — callback({ mode, dir }) when sort button is clicked
|
|
25
|
+
*/
|
|
26
|
+
let {
|
|
27
|
+
column,
|
|
28
|
+
selections = [],
|
|
29
|
+
showCounts = true,
|
|
30
|
+
virtualThreshold = 500,
|
|
31
|
+
virtualItemHeight = 36,
|
|
32
|
+
virtualOverscan = 5,
|
|
33
|
+
populateThreshold = 200,
|
|
34
|
+
ontoggle,
|
|
35
|
+
oncheckall,
|
|
36
|
+
oninvert,
|
|
37
|
+
onchecknone,
|
|
38
|
+
onsortchange,
|
|
39
|
+
} = $props();
|
|
40
|
+
|
|
41
|
+
// Internal: whether large option sets have been loaded (computed lazily from props)
|
|
42
|
+
const autoLoaded = $derived(
|
|
43
|
+
populateThreshold <= 0 ||
|
|
44
|
+
(Array.isArray(column.uniqueValues) ? column.uniqueValues.length : 0) <= populateThreshold,
|
|
45
|
+
);
|
|
46
|
+
let manuallyLoaded = $state(false);
|
|
47
|
+
const loaded = $derived(autoLoaded || manuallyLoaded);
|
|
48
|
+
|
|
49
|
+
// Internal: sort state
|
|
50
|
+
let sortMode = $state('name');
|
|
51
|
+
let sortDir = $state('asc');
|
|
52
|
+
|
|
53
|
+
// Internal: per-column search query
|
|
54
|
+
let searchQuery = $state('');
|
|
55
|
+
|
|
56
|
+
// Virtual list scroll tracking
|
|
57
|
+
let listRef = $state(null);
|
|
58
|
+
let scrollTop = $state(0);
|
|
59
|
+
let containerHeight = $state(200);
|
|
60
|
+
|
|
61
|
+
$effect(() => {
|
|
62
|
+
if (!listRef) return;
|
|
63
|
+
const ro = new ResizeObserver(() => {
|
|
64
|
+
containerHeight = listRef.clientHeight || 200;
|
|
65
|
+
});
|
|
66
|
+
ro.observe(listRef);
|
|
67
|
+
containerHeight = listRef.clientHeight || 200;
|
|
68
|
+
return () => ro.disconnect();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
function handleScroll(e) {
|
|
72
|
+
scrollTop = e.currentTarget.scrollTop;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Helper: extract numeric sort key from a value
|
|
76
|
+
function extractNumber(v) {
|
|
77
|
+
if (v === null || v === undefined) return 0;
|
|
78
|
+
if (typeof v === 'number') return v;
|
|
79
|
+
const match = String(v).match(/-?\d+(\.\d+)?/);
|
|
80
|
+
return match ? parseFloat(match[0]) : 0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Sorted + filtered option values for this column
|
|
84
|
+
const sortedValues = $derived.by(() => {
|
|
85
|
+
const raw = Array.isArray(column.uniqueValues) ? column.uniqueValues.slice() : [];
|
|
86
|
+
const q = searchQuery.trim().toLowerCase();
|
|
87
|
+
const filtered = q
|
|
88
|
+
? raw.filter((v) => {
|
|
89
|
+
const text = v === null || v === undefined ? '(empty)' : String(v);
|
|
90
|
+
return text.toLowerCase().includes(q);
|
|
91
|
+
})
|
|
92
|
+
: raw;
|
|
93
|
+
|
|
94
|
+
const dir = sortDir === 'asc' ? 1 : -1;
|
|
95
|
+
const withIndex = filtered.map((v, i) => ({ v, i }));
|
|
96
|
+
withIndex.sort((a, b) => {
|
|
97
|
+
const va = a.v;
|
|
98
|
+
const vb = b.v;
|
|
99
|
+
if (sortMode === 'count' && column.counts) {
|
|
100
|
+
const ca = Number(column.counts[va] ?? 0);
|
|
101
|
+
const cb = Number(column.counts[vb] ?? 0);
|
|
102
|
+
if (ca !== cb) return (ca - cb) * dir * -1;
|
|
103
|
+
}
|
|
104
|
+
if (sortMode === 'number') {
|
|
105
|
+
const na = extractNumber(va);
|
|
106
|
+
const nb = extractNumber(vb);
|
|
107
|
+
if (na !== nb) return (na - nb) * dir;
|
|
108
|
+
}
|
|
109
|
+
const sa = va === null || va === undefined ? '(empty)' : String(va);
|
|
110
|
+
const sb = vb === null || vb === undefined ? '(empty)' : String(vb);
|
|
111
|
+
const la = sa.toLowerCase();
|
|
112
|
+
const lb = sb.toLowerCase();
|
|
113
|
+
if (la < lb) return -1 * dir;
|
|
114
|
+
if (la > lb) return 1 * dir;
|
|
115
|
+
return a.i - b.i;
|
|
116
|
+
});
|
|
117
|
+
return withIndex.map((x) => x.v);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Virtual slice when options exceed threshold
|
|
121
|
+
const virtualSlice = $derived.by(() => {
|
|
122
|
+
if (!sortedValues || sortedValues.length <= virtualThreshold) return null;
|
|
123
|
+
const itemH = virtualItemHeight;
|
|
124
|
+
const height = containerHeight;
|
|
125
|
+
const overscan = virtualOverscan;
|
|
126
|
+
const start = Math.max(0, Math.floor(scrollTop / itemH) - overscan);
|
|
127
|
+
const visibleCount = Math.ceil(height / itemH) + overscan * 2;
|
|
128
|
+
const end = Math.min(sortedValues.length, start + visibleCount);
|
|
129
|
+
return {
|
|
130
|
+
slice: sortedValues.slice(start, end),
|
|
131
|
+
top: start * itemH,
|
|
132
|
+
totalHeight: sortedValues.length * itemH,
|
|
133
|
+
};
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
function handleSortClick(mode) {
|
|
137
|
+
if (sortMode === mode) {
|
|
138
|
+
sortDir = sortDir === 'asc' ? 'desc' : 'asc';
|
|
139
|
+
} else {
|
|
140
|
+
sortMode = mode;
|
|
141
|
+
sortDir = 'desc';
|
|
142
|
+
}
|
|
143
|
+
if (onsortchange) onsortchange({ mode: sortMode, dir: sortDir });
|
|
144
|
+
}
|
|
145
|
+
</script>
|
|
146
|
+
|
|
147
|
+
<!-- Search bar + sort buttons -->
|
|
148
|
+
<div class="px-1 py-1 border-b border-gray-200 dark:border-gray-600">
|
|
149
|
+
<div class="flex items-center gap-1 w-full min-w-0">
|
|
150
|
+
<input
|
|
151
|
+
type="text"
|
|
152
|
+
placeholder="Filter..."
|
|
153
|
+
class="p-1 flex-1 min-w-0 px-2 py-1 text-xs border border-gray-200 dark:border-gray-600 rounded bg-white dark:bg-gray-700 text-gray-700 dark:text-gray-200 focus:outline-none focus:border-blue-500"
|
|
154
|
+
bind:value={searchQuery}
|
|
155
|
+
/>
|
|
156
|
+
<div class="flex items-center gap-1">
|
|
157
|
+
<!-- Sort by name -->
|
|
158
|
+
<button
|
|
159
|
+
class="p-0.5 flex-1 flex items-center justify-center gap-1 text-sm text-gray-600 dark:text-gray-400 bg-transparent border border-gray-200 dark:border-gray-700 rounded hover:bg-gray-100 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300 transition-all"
|
|
160
|
+
class:bg-blue-100={sortMode === 'name'}
|
|
161
|
+
class:dark:bg-blue-900={sortMode === 'name'}
|
|
162
|
+
onclick={() => handleSortClick('name')}
|
|
163
|
+
title="Sort by name"
|
|
164
|
+
type="button"
|
|
165
|
+
>
|
|
166
|
+
<span
|
|
167
|
+
class="flex flex-col items-center leading-none font-semibold"
|
|
168
|
+
style="font-size:0.6rem;"
|
|
169
|
+
>
|
|
170
|
+
<span>A</span>
|
|
171
|
+
<span>Z</span>
|
|
172
|
+
</span>
|
|
173
|
+
<ArrowDownOutline
|
|
174
|
+
class={'w-3 h-3 transition-transform ' +
|
|
175
|
+
(sortMode === 'name'
|
|
176
|
+
? 'text-blue-900 dark:text-blue-100'
|
|
177
|
+
: 'text-gray-400 dark:text-gray-500')}
|
|
178
|
+
style={sortMode === 'name' && sortDir === 'asc' ? 'transform: rotate(180deg);' : ''}
|
|
179
|
+
aria-hidden="true"
|
|
180
|
+
/>
|
|
181
|
+
</button>
|
|
182
|
+
<!-- Sort by number -->
|
|
183
|
+
<button
|
|
184
|
+
class="p-0.5 flex-1 flex items-center justify-center gap-1 text-sm text-gray-600 dark:text-gray-400 bg-transparent border border-gray-200 dark:border-gray-700 rounded hover:bg-gray-100 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300 transition-all"
|
|
185
|
+
class:bg-blue-100={sortMode === 'number'}
|
|
186
|
+
class:dark:bg-blue-900={sortMode === 'number'}
|
|
187
|
+
onclick={() => handleSortClick('number')}
|
|
188
|
+
title="Sort by number"
|
|
189
|
+
aria-label="Sort by number"
|
|
190
|
+
type="button"
|
|
191
|
+
>
|
|
192
|
+
<span
|
|
193
|
+
class="flex flex-col items-center leading-none font-semibold"
|
|
194
|
+
style="font-size:0.6rem;"
|
|
195
|
+
>
|
|
196
|
+
<span>0</span>
|
|
197
|
+
<span>9</span>
|
|
198
|
+
</span>
|
|
199
|
+
<ArrowDownOutline
|
|
200
|
+
class={'w-3 h-3 transition-transform ' +
|
|
201
|
+
(sortMode === 'number'
|
|
202
|
+
? 'text-blue-900 dark:text-blue-100'
|
|
203
|
+
: 'text-gray-400 dark:text-gray-500')}
|
|
204
|
+
style={sortMode === 'number' && sortDir === 'asc' ? 'transform: rotate(180deg);' : ''}
|
|
205
|
+
aria-hidden="true"
|
|
206
|
+
/>
|
|
207
|
+
</button>
|
|
208
|
+
<!-- Sort by count (only when counts are available) -->
|
|
209
|
+
{#if showCounts && column.counts}
|
|
210
|
+
<button
|
|
211
|
+
class="p-0.5 flex-1 flex items-center justify-center gap-1 text-sm text-gray-600 dark:text-gray-400 bg-transparent border border-gray-200 dark:border-gray-700 rounded hover:bg-gray-100 dark:hover:bg-gray-700 hover:border-gray-300 dark:hover:border-gray-600 hover:text-gray-700 dark:hover:text-gray-300 transition-all"
|
|
212
|
+
class:bg-blue-100={sortMode === 'count'}
|
|
213
|
+
class:dark:bg-blue-900={sortMode === 'count'}
|
|
214
|
+
onclick={() => handleSortClick('count')}
|
|
215
|
+
title="Sort by count"
|
|
216
|
+
type="button"
|
|
217
|
+
>
|
|
218
|
+
<span class="text-sm font-semibold">#</span>
|
|
219
|
+
<ArrowDownOutline
|
|
220
|
+
class={'w-3 h-3 transition-transform ' +
|
|
221
|
+
(sortMode === 'count'
|
|
222
|
+
? 'text-blue-900 dark:text-blue-100'
|
|
223
|
+
: 'text-gray-400 dark:text-gray-500')}
|
|
224
|
+
style={sortMode === 'count' && sortDir === 'asc' ? 'transform: rotate(180deg);' : ''}
|
|
225
|
+
aria-hidden="true"
|
|
226
|
+
/>
|
|
227
|
+
</button>
|
|
228
|
+
{/if}
|
|
229
|
+
</div>
|
|
230
|
+
</div>
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
<!-- Check-all toolbar -->
|
|
234
|
+
<div class="px-2 py-1 border-b border-gray-100 dark:border-gray-600">
|
|
235
|
+
<div class="flex items-center justify-between">
|
|
236
|
+
<div class="flex items-center gap-1">
|
|
237
|
+
<button
|
|
238
|
+
type="button"
|
|
239
|
+
class="p-1 rounded-md flex items-center gap-1 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
240
|
+
onclick={() => oncheckall && oncheckall(sortedValues)}
|
|
241
|
+
disabled={sortedValues.length === 0}
|
|
242
|
+
aria-label="Check all"
|
|
243
|
+
title="Check all"
|
|
244
|
+
>
|
|
245
|
+
<svg
|
|
246
|
+
class="w-4 h-4 text-gray-800 dark:text-white"
|
|
247
|
+
viewBox="0 0 24 24"
|
|
248
|
+
fill="none"
|
|
249
|
+
aria-hidden="true"
|
|
250
|
+
>
|
|
251
|
+
<rect
|
|
252
|
+
x="3"
|
|
253
|
+
y="3"
|
|
254
|
+
width="18"
|
|
255
|
+
height="18"
|
|
256
|
+
rx="2"
|
|
257
|
+
stroke="currentColor"
|
|
258
|
+
stroke-width="2.5"
|
|
259
|
+
fill="none"
|
|
260
|
+
/>
|
|
261
|
+
<path
|
|
262
|
+
d="M7 13l3 3 7-7"
|
|
263
|
+
stroke="currentColor"
|
|
264
|
+
stroke-width="2"
|
|
265
|
+
stroke-linecap="round"
|
|
266
|
+
stroke-linejoin="round"
|
|
267
|
+
fill="none"
|
|
268
|
+
/>
|
|
269
|
+
</svg>
|
|
270
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">All</span>
|
|
271
|
+
</button>
|
|
272
|
+
<button
|
|
273
|
+
type="button"
|
|
274
|
+
class="p-1 rounded-md flex items-center gap-1 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
275
|
+
onclick={() => oninvert && oninvert(sortedValues)}
|
|
276
|
+
disabled={sortedValues.length === 0}
|
|
277
|
+
aria-label="Invert selection"
|
|
278
|
+
title="Invert selection"
|
|
279
|
+
>
|
|
280
|
+
<svg
|
|
281
|
+
class="w-4 h-4 text-gray-800 dark:text-white"
|
|
282
|
+
viewBox="0 0 24 24"
|
|
283
|
+
fill="none"
|
|
284
|
+
aria-hidden="true"
|
|
285
|
+
>
|
|
286
|
+
<rect
|
|
287
|
+
x="3"
|
|
288
|
+
y="3"
|
|
289
|
+
width="18"
|
|
290
|
+
height="18"
|
|
291
|
+
rx="2"
|
|
292
|
+
stroke="currentColor"
|
|
293
|
+
stroke-width="2.5"
|
|
294
|
+
fill="none"
|
|
295
|
+
/>
|
|
296
|
+
<rect
|
|
297
|
+
x="4"
|
|
298
|
+
y="4"
|
|
299
|
+
width="8.5"
|
|
300
|
+
height="16"
|
|
301
|
+
fill="currentColor"
|
|
302
|
+
opacity="0.28"
|
|
303
|
+
stroke="none"
|
|
304
|
+
/>
|
|
305
|
+
<path
|
|
306
|
+
d="M7 13l3 3 7-7"
|
|
307
|
+
stroke="currentColor"
|
|
308
|
+
stroke-width="2"
|
|
309
|
+
stroke-linecap="round"
|
|
310
|
+
stroke-linejoin="round"
|
|
311
|
+
fill="none"
|
|
312
|
+
/>
|
|
313
|
+
</svg>
|
|
314
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">Invert</span>
|
|
315
|
+
</button>
|
|
316
|
+
<button
|
|
317
|
+
type="button"
|
|
318
|
+
class="p-1 rounded-md flex items-center gap-1 text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
319
|
+
onclick={() => onchecknone && onchecknone()}
|
|
320
|
+
disabled={sortedValues.length === 0}
|
|
321
|
+
aria-label="Uncheck all"
|
|
322
|
+
title="Uncheck all"
|
|
323
|
+
>
|
|
324
|
+
<svg
|
|
325
|
+
class="w-4 h-4 text-gray-800 dark:text-white"
|
|
326
|
+
viewBox="0 0 24 24"
|
|
327
|
+
fill="none"
|
|
328
|
+
aria-hidden="true"
|
|
329
|
+
>
|
|
330
|
+
<rect
|
|
331
|
+
x="3"
|
|
332
|
+
y="3"
|
|
333
|
+
width="18"
|
|
334
|
+
height="18"
|
|
335
|
+
rx="2"
|
|
336
|
+
stroke="currentColor"
|
|
337
|
+
stroke-width="2"
|
|
338
|
+
fill="none"
|
|
339
|
+
/>
|
|
340
|
+
</svg>
|
|
341
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">None</span>
|
|
342
|
+
</button>
|
|
343
|
+
</div>
|
|
344
|
+
<div class="pr-1">
|
|
345
|
+
<Badge color="blue" rounded title="Total options">{sortedValues.length}</Badge>
|
|
346
|
+
</div>
|
|
347
|
+
</div>
|
|
348
|
+
</div>
|
|
349
|
+
|
|
350
|
+
<!-- Checkbox list -->
|
|
351
|
+
<div class="p-1 overflow-y-auto flex-1 max-h-48" bind:this={listRef} onscroll={handleScroll}>
|
|
352
|
+
{#if sortedValues.length > populateThreshold && !loaded}
|
|
353
|
+
<div class="p-1 text-sm text-gray-700 dark:text-gray-200">
|
|
354
|
+
Too many options ({sortedValues.length}) to render by default.
|
|
355
|
+
<button
|
|
356
|
+
class="ml-2 px-2 py-1 text-xs rounded bg-gray-100 dark:bg-gray-600 text-gray-800 dark:text-white border border-gray-200 hover:bg-gray-200 dark:hover:bg-gray-500"
|
|
357
|
+
type="button"
|
|
358
|
+
onclick={() => (manuallyLoaded = true)}
|
|
359
|
+
>
|
|
360
|
+
Load options
|
|
361
|
+
</button>
|
|
362
|
+
</div>
|
|
363
|
+
{:else if virtualSlice}
|
|
364
|
+
<div style="height:{virtualSlice.totalHeight}px; position:relative;">
|
|
365
|
+
<div style="position:absolute; top:{virtualSlice.top}px; left:0; right:0;">
|
|
366
|
+
{#each virtualSlice.slice as value, idx (idx)}
|
|
367
|
+
{@const isSelected = selections?.includes(value)}
|
|
368
|
+
{@const displayValue = value === null || value === undefined ? '(empty)' : String(value)}
|
|
369
|
+
<label
|
|
370
|
+
class="flex items-center gap-1 px-2 py-1 cursor-pointer rounded-sm hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors"
|
|
371
|
+
style="height:{virtualItemHeight}px; box-sizing:border-box;"
|
|
372
|
+
>
|
|
373
|
+
<input
|
|
374
|
+
type="checkbox"
|
|
375
|
+
checked={isSelected}
|
|
376
|
+
onchange={() => ontoggle && ontoggle(value)}
|
|
377
|
+
class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
|
|
378
|
+
/>
|
|
379
|
+
<span
|
|
380
|
+
class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
|
|
381
|
+
>{displayValue}</span
|
|
382
|
+
>
|
|
383
|
+
{#if showCounts && column.counts && column.counts[value] !== undefined}
|
|
384
|
+
<span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
|
|
385
|
+
>({column.counts[value]})</span
|
|
386
|
+
>
|
|
387
|
+
{/if}
|
|
388
|
+
</label>
|
|
389
|
+
{/each}
|
|
390
|
+
</div>
|
|
391
|
+
</div>
|
|
392
|
+
{:else if sortedValues.length > 0}
|
|
393
|
+
{#each sortedValues as value, idx (idx)}
|
|
394
|
+
{@const isSelected = selections?.includes(value)}
|
|
395
|
+
{@const displayValue = value === null || value === undefined ? '(empty)' : String(value)}
|
|
396
|
+
<label
|
|
397
|
+
class="flex items-center gap-1 px-2 py-1 cursor-pointer rounded-sm hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors"
|
|
398
|
+
>
|
|
399
|
+
<input
|
|
400
|
+
type="checkbox"
|
|
401
|
+
checked={isSelected}
|
|
402
|
+
onchange={() => ontoggle && ontoggle(value)}
|
|
403
|
+
class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
|
|
404
|
+
/>
|
|
405
|
+
<span
|
|
406
|
+
class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
|
|
407
|
+
>{displayValue}</span
|
|
408
|
+
>
|
|
409
|
+
{#if showCounts && column.counts && column.counts[value] !== undefined}
|
|
410
|
+
<span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
|
|
411
|
+
>({column.counts[value]})</span
|
|
412
|
+
>
|
|
413
|
+
{/if}
|
|
414
|
+
</label>
|
|
415
|
+
{/each}
|
|
416
|
+
{:else}
|
|
417
|
+
<div class="p-4 text-center text-sm text-gray-400 dark:text-gray-500">No values available</div>
|
|
418
|
+
{/if}
|
|
419
|
+
</div>
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { onDestroy } from 'svelte';
|
|
2
|
+
|
|
3
|
+
function isDateRangeColumn(col) {
|
|
4
|
+
return col.type === 'daterange' || col.type === 'datetimerange';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Svelte 5 reactive composable that manages per-column filter selections,
|
|
9
|
+
* debounced emission, and selection helpers.
|
|
10
|
+
*
|
|
11
|
+
* @param {object} opts
|
|
12
|
+
* @param {() => Array} opts.columnFilters - reactive getter for column filter configs
|
|
13
|
+
* @param {() => object} opts.activeFilters - reactive getter for active filters
|
|
14
|
+
* @param {() => number} opts.emitDebounce - reactive getter for debounce ms
|
|
15
|
+
* @param {() => Function|undefined} opts.onfilter - reactive getter for onfilter callback
|
|
16
|
+
*/
|
|
17
|
+
export function useFilterState(opts) {
|
|
18
|
+
let selections = $state({});
|
|
19
|
+
let _emitTimer = null;
|
|
20
|
+
let _pendingEmit = null;
|
|
21
|
+
|
|
22
|
+
// Sync selections from activeFilters whenever columnFilters or activeFilters change
|
|
23
|
+
$effect(() => {
|
|
24
|
+
const cols = opts.columnFilters();
|
|
25
|
+
const active = opts.activeFilters();
|
|
26
|
+
const newSelections = {};
|
|
27
|
+
for (const col of cols) {
|
|
28
|
+
newSelections[col.key] = active[col.key] ?? (isDateRangeColumn(col) ? {} : []);
|
|
29
|
+
}
|
|
30
|
+
selections = newSelections;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
function scheduleEmit(columnKey, selectedValues) {
|
|
34
|
+
_pendingEmit = { columnKey, selectedValues, allFilters: selections };
|
|
35
|
+
const delay = opts.emitDebounce();
|
|
36
|
+
if (!delay || delay <= 0) {
|
|
37
|
+
flushEmit();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (_emitTimer) clearTimeout(_emitTimer);
|
|
41
|
+
_emitTimer = setTimeout(() => flushEmit(), delay);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function flushEmit() {
|
|
45
|
+
if (!_pendingEmit) return;
|
|
46
|
+
const payload = _pendingEmit;
|
|
47
|
+
_pendingEmit = null;
|
|
48
|
+
if (_emitTimer) {
|
|
49
|
+
clearTimeout(_emitTimer);
|
|
50
|
+
_emitTimer = null;
|
|
51
|
+
}
|
|
52
|
+
const handler = opts.onfilter();
|
|
53
|
+
if (handler !== undefined) {
|
|
54
|
+
try {
|
|
55
|
+
handler({
|
|
56
|
+
key: payload.columnKey,
|
|
57
|
+
values: payload.selectedValues,
|
|
58
|
+
allFilters: payload.allFilters,
|
|
59
|
+
});
|
|
60
|
+
} catch (err) {
|
|
61
|
+
try {
|
|
62
|
+
console.error('onfilter threw:', err);
|
|
63
|
+
} catch (e) {}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
onDestroy(() => {
|
|
69
|
+
if (_emitTimer) clearTimeout(_emitTimer);
|
|
70
|
+
flushEmit();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
function toggleSelection(columnKey, value) {
|
|
74
|
+
const current = selections[columnKey] || [];
|
|
75
|
+
const updated = current.includes(value)
|
|
76
|
+
? current.filter((v) => v !== value)
|
|
77
|
+
: [...current, value];
|
|
78
|
+
selections = { ...selections, [columnKey]: updated };
|
|
79
|
+
scheduleEmit(columnKey, updated);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function clearColumn(columnKey) {
|
|
83
|
+
const col = opts.columnFilters().find((c) => c.key === columnKey);
|
|
84
|
+
const empty = isDateRangeColumn(col) ? {} : [];
|
|
85
|
+
selections = { ...selections, [columnKey]: empty };
|
|
86
|
+
scheduleEmit(columnKey, empty);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function clearAllFilters() {
|
|
90
|
+
const cleared = {};
|
|
91
|
+
for (const col of opts.columnFilters()) {
|
|
92
|
+
cleared[col.key] = isDateRangeColumn(col) ? {} : [];
|
|
93
|
+
}
|
|
94
|
+
selections = cleared;
|
|
95
|
+
scheduleEmit(null, []);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function checkAll(columnKey, values) {
|
|
99
|
+
const all = Array.isArray(values) ? values.slice() : [];
|
|
100
|
+
selections = { ...selections, [columnKey]: all };
|
|
101
|
+
scheduleEmit(columnKey, all);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function invertSelection(columnKey, values) {
|
|
105
|
+
const current = new Set(selections[columnKey] || []);
|
|
106
|
+
const next = (Array.isArray(values) ? values : []).filter((v) => !current.has(v));
|
|
107
|
+
selections = { ...selections, [columnKey]: next };
|
|
108
|
+
scheduleEmit(columnKey, next);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function checkNone(columnKey) {
|
|
112
|
+
clearColumn(columnKey);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function updateDateRange(columnKey, updated) {
|
|
116
|
+
selections = { ...selections, [columnKey]: updated };
|
|
117
|
+
scheduleEmit(columnKey, updated);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
get selections() {
|
|
122
|
+
return selections;
|
|
123
|
+
},
|
|
124
|
+
toggleSelection,
|
|
125
|
+
clearColumn,
|
|
126
|
+
clearAllFilters,
|
|
127
|
+
checkAll,
|
|
128
|
+
invertSelection,
|
|
129
|
+
checkNone,
|
|
130
|
+
updateDateRange,
|
|
131
|
+
};
|
|
132
|
+
}
|
package/src/lib/dist/styles.css
CHANGED
|
@@ -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;
|
|
@@ -220,11 +234,11 @@
|
|
|
220
234
|
color: #fff;
|
|
221
235
|
}
|
|
222
236
|
|
|
223
|
-
/* Force the active/current page to use the
|
|
237
|
+
/* Force the active/current page to use the accent tokens (override flowbite's primary/orange).
|
|
224
238
|
Intentionally specific to reliably win over third-party rules. */
|
|
225
239
|
[data-pagination-id] ul li [aria-current='page'] {
|
|
226
|
-
background-color:
|
|
227
|
-
color:
|
|
240
|
+
background-color: var(--vtable-color-accent-light) !important;
|
|
241
|
+
color: var(--vtable-color-accent) !important;
|
|
228
242
|
border-color: transparent !important;
|
|
229
243
|
}
|
|
230
244
|
|