@zhangt58/svelte-vtable 0.0.3 → 0.1.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 +159 -7
- package/package.json +10 -6
- package/src/components/DataTableControls.svelte +121 -38
- package/src/components/DataTableFilters.svelte +650 -0
- package/src/components/FiltersModal.svelte +40 -0
- package/src/lib/dist/styles.css +4 -4
- package/src/lib/filterUtils.js +145 -0
- package/src/lib/index.js +13 -1
- package/src/lib/styles.css +4 -4
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { Badge } from 'flowbite-svelte';
|
|
3
|
+
import { ChevronDownOutline, ArrowDownOutline } from 'flowbite-svelte-icons';
|
|
4
|
+
import { onDestroy } from 'svelte';
|
|
5
|
+
|
|
6
|
+
// shared default no-op function
|
|
7
|
+
const DEFAULT_NOOP = (..._args) => {};
|
|
8
|
+
|
|
9
|
+
// Props using Svelte 5 $props()
|
|
10
|
+
let {
|
|
11
|
+
// columnFilters: array of { key, label, uniqueValues }
|
|
12
|
+
// where uniqueValues is an array of unique values for that column
|
|
13
|
+
columnFilters = [],
|
|
14
|
+
// layout direction: 'horizontal' or 'vertical'
|
|
15
|
+
direction = 'horizontal',
|
|
16
|
+
// current active filters: { columnKey: [selectedValues] }
|
|
17
|
+
activeFilters = {},
|
|
18
|
+
// Callback when filters change
|
|
19
|
+
filterChange = DEFAULT_NOOP,
|
|
20
|
+
// Optional callback when sort mode/dir changes: ( { columnKey, mode, dir } )
|
|
21
|
+
sortChange = DEFAULT_NOOP,
|
|
22
|
+
// CSS class for the container
|
|
23
|
+
className = '',
|
|
24
|
+
// Whether to show filter counts
|
|
25
|
+
showCounts = true,
|
|
26
|
+
// Virtualization props (defaults)
|
|
27
|
+
virtualThreshold = 500,
|
|
28
|
+
virtualItemHeight = 36,
|
|
29
|
+
virtualOverscan = 5,
|
|
30
|
+
// Debounce milliseconds for emitting filterChange (0 = immediate)
|
|
31
|
+
emitDebounce = 100,
|
|
32
|
+
// If the number of options exceeds this threshold, the options are not generated by default
|
|
33
|
+
// and the user must click "Load options" to populate them. Set to 0 to always populate.
|
|
34
|
+
populateThreshold = 200,
|
|
35
|
+
// If true, render dropdowns inline (in-flow) so opening them expands the parent container.
|
|
36
|
+
// If false, render as absolutely-positioned overlays (old behavior).
|
|
37
|
+
dropdownInline = true
|
|
38
|
+
} = $props();
|
|
39
|
+
|
|
40
|
+
// Internal state for selected options per column
|
|
41
|
+
let selections = $state({});
|
|
42
|
+
|
|
43
|
+
// Track whether options have been loaded (populated) per-column when the list is large
|
|
44
|
+
let loadedOptions = $state({});
|
|
45
|
+
|
|
46
|
+
// Auto-enable loading for small lists when columnFilters change
|
|
47
|
+
$effect(() => {
|
|
48
|
+
const initial = {};
|
|
49
|
+
for (const col of columnFilters) {
|
|
50
|
+
const len = Array.isArray(col.uniqueValues) ? col.uniqueValues.length : 0;
|
|
51
|
+
initial[col.key] = (populateThreshold <= 0) || len <= populateThreshold;
|
|
52
|
+
}
|
|
53
|
+
loadedOptions = initial;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// --- Debounced emission helpers ---
|
|
57
|
+
let _emitTimer = null;
|
|
58
|
+
let _pendingEmit = null;
|
|
59
|
+
|
|
60
|
+
function scheduleEmit(columnKey, selectedValues) {
|
|
61
|
+
// capture latest state snapshot
|
|
62
|
+
_pendingEmit = { columnKey, selectedValues, allFilters: selections };
|
|
63
|
+
|
|
64
|
+
if (!emitDebounce || emitDebounce <= 0) {
|
|
65
|
+
// immediate
|
|
66
|
+
flushEmit();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (_emitTimer) clearTimeout(_emitTimer);
|
|
71
|
+
_emitTimer = setTimeout(() => flushEmit(), emitDebounce);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function flushEmit() {
|
|
75
|
+
if (!_pendingEmit) return;
|
|
76
|
+
const payload = _pendingEmit;
|
|
77
|
+
_pendingEmit = null;
|
|
78
|
+
if (_emitTimer) {
|
|
79
|
+
clearTimeout(_emitTimer);
|
|
80
|
+
_emitTimer = null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
(/** @type {any} */ (filterChange))(payload);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
try { console.error('filterChange threw:', err); } catch (e) {}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// cleanup on destroy to avoid timers running after unmount
|
|
91
|
+
onDestroy(() => {
|
|
92
|
+
if (_emitTimer) clearTimeout(_emitTimer);
|
|
93
|
+
// flush any pending emit synchronously
|
|
94
|
+
flushEmit();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Initialize selections from activeFilters
|
|
98
|
+
$effect(() => {
|
|
99
|
+
const newSelections = {};
|
|
100
|
+
for (const col of columnFilters) {
|
|
101
|
+
newSelections[col.key] = activeFilters[col.key] || [];
|
|
102
|
+
}
|
|
103
|
+
selections = newSelections;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Toggle selection for a specific column and value
|
|
107
|
+
function toggleSelection(columnKey, value) {
|
|
108
|
+
const current = selections[columnKey] || [];
|
|
109
|
+
let updated;
|
|
110
|
+
|
|
111
|
+
if (current.includes(value)) {
|
|
112
|
+
// Remove the value
|
|
113
|
+
updated = current.filter(v => v !== value);
|
|
114
|
+
} else {
|
|
115
|
+
// Add the value
|
|
116
|
+
updated = [...current, value];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
selections = { ...selections, [columnKey]: updated };
|
|
120
|
+
|
|
121
|
+
// Schedule debounced filter change emission
|
|
122
|
+
scheduleEmit(columnKey, updated);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Clear all selections for a column
|
|
126
|
+
function clearColumn(columnKey) {
|
|
127
|
+
selections = { ...selections, [columnKey]: [] };
|
|
128
|
+
scheduleEmit(columnKey, []);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Clear all filters
|
|
132
|
+
function clearAllFilters() {
|
|
133
|
+
const cleared = {};
|
|
134
|
+
for (const col of columnFilters) {
|
|
135
|
+
cleared[col.key] = [];
|
|
136
|
+
}
|
|
137
|
+
selections = cleared;
|
|
138
|
+
|
|
139
|
+
// emit after debounce (columnKey null indicates global clear)
|
|
140
|
+
scheduleEmit(null, []);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Check if any filters are active
|
|
144
|
+
const hasActiveFilters = $derived(() => {
|
|
145
|
+
return Object.values(selections).some(arr => arr && arr.length > 0);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Count active filters
|
|
149
|
+
const activeFilterCount = $derived(() => {
|
|
150
|
+
return Object.values(selections).reduce((sum, arr) => sum + (arr?.length || 0), 0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Toggle dropdown visibility for a column
|
|
154
|
+
let openDropdowns = $state({});
|
|
155
|
+
|
|
156
|
+
// Simple toggle/close functions — dropdowns are controlled only by their
|
|
157
|
+
// toggle button and programmatic calls (e.g. Show All / Collapse All).
|
|
158
|
+
function toggleDropdown(columnKey) {
|
|
159
|
+
const wasOpen = openDropdowns[columnKey];
|
|
160
|
+
openDropdowns = { ...openDropdowns, [columnKey]: !wasOpen };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Sorting state for each column's dropdown
|
|
164
|
+
let sortModes = $state({});
|
|
165
|
+
// Per-column sort direction: 'asc' or 'desc'
|
|
166
|
+
let sortDirs = $state({});
|
|
167
|
+
|
|
168
|
+
// Inline header search queries per column (small text filter for options)
|
|
169
|
+
let searchQueries = $state({});
|
|
170
|
+
|
|
171
|
+
// --- Virtualization settings and state ---
|
|
172
|
+
// (virtual props are provided via the main $props() destructure above)
|
|
173
|
+
|
|
174
|
+
// DOM refs for per-column scroll containers
|
|
175
|
+
let listRefs = $state({});
|
|
176
|
+
|
|
177
|
+
// Per-column scroll state: { scrollTop, height }
|
|
178
|
+
let virtualScroll = $state({});
|
|
179
|
+
|
|
180
|
+
// Svelte action to register list containers and keep virtualScroll updated
|
|
181
|
+
function registerList(node, params) {
|
|
182
|
+
const key = params && params.key;
|
|
183
|
+
if (!key) return { destroy() {} };
|
|
184
|
+
|
|
185
|
+
listRefs = { ...listRefs, [key]: node };
|
|
186
|
+
|
|
187
|
+
function updateState() {
|
|
188
|
+
virtualScroll = {
|
|
189
|
+
...virtualScroll,
|
|
190
|
+
[key]: { scrollTop: node.scrollTop, height: node.clientHeight }
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// initialize
|
|
195
|
+
updateState();
|
|
196
|
+
|
|
197
|
+
const onScroll = () => updateState();
|
|
198
|
+
const onResize = () => updateState();
|
|
199
|
+
|
|
200
|
+
node.addEventListener('scroll', onScroll);
|
|
201
|
+
// Observe size changes (in case container height changes)
|
|
202
|
+
const ro = new ResizeObserver(onResize);
|
|
203
|
+
ro.observe(node);
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
update(newParams) {
|
|
207
|
+
// allow key to be updated (unlikely)
|
|
208
|
+
if (!newParams || newParams.key !== key) {
|
|
209
|
+
node.removeEventListener('scroll', onScroll);
|
|
210
|
+
ro.disconnect();
|
|
211
|
+
delete listRefs[key];
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
destroy() {
|
|
215
|
+
node.removeEventListener('scroll', onScroll);
|
|
216
|
+
ro.disconnect();
|
|
217
|
+
delete listRefs[key];
|
|
218
|
+
// remove virtualScroll entry
|
|
219
|
+
const copy = { ...virtualScroll };
|
|
220
|
+
delete copy[key];
|
|
221
|
+
virtualScroll = copy;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Compute virtual slice for a column when the values exceed threshold
|
|
227
|
+
function getVirtualSlice(column, values) {
|
|
228
|
+
if (!values || values.length <= virtualThreshold) return null;
|
|
229
|
+
const vs = virtualScroll[column.key] || { scrollTop: 0, height: 200 };
|
|
230
|
+
const itemH = virtualItemHeight;
|
|
231
|
+
const height = vs.height || 200;
|
|
232
|
+
const overscan = virtualOverscan;
|
|
233
|
+
|
|
234
|
+
const start = Math.max(0, Math.floor(vs.scrollTop / itemH) - overscan);
|
|
235
|
+
const visibleCount = Math.ceil(height / itemH) + overscan * 2;
|
|
236
|
+
const end = Math.min(values.length, start + visibleCount);
|
|
237
|
+
const slice = values.slice(start, end);
|
|
238
|
+
const top = start * itemH;
|
|
239
|
+
const totalHeight = values.length * itemH;
|
|
240
|
+
|
|
241
|
+
return { slice, start, end, top, totalHeight };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// --- Helper: return the values for a column after applying search + sort ---
|
|
245
|
+
function getSortedValues(column) {
|
|
246
|
+
// column.uniqueValues is expected to be an array of possible values
|
|
247
|
+
const raw = Array.isArray(column.uniqueValues) ? column.uniqueValues.slice() : [];
|
|
248
|
+
|
|
249
|
+
// apply search filter if present
|
|
250
|
+
const q = (searchQueries[column.key] || '').toString().trim().toLowerCase();
|
|
251
|
+
let filtered = raw;
|
|
252
|
+
if (q) {
|
|
253
|
+
filtered = raw.filter(v => {
|
|
254
|
+
const text = v === null || v === undefined ? '(empty)' : String(v);
|
|
255
|
+
return text.toLowerCase().includes(q);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// determine sort mode and direction
|
|
260
|
+
const mode = sortModes[column.key] || 'name';
|
|
261
|
+
const dir = (sortDirs[column.key] || 'asc') === 'asc' ? 1 : -1;
|
|
262
|
+
|
|
263
|
+
// stable sort: map-index technique
|
|
264
|
+
const withIndex = filtered.map((v, i) => ({ v, i }));
|
|
265
|
+
|
|
266
|
+
withIndex.sort((a, b) => {
|
|
267
|
+
const va = a.v;
|
|
268
|
+
const vb = b.v;
|
|
269
|
+
|
|
270
|
+
if (mode === 'count' && column.counts) {
|
|
271
|
+
const ca = Number(column.counts[va] ?? 0);
|
|
272
|
+
const cb = Number(column.counts[vb] ?? 0);
|
|
273
|
+
if (ca !== cb) return (ca - cb) * dir * -1; // higher counts first when desc
|
|
274
|
+
// fallback to name compare
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// compare by name
|
|
278
|
+
const sa = va === null || va === undefined ? '(empty)' : String(va);
|
|
279
|
+
const sb = vb === null || vb === undefined ? '(empty)' : String(vb);
|
|
280
|
+
const la = sa.toLowerCase();
|
|
281
|
+
const lb = sb.toLowerCase();
|
|
282
|
+
if (la < lb) return -1 * dir;
|
|
283
|
+
if (la > lb) return 1 * dir;
|
|
284
|
+
// maintain stable order
|
|
285
|
+
return a.i - b.i;
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
return withIndex.map(x => x.v);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Toggle sort mode for a column. If the same mode is clicked again,
|
|
292
|
+
// flip the direction; otherwise set the mode and default to 'desc'.
|
|
293
|
+
function toggleSortMode(columnKey, mode) {
|
|
294
|
+
const prevMode = sortModes[columnKey];
|
|
295
|
+
if (prevMode === mode) {
|
|
296
|
+
// flip direction
|
|
297
|
+
const prevDir = sortDirs[columnKey] || 'desc';
|
|
298
|
+
const nextDir = prevDir === 'asc' ? 'desc' : 'asc';
|
|
299
|
+
sortDirs = { ...sortDirs, [columnKey]: nextDir };
|
|
300
|
+
} else {
|
|
301
|
+
sortModes = { ...sortModes, [columnKey]: mode };
|
|
302
|
+
// default first click to descending (common expectation for counts)
|
|
303
|
+
sortDirs = { ...sortDirs, [columnKey]: 'desc' };
|
|
304
|
+
}
|
|
305
|
+
// ensure the mode is set (for first-time clicks)
|
|
306
|
+
sortModes = { ...sortModes, [columnKey]: mode };
|
|
307
|
+
|
|
308
|
+
const newDir = sortDirs[columnKey] || 'asc';
|
|
309
|
+
try {
|
|
310
|
+
// notify parent if it wants to react to sort changes
|
|
311
|
+
(/** @type {any} */ (sortChange))({ columnKey, mode, dir: newDir });
|
|
312
|
+
} catch (err) {
|
|
313
|
+
try { console.debug('sortChange callback threw', err); } catch (e) {}
|
|
314
|
+
}
|
|
315
|
+
// debug log
|
|
316
|
+
try { console.debug('toggleSortMode', columnKey, mode, sortDirs[columnKey]); } catch (e) {}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Clear selection for a column (from within dropdown)
|
|
320
|
+
function clearSelection(columnKey) {
|
|
321
|
+
clearColumn(columnKey);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Check all values for a column
|
|
325
|
+
function checkAll(columnKey, values) {
|
|
326
|
+
const all = Array.isArray(values) ? values.slice() : [];
|
|
327
|
+
selections = { ...selections, [columnKey]: all };
|
|
328
|
+
scheduleEmit(columnKey, all);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Invert selection for a column (values is the full option list)
|
|
332
|
+
function invertSelection(columnKey, values) {
|
|
333
|
+
const current = new Set(selections[columnKey] || []);
|
|
334
|
+
const next = [];
|
|
335
|
+
for (const v of (Array.isArray(values) ? values : [])) {
|
|
336
|
+
if (!current.has(v)) next.push(v);
|
|
337
|
+
}
|
|
338
|
+
selections = { ...selections, [columnKey]: next };
|
|
339
|
+
scheduleEmit(columnKey, next);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Uncheck all -> reuse clearColumn
|
|
343
|
+
function checkNone(columnKey) {
|
|
344
|
+
clearColumn(columnKey);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Show all dropdowns
|
|
348
|
+
function showAllDropdowns() {
|
|
349
|
+
const allOpen = {};
|
|
350
|
+
for (const col of columnFilters) {
|
|
351
|
+
allOpen[col.key] = true;
|
|
352
|
+
}
|
|
353
|
+
openDropdowns = allOpen;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Collapse all dropdowns
|
|
357
|
+
function collapseAllDropdowns() {
|
|
358
|
+
openDropdowns = {};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Check if all dropdowns are open
|
|
362
|
+
const allDropdownsOpen = $derived.by(() => {
|
|
363
|
+
return columnFilters.every(col => openDropdowns[col.key]);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// Check if all dropdowns are closed
|
|
367
|
+
const allDropdownsClosed = $derived.by(() => {
|
|
368
|
+
return columnFilters.every(col => !openDropdowns[col.key]);
|
|
369
|
+
});
|
|
370
|
+
</script>
|
|
371
|
+
|
|
372
|
+
<div class="p-2 bg-gray-50 rounded-md border border-gray-200 dark:bg-gray-800 dark:border-gray-700 {className}"
|
|
373
|
+
class:horizontal={direction === 'horizontal'} class:vertical={direction === 'vertical'}>
|
|
374
|
+
<div class="flex justify-between items-center mb-2 flex-wrap gap-1">
|
|
375
|
+
<!-- Left controls: Show All, Close All -->
|
|
376
|
+
<div class="flex items-center gap-1 flex-wrap">
|
|
377
|
+
<button
|
|
378
|
+
class="px-3 py-1 text-sm text-green-600 bg-transparent border border-green-600 rounded-md hover:bg-green-600 hover:text-white transition-all cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent disabled:hover:text-green-600"
|
|
379
|
+
disabled={allDropdownsOpen}
|
|
380
|
+
onclick={showAllDropdowns}
|
|
381
|
+
title="Show all filter dropdowns"
|
|
382
|
+
>
|
|
383
|
+
Show All
|
|
384
|
+
</button>
|
|
385
|
+
<button
|
|
386
|
+
class="px-3 py-1 text-sm text-gray-600 bg-transparent border border-gray-600 rounded-md hover:bg-gray-600 hover:text-white transition-all cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent disabled:hover:text-gray-600"
|
|
387
|
+
disabled={allDropdownsClosed}
|
|
388
|
+
onclick={collapseAllDropdowns}
|
|
389
|
+
title="Close all filter dropdowns"
|
|
390
|
+
>
|
|
391
|
+
Close All
|
|
392
|
+
</button>
|
|
393
|
+
</div>
|
|
394
|
+
|
|
395
|
+
<!-- Right controls: active badge and Clear All -->
|
|
396
|
+
<div class="flex items-center gap-2 flex-wrap">
|
|
397
|
+
{#if hasActiveFilters()}
|
|
398
|
+
<Badge color="blue" rounded>{activeFilterCount()} active</Badge>
|
|
399
|
+
<button
|
|
400
|
+
class="px-3 py-1 text-sm text-red-600 bg-transparent border border-red-600 rounded-md hover:bg-red-600 hover:text-white transition-all cursor-pointer"
|
|
401
|
+
onclick={clearAllFilters}
|
|
402
|
+
title="Clear all filters"
|
|
403
|
+
>
|
|
404
|
+
Clear All
|
|
405
|
+
</button>
|
|
406
|
+
{/if}
|
|
407
|
+
</div>
|
|
408
|
+
</div>
|
|
409
|
+
|
|
410
|
+
<div class="grid gap-2" class:grid-horizontal={direction === 'horizontal'} class:grid-vertical={direction === 'vertical'}>
|
|
411
|
+
{#each columnFilters as column (column.key)}
|
|
412
|
+
{@const isActive = selections[column.key]?.length > 0}
|
|
413
|
+
{@const isOpen = openDropdowns[column.key]}
|
|
414
|
+
{@const sortedValues = getSortedValues(column)}
|
|
415
|
+
{@const currentSortMode = sortModes[column.key] || 'name'}
|
|
416
|
+
{@const currentSortDir = sortDirs[column.key] || 'asc'}
|
|
417
|
+
|
|
418
|
+
<div class="flex flex-col min-w-0 relative">
|
|
419
|
+
<button
|
|
420
|
+
id="filter-{column.key}"
|
|
421
|
+
class="w-full flex justify-between items-center px-3 py-2 text-sm text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md hover:border-gray-400 dark:hover:border-gray-500 focus:outline-none focus:border-blue-500 dark:focus:border-blue-400 focus:ring-2 focus:ring-blue-100 dark:focus:ring-blue-900 transition-all cursor-pointer gap-2 {isActive ? 'border-green-500 dark:border-green-400 bg-green-50 dark:bg-green-900/20' : ''}"
|
|
422
|
+
onclick={() => toggleDropdown(column.key)}
|
|
423
|
+
type="button"
|
|
424
|
+
>
|
|
425
|
+
<span class="flex-1 text-left whitespace-nowrap overflow-hidden text-ellipsis min-w-0">
|
|
426
|
+
{column.label || column.key}
|
|
427
|
+
</span>
|
|
428
|
+
{#if isActive}
|
|
429
|
+
<Badge color="green" rounded class="ml-1">{selections[column.key].length}</Badge>
|
|
430
|
+
{/if}
|
|
431
|
+
<ChevronDownOutline class="w-5 h-5 shrink-0 transition-transform {isOpen ? 'rotate-180' : ''}" />
|
|
432
|
+
</button>
|
|
433
|
+
|
|
434
|
+
{#if isOpen}
|
|
435
|
+
<div
|
|
436
|
+
class={
|
|
437
|
+
(dropdownInline
|
|
438
|
+
? `mt-1 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-lg dark:shadow-gray-900 max-h-64 flex flex-col dropdown-menu`
|
|
439
|
+
: `absolute top-full mt-1 left-0 right-0 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-lg dark:shadow-gray-900 max-h-64 flex flex-col dropdown-menu`)
|
|
440
|
+
}
|
|
441
|
+
tabindex="-1"
|
|
442
|
+
data-column={column.key}
|
|
443
|
+
>
|
|
444
|
+
<div class="px-1 py-1 border-b border-gray-200 dark:border-gray-600">
|
|
445
|
+
<div class="flex items-center gap-1 w-full min-w-0">
|
|
446
|
+
<input
|
|
447
|
+
type="text"
|
|
448
|
+
placeholder="Filter..."
|
|
449
|
+
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"
|
|
450
|
+
bind:value={searchQueries[column.key]}
|
|
451
|
+
/>
|
|
452
|
+
|
|
453
|
+
<div class="flex items-center gap-1">
|
|
454
|
+
<button
|
|
455
|
+
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"
|
|
456
|
+
class:bg-blue-100={currentSortMode === 'name'}
|
|
457
|
+
class:dark:bg-blue-900={currentSortMode === 'name'}
|
|
458
|
+
onclick={() => toggleSortMode(column.key, 'name')}
|
|
459
|
+
title="Sort by name"
|
|
460
|
+
type="button"
|
|
461
|
+
>
|
|
462
|
+
<span class="text-sm font-semibold">Aa</span>
|
|
463
|
+
<ArrowDownOutline
|
|
464
|
+
class={"w-3 h-3 transition-transform " + (currentSortMode === 'name' ? 'text-blue-900 dark:text-blue-100' : 'text-gray-400 dark:text-gray-500')}
|
|
465
|
+
style={currentSortMode === 'name' && currentSortDir === 'asc' ? 'transform: rotate(180deg);' : ''}
|
|
466
|
+
aria-hidden="true"
|
|
467
|
+
/>
|
|
468
|
+
</button>
|
|
469
|
+
|
|
470
|
+
{#if showCounts && column.counts}
|
|
471
|
+
<button
|
|
472
|
+
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"
|
|
473
|
+
class:bg-blue-100={currentSortMode === 'count'}
|
|
474
|
+
class:dark:bg-blue-900={currentSortMode === 'count'}
|
|
475
|
+
onclick={() => toggleSortMode(column.key, 'count')}
|
|
476
|
+
title="Sort by count"
|
|
477
|
+
type="button"
|
|
478
|
+
>
|
|
479
|
+
<span class="text-sm font-semibold">#</span>
|
|
480
|
+
<ArrowDownOutline
|
|
481
|
+
class={"w-3 h-3 transition-transform " + (currentSortMode === 'count' ? 'text-blue-900 dark:text-blue-100' : 'text-gray-400 dark:text-gray-500')}
|
|
482
|
+
style={currentSortMode === 'count' && currentSortDir === 'asc' ? 'transform: rotate(180deg);' : ''}
|
|
483
|
+
aria-hidden="true"
|
|
484
|
+
/>
|
|
485
|
+
</button>
|
|
486
|
+
{/if}
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
</div>
|
|
490
|
+
|
|
491
|
+
</div>
|
|
492
|
+
</div>
|
|
493
|
+
|
|
494
|
+
<!-- Action row: Check all / Invert / Uncheck all (icon buttons) -->
|
|
495
|
+
<div class="px-2 py-1 border-b border-gray-100 dark:border-gray-600">
|
|
496
|
+
<div class="flex items-center justify-between">
|
|
497
|
+
<div class="flex items-center gap-1">
|
|
498
|
+
<button
|
|
499
|
+
type="button"
|
|
500
|
+
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"
|
|
501
|
+
onclick={() => checkAll(column.key, sortedValues)}
|
|
502
|
+
disabled={sortedValues.length === 0}
|
|
503
|
+
aria-label="Check all"
|
|
504
|
+
title="Check all"
|
|
505
|
+
>
|
|
506
|
+
<!-- Checked square icon (higher contrast) -->
|
|
507
|
+
<svg class="w-4 h-4 text-gray-800 dark:text-white" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
508
|
+
<rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2.5" fill="none" />
|
|
509
|
+
<path d="M7 13l3 3 7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" fill="none" />
|
|
510
|
+
</svg>
|
|
511
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">All</span>
|
|
512
|
+
</button>
|
|
513
|
+
|
|
514
|
+
<button
|
|
515
|
+
type="button"
|
|
516
|
+
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"
|
|
517
|
+
onclick={() => invertSelection(column.key, sortedValues)}
|
|
518
|
+
disabled={sortedValues.length === 0}
|
|
519
|
+
aria-label="Invert selection"
|
|
520
|
+
title="Invert selection"
|
|
521
|
+
>
|
|
522
|
+
<!-- Half-checked square icon (left half filled) - higher contrast -->
|
|
523
|
+
<svg class="w-4 h-4 text-gray-800 dark:text-white" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
524
|
+
<rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2.5" fill="none" />
|
|
525
|
+
<rect x="4" y="4" width="8.5" height="16" fill="currentColor" opacity="0.28" stroke="none" />
|
|
526
|
+
<path d="M7 13l3 3 7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" fill="none" />
|
|
527
|
+
</svg>
|
|
528
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">Invert</span>
|
|
529
|
+
</button>
|
|
530
|
+
|
|
531
|
+
<button
|
|
532
|
+
type="button"
|
|
533
|
+
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"
|
|
534
|
+
onclick={() => checkNone(column.key)}
|
|
535
|
+
disabled={sortedValues.length === 0}
|
|
536
|
+
aria-label="Uncheck all"
|
|
537
|
+
title="Uncheck all"
|
|
538
|
+
>
|
|
539
|
+
<!-- Empty square icon (higher contrast stroke) -->
|
|
540
|
+
<svg class="w-4 h-4 text-gray-800 dark:text-white" viewBox="0 0 24 24" fill="none" aria-hidden="true">
|
|
541
|
+
<rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2" fill="none" />
|
|
542
|
+
</svg>
|
|
543
|
+
<span class="text-sm text-gray-800 dark:text-gray-100 select-none">None</span>
|
|
544
|
+
</button>
|
|
545
|
+
</div>
|
|
546
|
+
|
|
547
|
+
<div class="pr-1">
|
|
548
|
+
<Badge color="blue" rounded title="Total options">{sortedValues.length}</Badge>
|
|
549
|
+
</div>
|
|
550
|
+
</div>
|
|
551
|
+
</div>
|
|
552
|
+
|
|
553
|
+
<div class="p-1 overflow-y-auto flex-1" use:registerList={{ key: column.key }}>
|
|
554
|
+
{#if sortedValues.length > populateThreshold && !loadedOptions[column.key]}
|
|
555
|
+
<div class="p-1 text-sm text-gray-700 dark:text-gray-200">
|
|
556
|
+
Too many options ({sortedValues.length}) to render by default.
|
|
557
|
+
<button
|
|
558
|
+
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"
|
|
559
|
+
type="button"
|
|
560
|
+
onclick={() => loadedOptions = { ...loadedOptions, [column.key]: true }}
|
|
561
|
+
>
|
|
562
|
+
Load options
|
|
563
|
+
</button>
|
|
564
|
+
</div>
|
|
565
|
+
{:else}
|
|
566
|
+
{#if sortedValues.length > virtualThreshold}
|
|
567
|
+
{@const v = getVirtualSlice(column, sortedValues)}
|
|
568
|
+
|
|
569
|
+
<div style="height:{v.totalHeight}px; position:relative;">
|
|
570
|
+
<div style="position:absolute; top:{v.top}px; left:0; right:0;">
|
|
571
|
+
{#each v.slice as value, idx (idx)}
|
|
572
|
+
{@const isSelected = selections[column.key]?.includes(value)}
|
|
573
|
+
{@const displayValue = value === null || value === undefined ? '(empty)' : String(value)}
|
|
574
|
+
|
|
575
|
+
<label 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" style="height:{virtualItemHeight}px; box-sizing:border-box;">
|
|
576
|
+
<input
|
|
577
|
+
type="checkbox"
|
|
578
|
+
checked={isSelected}
|
|
579
|
+
onchange={() => toggleSelection(column.key, value)}
|
|
580
|
+
class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
|
|
581
|
+
/>
|
|
582
|
+
<span class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis">{displayValue}</span>
|
|
583
|
+
{#if showCounts && column.counts && column.counts[value] !== undefined}
|
|
584
|
+
<span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0">({column.counts[value]})</span>
|
|
585
|
+
{/if}
|
|
586
|
+
</label>
|
|
587
|
+
{/each}
|
|
588
|
+
</div>
|
|
589
|
+
</div>
|
|
590
|
+
|
|
591
|
+
{:else}
|
|
592
|
+
{#if sortedValues.length > 0}
|
|
593
|
+
{#each sortedValues as value, idx (idx)}
|
|
594
|
+
{@const isSelected = selections[column.key]?.includes(value)}
|
|
595
|
+
{@const displayValue = value === null || value === undefined ? '(empty)' : String(value)}
|
|
596
|
+
|
|
597
|
+
<label 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">
|
|
598
|
+
<input
|
|
599
|
+
type="checkbox"
|
|
600
|
+
checked={isSelected}
|
|
601
|
+
onchange={() => toggleSelection(column.key, value)}
|
|
602
|
+
class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
|
|
603
|
+
/>
|
|
604
|
+
<span class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis">{displayValue}</span>
|
|
605
|
+
{#if showCounts && column.counts && column.counts[value] !== undefined}
|
|
606
|
+
<span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0">({column.counts[value]})</span>
|
|
607
|
+
{/if}
|
|
608
|
+
</label>
|
|
609
|
+
{/each}
|
|
610
|
+
{:else}
|
|
611
|
+
<div class="p-4 text-center text-sm text-gray-400 dark:text-gray-500">No values available</div>
|
|
612
|
+
{/if}
|
|
613
|
+
{/if}
|
|
614
|
+
{/if}
|
|
615
|
+
</div>
|
|
616
|
+
</div>
|
|
617
|
+
{/if}
|
|
618
|
+
</div>
|
|
619
|
+
{/each}
|
|
620
|
+
</div>
|
|
621
|
+
</div>
|
|
622
|
+
|
|
623
|
+
<style>
|
|
624
|
+
/* Grid layout classes */
|
|
625
|
+
.grid-horizontal {
|
|
626
|
+
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
.grid-vertical {
|
|
630
|
+
grid-template-columns: 1fr;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/* Dropdown menu positioning */
|
|
634
|
+
.dropdown-menu {
|
|
635
|
+
z-index: 9999;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/* Responsive adjustments */
|
|
639
|
+
@media (max-width: 768px) {
|
|
640
|
+
.grid-horizontal {
|
|
641
|
+
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
@media (max-width: 640px) {
|
|
646
|
+
.grid-horizontal {
|
|
647
|
+
grid-template-columns: 1fr;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
</style>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { Modal } from 'flowbite-svelte';
|
|
3
|
+
import { CloseOutline } from "flowbite-svelte-icons";
|
|
4
|
+
import DataTableFilters from './DataTableFilters.svelte';
|
|
5
|
+
|
|
6
|
+
// Props — make `open` bindable so parent can use `bind:open`
|
|
7
|
+
let {
|
|
8
|
+
open = $bindable(false),
|
|
9
|
+
columnFilters = [],
|
|
10
|
+
activeFilters = {},
|
|
11
|
+
/** @type {string} */
|
|
12
|
+
title = 'Filters',
|
|
13
|
+
/** @type {(..._args: any[]) => void} */
|
|
14
|
+
filterChange = (..._args) => {},
|
|
15
|
+
direction = 'horizontal',
|
|
16
|
+
showCounts = true,
|
|
17
|
+
className = ''
|
|
18
|
+
} = $props();
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<Modal bind:open={open} size="lg" dismissable={false}>
|
|
22
|
+
<div class="flex items-center justify-between mb-2">
|
|
23
|
+
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">Filters</h3>
|
|
24
|
+
<button
|
|
25
|
+
aria-label="Close"
|
|
26
|
+
class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"
|
|
27
|
+
onclick={() => open = false}
|
|
28
|
+
>
|
|
29
|
+
<CloseOutline class="shrink-0 h-6 w-6" />
|
|
30
|
+
</button>
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
<DataTableFilters
|
|
34
|
+
columnFilters={columnFilters}
|
|
35
|
+
activeFilters={activeFilters}
|
|
36
|
+
filterChange={(payload) => { try { return filterChange(payload); } catch (e) {} }}
|
|
37
|
+
direction={direction}
|
|
38
|
+
showCounts={showCounts}
|
|
39
|
+
/>
|
|
40
|
+
</Modal>
|