@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.
@@ -10,8 +10,8 @@
10
10
  activeFilters = {},
11
11
  /** @type {string} */
12
12
  title = 'Filters',
13
- /** @type {(..._args: any[]) => void} */
14
- filterChange = (..._args) => {},
13
+ // onfilter({ key, values, allFilters })
14
+ onfilter = undefined,
15
15
  direction = 'horizontal',
16
16
  showCounts = true,
17
17
  className = '',
@@ -30,15 +30,5 @@
30
30
  </button>
31
31
  </div>
32
32
 
33
- <DataTableFilters
34
- {columnFilters}
35
- {activeFilters}
36
- filterChange={(payload) => {
37
- try {
38
- return filterChange(payload);
39
- } catch (e) {}
40
- }}
41
- {direction}
42
- {showCounts}
43
- />
33
+ <DataTableFilters {columnFilters} {activeFilters} {onfilter} {direction} {showCounts} />
44
34
  </Modal>
@@ -0,0 +1,86 @@
1
+ <script>
2
+ /**
3
+ * PaginationBar — a self-contained native pagination sub-component.
4
+ *
5
+ * Props:
6
+ * - pages: { name: string; active: boolean; jump?: number }[]
7
+ * - currentPage: number
8
+ * - onpage: (page: number) => void
9
+ */
10
+ let { pages = [], currentPage = 1, onpage = undefined } = $props();
11
+
12
+ // Derive total pages from the last entry in the pages array (always the last page number).
13
+ const lastPage = $derived(pages.length > 0 ? +pages[pages.length - 1].name || 1 : 1);
14
+
15
+ function goTo(p) {
16
+ if (onpage) onpage(p);
17
+ }
18
+ </script>
19
+
20
+ <nav aria-label="Pagination">
21
+ <ul role="list" class="flex gap-0.5 items-center list-none p-0 m-0">
22
+ <li>
23
+ <button
24
+ type="button"
25
+ aria-label="Previous"
26
+ disabled={currentPage <= 1}
27
+ class="text-sm rounded-l-lg border border-gray-300 bg-transparent w-8 h-8 inline-flex items-center justify-center transition-colors hover:bg-gray-100 disabled:opacity-60 disabled:cursor-default disabled:pointer-events-none"
28
+ onclick={() => goTo(currentPage - 1)}
29
+ >
30
+ <span class="sr-only">Previous</span>
31
+ <svg
32
+ class="shrink-0 h-4 w-4"
33
+ xmlns="http://www.w3.org/2000/svg"
34
+ fill="none"
35
+ viewBox="0 0 24 24"
36
+ stroke="currentColor"
37
+ aria-hidden="true"
38
+ >
39
+ <path
40
+ stroke-linecap="round"
41
+ stroke-linejoin="round"
42
+ stroke-width="1"
43
+ d="M15 19l-7-7 7-7"
44
+ />
45
+ </svg>
46
+ </button>
47
+ </li>
48
+
49
+ {#each pages as p}
50
+ <li>
51
+ <button
52
+ type="button"
53
+ aria-current={p.active ? 'page' : undefined}
54
+ title={p.name === '…' ? `Jump to page ${p.jump}` : undefined}
55
+ onclick={() => goTo(p.jump ?? +p.name)}
56
+ class={`w-8 h-8 text-sm border inline-flex items-center justify-center transition-colors${p.active ? ' border-transparent' : ' border-gray-300 bg-transparent hover:bg-gray-100'}`}
57
+ style={p.active
58
+ ? 'background-color: var(--vtable-color-accent-light); color: var(--vtable-color-accent);'
59
+ : ''}>{p.name}</button
60
+ >
61
+ </li>
62
+ {/each}
63
+
64
+ <li>
65
+ <button
66
+ type="button"
67
+ aria-label="Next"
68
+ disabled={currentPage >= lastPage}
69
+ class="text-sm rounded-r-lg border border-gray-300 bg-transparent w-8 h-8 inline-flex items-center justify-center transition-colors hover:bg-gray-100 disabled:opacity-60 disabled:cursor-default disabled:pointer-events-none"
70
+ onclick={() => goTo(currentPage + 1)}
71
+ >
72
+ <span class="sr-only">Next</span>
73
+ <svg
74
+ class="shrink-0 h-4 w-4"
75
+ xmlns="http://www.w3.org/2000/svg"
76
+ fill="none"
77
+ viewBox="0 0 24 24"
78
+ stroke="currentColor"
79
+ aria-hidden="true"
80
+ >
81
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M9 5l7 7-7 7" />
82
+ </svg>
83
+ </button>
84
+ </li>
85
+ </ul>
86
+ </nav>
@@ -0,0 +1,46 @@
1
+ <script>
2
+ import { ChevronDownOutline } from 'flowbite-svelte-icons';
3
+
4
+ /**
5
+ * Renders a single column's toggle button header with label, badge, and chevron.
6
+ *
7
+ * Props:
8
+ * column — column config object { key, label, type, ... }
9
+ * isOpen — whether the dropdown is currently open
10
+ * isActive — whether the column has any active filters
11
+ * selection — current selection (array for value filters, object for date range)
12
+ * ontoggle — callback invoked when the button is clicked
13
+ */
14
+ let { column, isOpen = false, isActive = false, selection = [], ontoggle } = $props();
15
+
16
+ const selectionIsRange = $derived(!Array.isArray(selection));
17
+ const selectionCount = $derived(Array.isArray(selection) ? selection.length : 0);
18
+ </script>
19
+
20
+ <button
21
+ id="filter-{column.key}"
22
+ 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"
23
+ style={isActive
24
+ ? 'border-color: var(--vtable-color-accent-border); background-color: color-mix(in srgb, var(--vtable-color-accent-light) 40%, transparent);'
25
+ : ''}
26
+ onclick={ontoggle}
27
+ type="button"
28
+ >
29
+ <span class="flex-1 text-left whitespace-nowrap overflow-hidden text-ellipsis min-w-0">
30
+ {column.label || column.key}
31
+ </span>
32
+ {#if isActive}
33
+ {#if !selectionIsRange}
34
+ <span
35
+ class="ml-1 inline-flex items-center justify-center px-2 py-0.5 rounded-full text-xs font-medium text-white"
36
+ style="background-color: var(--vtable-color-accent);">{selectionCount}</span
37
+ >
38
+ {:else}
39
+ <span
40
+ class="ml-1 inline-flex items-center justify-center px-2 py-0.5 rounded-full text-xs font-medium text-white"
41
+ style="background-color: var(--vtable-color-accent);">range</span
42
+ >
43
+ {/if}
44
+ {/if}
45
+ <ChevronDownOutline class="w-5 h-5 shrink-0 transition-transform {isOpen ? 'rotate-180' : ''}" />
46
+ </button>
@@ -0,0 +1,341 @@
1
+ <script>
2
+ /**
3
+ * Renders a date/datetime range picker for a single filter column, including:
4
+ * - From / To date inputs with "Earliest" / "Latest" shortcuts
5
+ * - Dual-range slider (when column provides minValue / maxValue)
6
+ * - Relative-range quick preset buttons
7
+ * - Clear button
8
+ *
9
+ * Props:
10
+ * column — column config { key, label, type, minValue, maxValue }
11
+ * selection — current range selection { from?: string, to?: string }
12
+ * relativeRangePresets — array of { label, value, unit } presets
13
+ * onchange — callback({ from?, to? }) called with the updated selection
14
+ * onclear — callback() when the clear button is clicked
15
+ */
16
+ let { column, selection = {}, relativeRangePresets = [], onchange, onclear } = $props();
17
+
18
+ const inputType = $derived(column.type === 'datetimerange' ? 'datetime-local' : 'date');
19
+
20
+ const applicablePresets = $derived(
21
+ relativeRangePresets.filter((p) => p.unit !== 'hour' || column.type === 'datetimerange'),
22
+ );
23
+
24
+ // Parse a filter date string to ms timestamp (consistent with filterUtils.matchesDateRange)
25
+ function parseFilterDate(str) {
26
+ if (!str) return NaN;
27
+ return /^\d{4}-\d{2}-\d{2}$/.test(str)
28
+ ? new Date(str + 'T00:00:00').getTime()
29
+ : new Date(str).getTime();
30
+ }
31
+
32
+ function formatAsInputValue(ms, type) {
33
+ const d = new Date(ms);
34
+ const pad = (n) => String(n).padStart(2, '0');
35
+ const dateStr = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
36
+ if (type === 'datetime-local') {
37
+ return `${dateStr}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
38
+ }
39
+ return dateStr;
40
+ }
41
+
42
+ const minMs = $derived(parseFilterDate(column.minValue));
43
+ const maxMs = $derived(parseFilterDate(column.maxValue));
44
+ const totalMs = $derived(maxMs - minMs);
45
+ const fromMs = $derived(parseFilterDate(selection.from));
46
+ const toMs = $derived(parseFilterDate(selection.to));
47
+
48
+ const fromPct = $derived(
49
+ totalMs > 0 && !isNaN(fromMs)
50
+ ? Math.max(0, Math.min(100, ((fromMs - minMs) / totalMs) * 100))
51
+ : 0,
52
+ );
53
+ const toPct = $derived(
54
+ totalMs > 0 && !isNaN(toMs)
55
+ ? Math.max(0, Math.min(100, ((toMs - minMs) / totalMs) * 100))
56
+ : 100,
57
+ );
58
+
59
+ function handleFieldInput(field, value) {
60
+ if (onchange) onchange({ ...selection, [field]: value });
61
+ }
62
+
63
+ function handleFromSlider(pct) {
64
+ let newFromMs = minMs + (pct / 100) * (maxMs - minMs);
65
+ if (!isNaN(toMs) && newFromMs > toMs) newFromMs = toMs;
66
+ if (onchange) onchange({ ...selection, from: formatAsInputValue(newFromMs, inputType) });
67
+ }
68
+
69
+ function handleToSlider(pct) {
70
+ let newToMs = minMs + (pct / 100) * (maxMs - minMs);
71
+ if (!isNaN(fromMs) && newToMs < fromMs) newToMs = fromMs;
72
+ if (onchange) onchange({ ...selection, to: formatAsInputValue(newToMs, inputType) });
73
+ }
74
+
75
+ function setRelativeRange(value, unit) {
76
+ const now = new Date();
77
+ const from = new Date(now);
78
+ switch (unit) {
79
+ case 'hour':
80
+ from.setHours(from.getHours() - value);
81
+ break;
82
+ case 'day':
83
+ from.setDate(from.getDate() - value);
84
+ break;
85
+ case 'week':
86
+ from.setDate(from.getDate() - value * 7);
87
+ break;
88
+ case 'month':
89
+ from.setMonth(from.getMonth() - value);
90
+ break;
91
+ case 'year':
92
+ from.setFullYear(from.getFullYear() - value);
93
+ break;
94
+ }
95
+ if (onchange) {
96
+ onchange({
97
+ from: formatAsInputValue(from.getTime(), inputType),
98
+ to: formatAsInputValue(now.getTime(), inputType),
99
+ });
100
+ }
101
+ }
102
+ </script>
103
+
104
+ <div class="p-3 flex flex-col gap-2">
105
+ <!-- From input + Earliest shortcut -->
106
+ <div class="flex items-center gap-2">
107
+ <label
108
+ class="text-xs font-medium text-gray-500 dark:text-gray-400 w-8 shrink-0"
109
+ for="filter-{column.key}-from"
110
+ >
111
+ From
112
+ </label>
113
+ <input
114
+ id="filter-{column.key}-from"
115
+ type={inputType}
116
+ 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"
117
+ value={selection.from || ''}
118
+ oninput={(e) => handleFieldInput('from', e.currentTarget.value)}
119
+ />
120
+ {#if column.minValue}
121
+ <button
122
+ type="button"
123
+ class="shrink-0 px-2 py-0.5 text-xs rounded border border-indigo-400 text-indigo-700 dark:text-indigo-300 hover:bg-indigo-50 dark:hover:bg-indigo-900/20 transition-colors flex items-center"
124
+ title="Set From to the earliest record ({column.minValue})"
125
+ aria-label="Set From to the earliest record"
126
+ onclick={() => handleFieldInput('from', column.minValue)}
127
+ >
128
+ <svg
129
+ class="w-4 h-4 text-indigo-700 dark:text-indigo-300"
130
+ viewBox="0 0 24 24"
131
+ fill="none"
132
+ aria-hidden="true"
133
+ >
134
+ <path
135
+ d="M11.5 7.5L7 12l4.5 4.5"
136
+ stroke="currentColor"
137
+ stroke-width="2"
138
+ stroke-linecap="round"
139
+ stroke-linejoin="round"
140
+ fill="none"
141
+ />
142
+ <path
143
+ d="M17.5 7.5L13 12l4.5 4.5"
144
+ stroke="currentColor"
145
+ stroke-width="2"
146
+ stroke-linecap="round"
147
+ stroke-linejoin="round"
148
+ fill="none"
149
+ />
150
+ </svg>
151
+ </button>
152
+ {/if}
153
+ </div>
154
+
155
+ <!-- To input + Latest shortcut -->
156
+ <div class="flex items-center gap-2">
157
+ <label
158
+ class="text-xs font-medium text-gray-500 dark:text-gray-400 w-8 shrink-0"
159
+ for="filter-{column.key}-to"
160
+ >
161
+ To
162
+ </label>
163
+ <input
164
+ id="filter-{column.key}-to"
165
+ type={inputType}
166
+ 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"
167
+ value={selection.to || ''}
168
+ oninput={(e) => handleFieldInput('to', e.currentTarget.value)}
169
+ />
170
+ {#if column.maxValue}
171
+ <button
172
+ type="button"
173
+ class="shrink-0 px-2 py-0.5 text-xs rounded border border-indigo-400 text-indigo-700 dark:text-indigo-300 hover:bg-indigo-50 dark:hover:bg-indigo-900/20 transition-colors flex items-center"
174
+ title="Set To to the latest record ({column.maxValue})"
175
+ aria-label="Set To to the latest record"
176
+ onclick={() => handleFieldInput('to', column.maxValue)}
177
+ >
178
+ <svg
179
+ class="w-4 h-4 text-indigo-700 dark:text-indigo-300"
180
+ viewBox="0 0 24 24"
181
+ fill="none"
182
+ aria-hidden="true"
183
+ >
184
+ <path
185
+ d="M12.5 7.5L17 12l-4.5 4.5"
186
+ stroke="currentColor"
187
+ stroke-width="2"
188
+ stroke-linecap="round"
189
+ stroke-linejoin="round"
190
+ fill="none"
191
+ />
192
+ <path
193
+ d="M6.5 7.5L11 12l-4.5 4.5"
194
+ stroke="currentColor"
195
+ stroke-width="2"
196
+ stroke-linecap="round"
197
+ stroke-linejoin="round"
198
+ fill="none"
199
+ />
200
+ </svg>
201
+ </button>
202
+ {/if}
203
+ </div>
204
+
205
+ <!-- Dual-range slider (only when column provides min/max) -->
206
+ {#if column.minValue && column.maxValue && totalMs > 0}
207
+ <div class="dual-range-wrapper" aria-label="Date range slider">
208
+ <div class="dual-range-track-bg"></div>
209
+ <div class="dual-range-track-fill" style="left:{fromPct}%; right:{100 - toPct}%"></div>
210
+ <input
211
+ type="range"
212
+ class="dual-range-input"
213
+ min="0"
214
+ max="100"
215
+ step="0.02"
216
+ value={fromPct}
217
+ style="z-index:{fromPct >= toPct - 2 ? 4 : 3}"
218
+ oninput={(e) => handleFromSlider(parseFloat(e.currentTarget.value))}
219
+ aria-label="Range start"
220
+ />
221
+ <input
222
+ type="range"
223
+ class="dual-range-input"
224
+ min="0"
225
+ max="100"
226
+ step="0.02"
227
+ value={toPct}
228
+ style="z-index:{fromPct >= toPct - 2 ? 3 : 4}"
229
+ oninput={(e) => handleToSlider(parseFloat(e.currentTarget.value))}
230
+ aria-label="Range end"
231
+ />
232
+ </div>
233
+ {/if}
234
+
235
+ <!-- Relative-range quick buttons -->
236
+ {#if applicablePresets.length > 0}
237
+ <div class="flex flex-wrap gap-1 pt-1 border-t border-gray-100 dark:border-gray-600">
238
+ {#each applicablePresets as preset (preset.label)}
239
+ <button
240
+ type="button"
241
+ class="px-2 py-0.5 text-xs rounded border border-blue-400 text-blue-700 dark:text-blue-300 hover:bg-blue-50 dark:hover:bg-blue-900/20 transition-colors"
242
+ title="Last {preset.value} {preset.unit}{preset.value !== 1 ? 's' : ''}"
243
+ onclick={() => setRelativeRange(preset.value, preset.unit)}
244
+ >
245
+ Last {preset.label}
246
+ </button>
247
+ {/each}
248
+ </div>
249
+ {/if}
250
+
251
+ {#if selection.from || selection.to}
252
+ <button
253
+ type="button"
254
+ class="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"
255
+ onclick={() => onclear && onclear()}
256
+ >
257
+ Clear range
258
+ </button>
259
+ {/if}
260
+ </div>
261
+
262
+ <style>
263
+ .dual-range-wrapper {
264
+ position: relative;
265
+ height: 20px;
266
+ display: flex;
267
+ align-items: center;
268
+ margin: 4px 0;
269
+ }
270
+
271
+ .dual-range-track-bg {
272
+ position: absolute;
273
+ left: 0;
274
+ right: 0;
275
+ height: 4px;
276
+ border-radius: 2px;
277
+ background: #d1d5db;
278
+ }
279
+
280
+ :global(.dark) .dual-range-track-bg {
281
+ background: #4b5563;
282
+ }
283
+
284
+ .dual-range-track-fill {
285
+ position: absolute;
286
+ height: 4px;
287
+ border-radius: 2px;
288
+ background: var(--vtable-color-info);
289
+ pointer-events: none;
290
+ }
291
+
292
+ .dual-range-input {
293
+ position: absolute;
294
+ left: 0;
295
+ width: 100%;
296
+ height: 4px;
297
+ background: none;
298
+ pointer-events: none;
299
+ appearance: none;
300
+ -webkit-appearance: none;
301
+ outline: none;
302
+ margin: 0;
303
+ padding: 0;
304
+ }
305
+
306
+ .dual-range-input::-webkit-slider-thumb {
307
+ -webkit-appearance: none;
308
+ appearance: none;
309
+ pointer-events: all;
310
+ width: 16px;
311
+ height: 16px;
312
+ border-radius: 50%;
313
+ background: var(--vtable-color-info);
314
+ cursor: pointer;
315
+ border: 2px solid #ffffff;
316
+ box-shadow: 0 0 0 1px var(--vtable-color-info);
317
+ transition: background 0.15s;
318
+ }
319
+
320
+ .dual-range-input::-webkit-slider-thumb:hover {
321
+ background: var(--vtable-color-info);
322
+ filter: brightness(0.85);
323
+ }
324
+
325
+ .dual-range-input::-moz-range-thumb {
326
+ pointer-events: all;
327
+ width: 14px;
328
+ height: 14px;
329
+ border-radius: 50%;
330
+ background: var(--vtable-color-info);
331
+ cursor: pointer;
332
+ border: 2px solid #ffffff;
333
+ box-shadow: 0 0 0 1px var(--vtable-color-info);
334
+ transition: background 0.15s;
335
+ }
336
+
337
+ .dual-range-input::-moz-range-thumb:hover {
338
+ background: var(--vtable-color-info);
339
+ filter: brightness(0.85);
340
+ }
341
+ </style>