@zhangt58/svelte-vtable 0.1.2 → 0.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhangt58/svelte-vtable",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "A Svelte 5 library for virtualized data tables with sorting, selection, and pagination controls.",
5
5
  "type": "module",
6
6
  "svelte": "./src/lib/index.js",
@@ -18,7 +18,7 @@
18
18
  "check": "svelte-check --tsconfig ./jsconfig.json",
19
19
  "build:styles": "node ./scripts/build-styles.cjs",
20
20
  "prepare": "npm run build:styles",
21
- "prepublishOnly": "npm run build:styles",
21
+ "prepublishOnly": "npm run build:styles && npm run check",
22
22
  "format": "prettier --write .",
23
23
  "format:check": "prettier --check ."
24
24
  },
@@ -53,12 +53,12 @@
53
53
  "@sveltejs/vite-plugin-svelte": "^6.2.1",
54
54
  "flowbite-svelte": "^1.28.1",
55
55
  "flowbite-svelte-icons": "^3.0.0",
56
- "prettier": "^3.7.4",
57
- "prettier-plugin-svelte": "^3.4.1",
58
56
  "svelte": "^5.0.0",
59
- "svelte-check": "^4.3.5",
57
+ "svelte-check": "^4.3.4",
60
58
  "svelte-virtuallists": "^1.4.2",
61
- "vite": "^7.2.2"
59
+ "vite": "^7.2.2",
60
+ "prettier": "^3.7.4",
61
+ "prettier-plugin-svelte": "^3.4.1"
62
62
  },
63
63
  "files": [
64
64
  "src",
@@ -96,11 +96,16 @@
96
96
  flushEmit();
97
97
  });
98
98
 
99
+ // Helper: is this column a date/datetime range filter?
100
+ function isDateRangeColumn(col) {
101
+ return col.type === 'daterange' || col.type === 'datetimerange';
102
+ }
103
+
99
104
  // Initialize selections from activeFilters
100
105
  $effect(() => {
101
106
  const newSelections = {};
102
107
  for (const col of columnFilters) {
103
- newSelections[col.key] = activeFilters[col.key] || [];
108
+ newSelections[col.key] = activeFilters[col.key] ?? (isDateRangeColumn(col) ? {} : []);
104
109
  }
105
110
  selections = newSelections;
106
111
  });
@@ -126,15 +131,17 @@
126
131
 
127
132
  // Clear all selections for a column
128
133
  function clearColumn(columnKey) {
129
- selections = { ...selections, [columnKey]: [] };
130
- scheduleEmit(columnKey, []);
134
+ const col = columnFilters.find((c) => c.key === columnKey);
135
+ const empty = isDateRangeColumn(col) ? {} : [];
136
+ selections = { ...selections, [columnKey]: empty };
137
+ scheduleEmit(columnKey, empty);
131
138
  }
132
139
 
133
140
  // Clear all filters
134
141
  function clearAllFilters() {
135
142
  const cleared = {};
136
143
  for (const col of columnFilters) {
137
- cleared[col.key] = [];
144
+ cleared[col.key] = isDateRangeColumn(col) ? {} : [];
138
145
  }
139
146
  selections = cleared;
140
147
 
@@ -144,12 +151,20 @@
144
151
 
145
152
  // Check if any filters are active
146
153
  const hasActiveFilters = $derived(() => {
147
- return Object.values(selections).some((arr) => arr && arr.length > 0);
154
+ return Object.values(selections).some((val) => {
155
+ if (Array.isArray(val)) return val.length > 0;
156
+ if (val && typeof val === 'object') return !!(val.from || val.to);
157
+ return false;
158
+ });
148
159
  });
149
160
 
150
161
  // Count active filters
151
162
  const activeFilterCount = $derived(() => {
152
- return Object.values(selections).reduce((sum, arr) => sum + (arr?.length || 0), 0);
163
+ return Object.values(selections).reduce((sum, val) => {
164
+ if (Array.isArray(val)) return sum + (val?.length || 0);
165
+ if (val && typeof val === 'object' && (val.from || val.to)) return sum + 1;
166
+ return sum;
167
+ }, 0);
153
168
  });
154
169
 
155
170
  // Toggle dropdown visibility for a column
@@ -243,6 +258,17 @@
243
258
  return { slice, start, end, top, totalHeight };
244
259
  }
245
260
 
261
+ // --- Helper: extract a numeric sort key from a value ---
262
+ // Returns the number itself if numeric, the first number found in a string,
263
+ // or 0 if no number can be found.
264
+ function extractNumber(v) {
265
+ if (v === null || v === undefined) return 0;
266
+ if (typeof v === 'number') return v;
267
+ const s = String(v);
268
+ const match = s.match(/-?\d+(\.\d+)?/);
269
+ return match ? parseFloat(match[0]) : 0;
270
+ }
271
+
246
272
  // --- Helper: return the values for a column after applying search + sort ---
247
273
  function getSortedValues(column) {
248
274
  // column.uniqueValues is expected to be an array of possible values
@@ -276,6 +302,13 @@
276
302
  // fallback to name compare
277
303
  }
278
304
 
305
+ if (mode === 'number') {
306
+ const na = extractNumber(va);
307
+ const nb = extractNumber(vb);
308
+ if (na !== nb) return (na - nb) * dir;
309
+ // fallback to name compare for equal numeric values
310
+ }
311
+
279
312
  // compare by name
280
313
  const sa = va === null || va === undefined ? '(empty)' : String(va);
281
314
  const sb = vb === null || vb === undefined ? '(empty)' : String(vb);
@@ -350,6 +383,14 @@
350
383
  clearColumn(columnKey);
351
384
  }
352
385
 
386
+ // Handle a date/datetime range input change
387
+ function handleDateRangeChange(columnKey, field, value) {
388
+ const current = selections[columnKey] || {};
389
+ const updated = { ...current, [field]: value };
390
+ selections = { ...selections, [columnKey]: updated };
391
+ scheduleEmit(columnKey, updated);
392
+ }
393
+
353
394
  // Show all dropdowns
354
395
  function showAllDropdowns() {
355
396
  const allOpen = {};
@@ -422,7 +463,8 @@
422
463
  class:grid-vertical={direction === 'vertical'}
423
464
  >
424
465
  {#each columnFilters as column (column.key)}
425
- {@const isActive = selections[column.key]?.length > 0}
466
+ {@const _sel = selections[column.key]}
467
+ {@const isActive = Array.isArray(_sel) ? _sel.length > 0 : !!(_sel?.from || _sel?.to)}
426
468
  {@const isOpen = openDropdowns[column.key]}
427
469
  {@const sortedValues = getSortedValues(column)}
428
470
  {@const currentSortMode = sortModes[column.key] || 'name'}
@@ -441,7 +483,11 @@
441
483
  {column.label || column.key}
442
484
  </span>
443
485
  {#if isActive}
444
- <Badge color="green" rounded class="ml-1">{selections[column.key].length}</Badge>
486
+ {#if Array.isArray(_sel)}
487
+ <Badge color="green" rounded class="ml-1">{_sel.length}</Badge>
488
+ {:else}
489
+ <Badge color="green" rounded class="ml-1">range</Badge>
490
+ {/if}
445
491
  {/if}
446
492
  <ChevronDownOutline
447
493
  class="w-5 h-5 shrink-0 transition-transform {isOpen ? 'rotate-180' : ''}"
@@ -456,263 +502,351 @@
456
502
  tabindex="-1"
457
503
  data-column={column.key}
458
504
  >
459
- <div class="px-1 py-1 border-b border-gray-200 dark:border-gray-600">
460
- <div class="flex items-center gap-1 w-full min-w-0">
461
- <input
462
- type="text"
463
- placeholder="Filter..."
464
- 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"
465
- bind:value={searchQueries[column.key]}
466
- />
467
-
468
- <div class="flex items-center gap-1">
505
+ {#if isDateRangeColumn(column)}
506
+ <!-- Date / datetime range picker UI -->
507
+ {@const inputType = column.type === 'datetimerange' ? 'datetime-local' : 'date'}
508
+ {@const dateRange = selections[column.key] || {}}
509
+ <div class="p-3 flex flex-col gap-2">
510
+ <div class="flex items-center gap-2">
511
+ <label
512
+ class="text-xs font-medium text-gray-500 dark:text-gray-400 w-8 shrink-0"
513
+ for="filter-{column.key}-from"
514
+ >
515
+ From
516
+ </label>
517
+ <input
518
+ id="filter-{column.key}-from"
519
+ type={inputType}
520
+ class="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-800 text-gray-700 dark:text-gray-200 focus:outline-none focus:border-blue-500 dark:focus:border-blue-400"
521
+ value={dateRange.from || ''}
522
+ oninput={(e) =>
523
+ handleDateRangeChange(column.key, 'from', e.currentTarget.value)}
524
+ />
525
+ </div>
526
+ <div class="flex items-center gap-2">
527
+ <label
528
+ class="text-xs font-medium text-gray-500 dark:text-gray-400 w-8 shrink-0"
529
+ for="filter-{column.key}-to"
530
+ >
531
+ To
532
+ </label>
533
+ <input
534
+ id="filter-{column.key}-to"
535
+ type={inputType}
536
+ class="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-800 text-gray-700 dark:text-gray-200 focus:outline-none focus:border-blue-500 dark:focus:border-blue-400"
537
+ value={dateRange.to || ''}
538
+ oninput={(e) => handleDateRangeChange(column.key, 'to', e.currentTarget.value)}
539
+ />
540
+ </div>
541
+ {#if dateRange.from || dateRange.to}
469
542
  <button
470
- 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"
471
- class:bg-blue-100={currentSortMode === 'name'}
472
- class:dark:bg-blue-900={currentSortMode === 'name'}
473
- onclick={() => toggleSortMode(column.key, 'name')}
474
- title="Sort by name"
475
543
  type="button"
544
+ class="mt-1 self-start px-2 py-0.5 text-xs text-red-600 border border-red-400 rounded hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors"
545
+ onclick={() => clearColumn(column.key)}
476
546
  >
477
- <span class="text-sm font-semibold">Aa</span>
478
- <ArrowDownOutline
479
- class={'w-3 h-3 transition-transform ' +
480
- (currentSortMode === 'name'
481
- ? 'text-blue-900 dark:text-blue-100'
482
- : 'text-gray-400 dark:text-gray-500')}
483
- style={currentSortMode === 'name' && currentSortDir === 'asc'
484
- ? 'transform: rotate(180deg);'
485
- : ''}
486
- aria-hidden="true"
487
- />
547
+ Clear range
488
548
  </button>
549
+ {/if}
550
+ </div>
551
+ {:else}
552
+ <!-- Value-selection filter UI (checkboxes) -->
553
+ <div class="px-1 py-1 border-b border-gray-200 dark:border-gray-600">
554
+ <div class="flex items-center gap-1 w-full min-w-0">
555
+ <input
556
+ type="text"
557
+ placeholder="Filter..."
558
+ 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"
559
+ bind:value={searchQueries[column.key]}
560
+ />
561
+
562
+ <div class="flex items-center gap-1">
563
+ <button
564
+ 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"
565
+ class:bg-blue-100={currentSortMode === 'name'}
566
+ class:dark:bg-blue-900={currentSortMode === 'name'}
567
+ onclick={() => toggleSortMode(column.key, 'name')}
568
+ title="Sort by name"
569
+ type="button"
570
+ >
571
+ <span
572
+ class="flex flex-col items-center leading-none font-semibold"
573
+ style="font-size:0.6rem;"
574
+ >
575
+ <span>A</span>
576
+ <span>Z</span>
577
+ </span>
578
+ <ArrowDownOutline
579
+ class={'w-3 h-3 transition-transform ' +
580
+ (currentSortMode === 'name'
581
+ ? 'text-blue-900 dark:text-blue-100'
582
+ : 'text-gray-400 dark:text-gray-500')}
583
+ style={currentSortMode === 'name' && currentSortDir === 'asc'
584
+ ? 'transform: rotate(180deg);'
585
+ : ''}
586
+ aria-hidden="true"
587
+ />
588
+ </button>
489
589
 
490
- {#if showCounts && column.counts}
491
590
  <button
492
591
  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"
493
- class:bg-blue-100={currentSortMode === 'count'}
494
- class:dark:bg-blue-900={currentSortMode === 'count'}
495
- onclick={() => toggleSortMode(column.key, 'count')}
496
- title="Sort by count"
592
+ class:bg-blue-100={currentSortMode === 'number'}
593
+ class:dark:bg-blue-900={currentSortMode === 'number'}
594
+ onclick={() => toggleSortMode(column.key, 'number')}
595
+ title="Sort by number"
596
+ aria-label="Sort by number"
497
597
  type="button"
498
598
  >
499
- <span class="text-sm font-semibold">#</span>
599
+ <span
600
+ class="flex flex-col items-center leading-none font-semibold"
601
+ style="font-size:0.6rem;"
602
+ >
603
+ <span>0</span>
604
+ <span>9</span>
605
+ </span>
500
606
  <ArrowDownOutline
501
607
  class={'w-3 h-3 transition-transform ' +
502
- (currentSortMode === 'count'
608
+ (currentSortMode === 'number'
503
609
  ? 'text-blue-900 dark:text-blue-100'
504
610
  : 'text-gray-400 dark:text-gray-500')}
505
- style={currentSortMode === 'count' && currentSortDir === 'asc'
611
+ style={currentSortMode === 'number' && currentSortDir === 'asc'
506
612
  ? 'transform: rotate(180deg);'
507
613
  : ''}
508
614
  aria-hidden="true"
509
615
  />
510
616
  </button>
511
- {/if}
617
+
618
+ {#if showCounts && column.counts}
619
+ <button
620
+ 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"
621
+ class:bg-blue-100={currentSortMode === 'count'}
622
+ class:dark:bg-blue-900={currentSortMode === 'count'}
623
+ onclick={() => toggleSortMode(column.key, 'count')}
624
+ title="Sort by count"
625
+ type="button"
626
+ >
627
+ <span class="text-sm font-semibold">#</span>
628
+ <ArrowDownOutline
629
+ class={'w-3 h-3 transition-transform ' +
630
+ (currentSortMode === 'count'
631
+ ? 'text-blue-900 dark:text-blue-100'
632
+ : 'text-gray-400 dark:text-gray-500')}
633
+ style={currentSortMode === 'count' && currentSortDir === 'asc'
634
+ ? 'transform: rotate(180deg);'
635
+ : ''}
636
+ aria-hidden="true"
637
+ />
638
+ </button>
639
+ {/if}
640
+ </div>
512
641
  </div>
513
642
  </div>
514
- </div>
515
643
 
516
- <!-- Action row: Check all / Invert / Uncheck all (icon buttons) -->
517
- <div class="px-2 py-1 border-b border-gray-100 dark:border-gray-600">
518
- <div class="flex items-center justify-between">
519
- <div class="flex items-center gap-1">
520
- <button
521
- type="button"
522
- 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"
523
- onclick={() => checkAll(column.key, sortedValues)}
524
- disabled={sortedValues.length === 0}
525
- aria-label="Check all"
526
- title="Check all"
527
- >
528
- <!-- Checked square icon (higher contrast) -->
529
- <svg
530
- class="w-4 h-4 text-gray-800 dark:text-white"
531
- viewBox="0 0 24 24"
532
- fill="none"
533
- aria-hidden="true"
644
+ <!-- Action row: Check all / Invert / Uncheck all (icon buttons) -->
645
+ <div class="px-2 py-1 border-b border-gray-100 dark:border-gray-600">
646
+ <div class="flex items-center justify-between">
647
+ <div class="flex items-center gap-1">
648
+ <button
649
+ type="button"
650
+ 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"
651
+ onclick={() => checkAll(column.key, sortedValues)}
652
+ disabled={sortedValues.length === 0}
653
+ aria-label="Check all"
654
+ title="Check all"
534
655
  >
535
- <rect
536
- x="3"
537
- y="3"
538
- width="18"
539
- height="18"
540
- rx="2"
541
- stroke="currentColor"
542
- stroke-width="2.5"
656
+ <!-- Checked square icon (higher contrast) -->
657
+ <svg
658
+ class="w-4 h-4 text-gray-800 dark:text-white"
659
+ viewBox="0 0 24 24"
543
660
  fill="none"
544
- />
545
- <path
546
- d="M7 13l3 3 7-7"
547
- stroke="currentColor"
548
- stroke-width="2"
549
- stroke-linecap="round"
550
- stroke-linejoin="round"
551
- fill="none"
552
- />
553
- </svg>
554
- <span class="text-sm text-gray-800 dark:text-gray-100 select-none">All</span>
555
- </button>
661
+ aria-hidden="true"
662
+ >
663
+ <rect
664
+ x="3"
665
+ y="3"
666
+ width="18"
667
+ height="18"
668
+ rx="2"
669
+ stroke="currentColor"
670
+ stroke-width="2.5"
671
+ fill="none"
672
+ />
673
+ <path
674
+ d="M7 13l3 3 7-7"
675
+ stroke="currentColor"
676
+ stroke-width="2"
677
+ stroke-linecap="round"
678
+ stroke-linejoin="round"
679
+ fill="none"
680
+ />
681
+ </svg>
682
+ <span class="text-sm text-gray-800 dark:text-gray-100 select-none">All</span>
683
+ </button>
556
684
 
557
- <button
558
- type="button"
559
- 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"
560
- onclick={() => invertSelection(column.key, sortedValues)}
561
- disabled={sortedValues.length === 0}
562
- aria-label="Invert selection"
563
- title="Invert selection"
564
- >
565
- <!-- Half-checked square icon (left half filled) - higher contrast -->
566
- <svg
567
- class="w-4 h-4 text-gray-800 dark:text-white"
568
- viewBox="0 0 24 24"
569
- fill="none"
570
- aria-hidden="true"
685
+ <button
686
+ type="button"
687
+ 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"
688
+ onclick={() => invertSelection(column.key, sortedValues)}
689
+ disabled={sortedValues.length === 0}
690
+ aria-label="Invert selection"
691
+ title="Invert selection"
571
692
  >
572
- <rect
573
- x="3"
574
- y="3"
575
- width="18"
576
- height="18"
577
- rx="2"
578
- stroke="currentColor"
579
- stroke-width="2.5"
580
- fill="none"
581
- />
582
- <rect
583
- x="4"
584
- y="4"
585
- width="8.5"
586
- height="16"
587
- fill="currentColor"
588
- opacity="0.28"
589
- stroke="none"
590
- />
591
- <path
592
- d="M7 13l3 3 7-7"
593
- stroke="currentColor"
594
- stroke-width="2"
595
- stroke-linecap="round"
596
- stroke-linejoin="round"
693
+ <!-- Half-checked square icon (left half filled) - higher contrast -->
694
+ <svg
695
+ class="w-4 h-4 text-gray-800 dark:text-white"
696
+ viewBox="0 0 24 24"
597
697
  fill="none"
598
- />
599
- </svg>
600
- <span class="text-sm text-gray-800 dark:text-gray-100 select-none">Invert</span>
601
- </button>
698
+ aria-hidden="true"
699
+ >
700
+ <rect
701
+ x="3"
702
+ y="3"
703
+ width="18"
704
+ height="18"
705
+ rx="2"
706
+ stroke="currentColor"
707
+ stroke-width="2.5"
708
+ fill="none"
709
+ />
710
+ <rect
711
+ x="4"
712
+ y="4"
713
+ width="8.5"
714
+ height="16"
715
+ fill="currentColor"
716
+ opacity="0.28"
717
+ stroke="none"
718
+ />
719
+ <path
720
+ d="M7 13l3 3 7-7"
721
+ stroke="currentColor"
722
+ stroke-width="2"
723
+ stroke-linecap="round"
724
+ stroke-linejoin="round"
725
+ fill="none"
726
+ />
727
+ </svg>
728
+ <span class="text-sm text-gray-800 dark:text-gray-100 select-none"
729
+ >Invert</span
730
+ >
731
+ </button>
602
732
 
603
- <button
604
- type="button"
605
- 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"
606
- onclick={() => checkNone(column.key)}
607
- disabled={sortedValues.length === 0}
608
- aria-label="Uncheck all"
609
- title="Uncheck all"
610
- >
611
- <!-- Empty square icon (higher contrast stroke) -->
612
- <svg
613
- class="w-4 h-4 text-gray-800 dark:text-white"
614
- viewBox="0 0 24 24"
615
- fill="none"
616
- aria-hidden="true"
733
+ <button
734
+ type="button"
735
+ 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"
736
+ onclick={() => checkNone(column.key)}
737
+ disabled={sortedValues.length === 0}
738
+ aria-label="Uncheck all"
739
+ title="Uncheck all"
617
740
  >
618
- <rect
619
- x="3"
620
- y="3"
621
- width="18"
622
- height="18"
623
- rx="2"
624
- stroke="currentColor"
625
- stroke-width="2"
741
+ <!-- Empty square icon (higher contrast stroke) -->
742
+ <svg
743
+ class="w-4 h-4 text-gray-800 dark:text-white"
744
+ viewBox="0 0 24 24"
626
745
  fill="none"
627
- />
628
- </svg>
629
- <span class="text-sm text-gray-800 dark:text-gray-100 select-none">None</span>
630
- </button>
631
- </div>
746
+ aria-hidden="true"
747
+ >
748
+ <rect
749
+ x="3"
750
+ y="3"
751
+ width="18"
752
+ height="18"
753
+ rx="2"
754
+ stroke="currentColor"
755
+ stroke-width="2"
756
+ fill="none"
757
+ />
758
+ </svg>
759
+ <span class="text-sm text-gray-800 dark:text-gray-100 select-none">None</span>
760
+ </button>
761
+ </div>
632
762
 
633
- <div class="pr-1">
634
- <Badge color="blue" rounded title="Total options">{sortedValues.length}</Badge>
763
+ <div class="pr-1">
764
+ <Badge color="blue" rounded title="Total options">{sortedValues.length}</Badge>
765
+ </div>
635
766
  </div>
636
767
  </div>
637
- </div>
638
768
 
639
- <div class="p-1 overflow-y-auto flex-1" use:registerList={{ key: column.key }}>
640
- {#if sortedValues.length > populateThreshold && !loadedOptions[column.key]}
641
- <div class="p-1 text-sm text-gray-700 dark:text-gray-200">
642
- Too many options ({sortedValues.length}) to render by default.
643
- <button
644
- 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"
645
- type="button"
646
- onclick={() => (loadedOptions = { ...loadedOptions, [column.key]: true })}
647
- >
648
- Load options
649
- </button>
650
- </div>
651
- {:else if sortedValues.length > virtualThreshold}
652
- {@const v = getVirtualSlice(column, sortedValues)}
653
-
654
- <div style="height:{v.totalHeight}px; position:relative;">
655
- <div style="position:absolute; top:{v.top}px; left:0; right:0;">
656
- {#each v.slice as value, idx (idx)}
657
- {@const isSelected = selections[column.key]?.includes(value)}
658
- {@const displayValue =
659
- value === null || value === undefined ? '(empty)' : String(value)}
660
-
661
- <label
662
- 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"
663
- style="height:{virtualItemHeight}px; box-sizing:border-box;"
664
- >
665
- <input
666
- type="checkbox"
667
- checked={isSelected}
668
- onchange={() => toggleSelection(column.key, value)}
669
- class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
670
- />
671
- <span
672
- class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
673
- >{displayValue}</span
769
+ <div
770
+ class="p-1 overflow-y-auto flex-1 max-h-48"
771
+ use:registerList={{ key: column.key }}
772
+ >
773
+ {#if sortedValues.length > populateThreshold && !loadedOptions[column.key]}
774
+ <div class="p-1 text-sm text-gray-700 dark:text-gray-200">
775
+ Too many options ({sortedValues.length}) to render by default.
776
+ <button
777
+ 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"
778
+ type="button"
779
+ onclick={() => (loadedOptions = { ...loadedOptions, [column.key]: true })}
780
+ >
781
+ Load options
782
+ </button>
783
+ </div>
784
+ {:else if sortedValues.length > virtualThreshold}
785
+ {@const v = getVirtualSlice(column, sortedValues)}
786
+
787
+ <div style="height:{v.totalHeight}px; position:relative;">
788
+ <div style="position:absolute; top:{v.top}px; left:0; right:0;">
789
+ {#each v.slice as value, idx (idx)}
790
+ {@const isSelected = selections[column.key]?.includes(value)}
791
+ {@const displayValue =
792
+ value === null || value === undefined ? '(empty)' : String(value)}
793
+
794
+ <label
795
+ 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"
796
+ style="height:{virtualItemHeight}px; box-sizing:border-box;"
674
797
  >
675
- {#if showCounts && column.counts && column.counts[value] !== undefined}
676
- <span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
677
- >({column.counts[value]})</span
798
+ <input
799
+ type="checkbox"
800
+ checked={isSelected}
801
+ onchange={() => toggleSelection(column.key, value)}
802
+ class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
803
+ />
804
+ <span
805
+ class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
806
+ >{displayValue}</span
678
807
  >
679
- {/if}
680
- </label>
681
- {/each}
808
+ {#if showCounts && column.counts && column.counts[value] !== undefined}
809
+ <span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
810
+ >({column.counts[value]})</span
811
+ >
812
+ {/if}
813
+ </label>
814
+ {/each}
815
+ </div>
682
816
  </div>
683
- </div>
684
- {:else if sortedValues.length > 0}
685
- {#each sortedValues as value, idx (idx)}
686
- {@const isSelected = selections[column.key]?.includes(value)}
687
- {@const displayValue =
688
- value === null || value === undefined ? '(empty)' : String(value)}
689
-
690
- <label
691
- 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"
692
- >
693
- <input
694
- type="checkbox"
695
- checked={isSelected}
696
- onchange={() => toggleSelection(column.key, value)}
697
- class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
698
- />
699
- <span
700
- class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
701
- >{displayValue}</span
817
+ {:else if sortedValues.length > 0}
818
+ {#each sortedValues as value, idx (idx)}
819
+ {@const isSelected = selections[column.key]?.includes(value)}
820
+ {@const displayValue =
821
+ value === null || value === undefined ? '(empty)' : String(value)}
822
+
823
+ <label
824
+ 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"
702
825
  >
703
- {#if showCounts && column.counts && column.counts[value] !== undefined}
704
- <span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
705
- >({column.counts[value]})</span
826
+ <input
827
+ type="checkbox"
828
+ checked={isSelected}
829
+ onchange={() => toggleSelection(column.key, value)}
830
+ class="w-4 h-4 shrink-0 cursor-pointer accent-blue-500"
831
+ />
832
+ <span
833
+ class="flex-1 text-sm text-gray-700 dark:text-gray-300 select-none min-w-0 overflow-hidden text-ellipsis"
834
+ >{displayValue}</span
706
835
  >
707
- {/if}
708
- </label>
709
- {/each}
710
- {:else}
711
- <div class="p-4 text-center text-sm text-gray-400 dark:text-gray-500">
712
- No values available
713
- </div>
714
- {/if}
715
- </div>
836
+ {#if showCounts && column.counts && column.counts[value] !== undefined}
837
+ <span class="text-xs text-gray-500 dark:text-gray-400 ml-auto shrink-0"
838
+ >({column.counts[value]})</span
839
+ >
840
+ {/if}
841
+ </label>
842
+ {/each}
843
+ {:else}
844
+ <div class="p-4 text-center text-sm text-gray-400 dark:text-gray-500">
845
+ No values available
846
+ </div>
847
+ {/if}
848
+ </div>
849
+ {/if}
716
850
  </div>
717
851
  {/if}
718
852
  </div>
@@ -27,15 +27,27 @@ export function getUniqueValuesWithCounts(data, key) {
27
27
  /**
28
28
  * Build column filters configuration from data and column definitions
29
29
  * @param {Array} data - Array of data objects
30
- * @param {Array} columns - Array of column definitions {key, label}
30
+ * @param {Array} columns - Array of column definitions {key, label, type?}
31
+ * Supported types: 'value' (default), 'daterange', 'datetimerange'
31
32
  * @returns {Array} Column filters configuration for DataTableFilters
32
33
  */
33
34
  export function buildColumnFilters(data, columns) {
34
35
  return columns.map((col) => {
36
+ const isDateRange = col.type === 'daterange' || col.type === 'datetimerange';
37
+ if (isDateRange) {
38
+ return {
39
+ key: col.key,
40
+ label: col.label || col.key,
41
+ type: col.type,
42
+ uniqueValues: [],
43
+ counts: {},
44
+ };
45
+ }
35
46
  const { uniqueValues, counts } = getUniqueValuesWithCounts(data, col.key);
36
47
  return {
37
48
  key: col.key,
38
49
  label: col.label || col.key,
50
+ type: col.type || 'value',
39
51
  uniqueValues,
40
52
  counts,
41
53
  };
@@ -43,9 +55,52 @@ export function buildColumnFilters(data, columns) {
43
55
  }
44
56
 
45
57
  /**
46
- * Apply filters to data with OR logic within columns and AND logic across columns
58
+ * Check whether a value falls within a date/datetime range.
59
+ * Both `from` and `to` are optional; omitting one leaves that end open.
60
+ * Date-only strings (YYYY-MM-DD) are parsed as **local** midnight (i.e. they
61
+ * are treated as `<date>T00:00:00` in the local timezone, not UTC). A date-only
62
+ * `to` value is inclusive through the end of that calendar day (23:59:59.999
63
+ * local time).
64
+ * @param {any} value - The item's date value (string, Date, or number)
65
+ * @param {string|null|undefined} from - Range start (ISO date or datetime-local string)
66
+ * @param {string|null|undefined} to - Range end (ISO date or datetime-local string)
67
+ * @returns {boolean}
68
+ */
69
+ export function matchesDateRange(value, from, to) {
70
+ if (value === null || value === undefined || value === '') return false;
71
+
72
+ // Parse the item date; treat date-only strings as local midnight
73
+ let itemDate;
74
+ if (value instanceof Date) {
75
+ itemDate = value;
76
+ } else {
77
+ const str = String(value);
78
+ itemDate = /^\d{4}-\d{2}-\d{2}$/.test(str) ? new Date(str + 'T00:00:00') : new Date(str);
79
+ }
80
+ if (isNaN(itemDate.getTime())) return false;
81
+
82
+ if (from) {
83
+ const fromDate = /^\d{4}-\d{2}-\d{2}$/.test(from)
84
+ ? new Date(from + 'T00:00:00')
85
+ : new Date(from);
86
+ if (!isNaN(fromDate.getTime()) && itemDate < fromDate) return false;
87
+ }
88
+
89
+ if (to) {
90
+ // Extend date-only "to" to the end of that day so it is fully inclusive
91
+ const toDate = /^\d{4}-\d{2}-\d{2}$/.test(to) ? new Date(to + 'T23:59:59.999') : new Date(to);
92
+ if (!isNaN(toDate.getTime()) && itemDate > toDate) return false;
93
+ }
94
+
95
+ return true;
96
+ }
97
+
98
+ /**
99
+ * Apply filters to data with OR logic within columns and AND logic across columns.
100
+ * Supports both value-selection filters (array) and date/datetime range filters
101
+ * (object with optional `from` and `to` string fields).
47
102
  * @param {Array} data - Array of data objects to filter
48
- * @param {Object} activeFilters - Active filters object { columnKey: [selectedValues] }
103
+ * @param {Object} activeFilters - Active filters: { columnKey: string[] | {from?, to?} }
49
104
  * @returns {Array} Filtered data array
50
105
  */
51
106
  export function applyFilters(data, activeFilters) {
@@ -55,17 +110,20 @@ export function applyFilters(data, activeFilters) {
55
110
 
56
111
  return data.filter((item) => {
57
112
  // Check each column filter (AND logic across columns)
58
- for (const [columnKey, selectedValues] of Object.entries(activeFilters)) {
59
- // Skip if no values selected for this column
60
- if (!selectedValues || selectedValues.length === 0) continue;
61
-
62
- // Get the item's value for this column
63
- const itemValue = item[columnKey];
64
- const normalizedItemValue = itemValue ?? '(empty)';
65
-
66
- // Check if item value matches any selected value (OR logic within column)
67
- if (!selectedValues.includes(normalizedItemValue)) {
68
- return false; // Item doesn't match this column filter
113
+ for (const [columnKey, filterValue] of Object.entries(activeFilters)) {
114
+ if (!filterValue) continue;
115
+
116
+ if (Array.isArray(filterValue)) {
117
+ // Value-based filter (OR logic within column)
118
+ if (filterValue.length === 0) continue;
119
+ const itemValue = item[columnKey];
120
+ const normalizedItemValue = itemValue ?? '(empty)';
121
+ if (!filterValue.includes(normalizedItemValue)) return false;
122
+ } else if (typeof filterValue === 'object') {
123
+ // Date / datetime range filter
124
+ const { from, to } = filterValue;
125
+ if (!from && !to) continue;
126
+ if (!matchesDateRange(item[columnKey], from, to)) return false;
69
127
  }
70
128
  }
71
129
  return true; // Item matches all column filters
@@ -74,37 +132,48 @@ export function applyFilters(data, activeFilters) {
74
132
 
75
133
  /**
76
134
  * Check if any filters are currently active
77
- * @param {Object} activeFilters - Active filters object { columnKey: [selectedValues] }
135
+ * @param {Object} activeFilters - Active filters object
78
136
  * @returns {boolean} True if any filters are active
79
137
  */
80
138
  export function hasActiveFilters(activeFilters) {
81
- return Object.values(activeFilters).some((arr) => arr && arr.length > 0);
139
+ return Object.values(activeFilters).some((val) => {
140
+ if (Array.isArray(val)) return val.length > 0;
141
+ if (val && typeof val === 'object') return !!(val.from || val.to);
142
+ return false;
143
+ });
82
144
  }
83
145
 
84
146
  /**
85
- * Count total number of active filter selections
86
- * @param {Object} activeFilters - Active filters object { columnKey: [selectedValues] }
87
- * @returns {number} Total count of selected filter values
147
+ * Count total number of active filter selections.
148
+ * Each active date range counts as 1.
149
+ * @param {Object} activeFilters - Active filters object
150
+ * @returns {number} Total count of active filter values/ranges
88
151
  */
89
152
  export function countActiveFilters(activeFilters) {
90
- return Object.values(activeFilters).reduce((sum, arr) => sum + (arr?.length || 0), 0);
153
+ return Object.values(activeFilters).reduce((sum, val) => {
154
+ if (Array.isArray(val)) return sum + (val?.length || 0);
155
+ if (val && typeof val === 'object' && (val.from || val.to)) return sum + 1;
156
+ return sum;
157
+ }, 0);
91
158
  }
92
159
 
93
160
  /**
94
161
  * Clear all filters
95
162
  * @param {Array} columnFilters - Column filters configuration
96
- * @returns {Object} Empty filters object
163
+ * @returns {Object} Cleared filters object (arrays for value columns, empty objects for date ranges)
97
164
  */
98
165
  export function clearAllFilters(columnFilters) {
99
166
  const cleared = {};
100
167
  columnFilters.forEach((col) => {
101
- cleared[col.key] = [];
168
+ const isDateRange = col.type === 'daterange' || col.type === 'datetimerange';
169
+ cleared[col.key] = isDateRange ? {} : [];
102
170
  });
103
171
  return cleared;
104
172
  }
105
173
 
106
174
  /**
107
- * Export active filters as URL search params
175
+ * Export active filters as URL search params.
176
+ * Date range filters are serialised as `<key>_from` and `<key>_to` params.
108
177
  * @param {Object} activeFilters - Active filters object
109
178
  * @returns {URLSearchParams} URL search params object
110
179
  */
@@ -112,8 +181,11 @@ export function filtersToSearchParams(activeFilters) {
112
181
  const params = new URLSearchParams();
113
182
 
114
183
  for (const [key, values] of Object.entries(activeFilters)) {
115
- if (values && values.length > 0) {
184
+ if (Array.isArray(values) && values.length > 0) {
116
185
  params.set(key, values.join(','));
186
+ } else if (values && typeof values === 'object' && !Array.isArray(values)) {
187
+ if (values.from) params.set(`${key}_from`, values.from);
188
+ if (values.to) params.set(`${key}_to`, values.to);
117
189
  }
118
190
  }
119
191
 
@@ -131,10 +203,22 @@ export function searchParamsToFilters(searchParams, columnFilters) {
131
203
  typeof searchParams === 'string' ? new URLSearchParams(searchParams) : searchParams;
132
204
 
133
205
  const validKeys = new Set(columnFilters.map((c) => c.key));
206
+ const dateRangeKeys = new Set(
207
+ columnFilters
208
+ .filter((c) => c.type === 'daterange' || c.type === 'datetimerange')
209
+ .map((c) => c.key),
210
+ );
134
211
  const filters = {};
135
212
 
136
213
  for (const [key, value] of params.entries()) {
137
- if (validKeys.has(key) && value) {
214
+ if (key.endsWith('_from') || key.endsWith('_to')) {
215
+ const colKey = key.replace(/_from$|_to$/, '');
216
+ if (validKeys.has(colKey) && dateRangeKeys.has(colKey)) {
217
+ if (!filters[colKey]) filters[colKey] = {};
218
+ if (key.endsWith('_from')) filters[colKey].from = value;
219
+ else filters[colKey].to = value;
220
+ }
221
+ } else if (validKeys.has(key) && !dateRangeKeys.has(key) && value) {
138
222
  filters[key] = value
139
223
  .split(',')
140
224
  .map((v) => v.trim())
package/src/lib/index.js CHANGED
@@ -7,6 +7,7 @@ export {
7
7
  getUniqueValuesWithCounts,
8
8
  buildColumnFilters,
9
9
  applyFilters,
10
+ matchesDateRange,
10
11
  hasActiveFilters,
11
12
  countActiveFilters,
12
13
  clearAllFilters,