@zhangt58/svelte-vtable 0.1.4 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +177 -44
  2. package/dist/components/DataTable.svelte +489 -0
  3. package/dist/components/DataTable.svelte.d.ts +31 -0
  4. package/dist/components/DataTableControls.svelte +576 -0
  5. package/dist/components/DataTableControls.svelte.d.ts +39 -0
  6. package/dist/components/DataTableFilters.svelte +215 -0
  7. package/dist/components/DataTableFilters.svelte.d.ts +37 -0
  8. package/dist/components/FiltersModal.svelte +65 -0
  9. package/dist/components/FiltersModal.svelte.d.ts +25 -0
  10. package/dist/components/PaginationBar.svelte +86 -0
  11. package/dist/components/PaginationBar.svelte.d.ts +15 -0
  12. package/dist/components/filters/FilterColumn.svelte +53 -0
  13. package/dist/components/filters/FilterColumn.svelte.d.ts +19 -0
  14. package/dist/components/filters/FilterDateRange.svelte +341 -0
  15. package/dist/components/filters/FilterDateRange.svelte.d.ts +19 -0
  16. package/dist/components/filters/FilterValueList.svelte +437 -0
  17. package/dist/components/filters/FilterValueList.svelte.d.ts +33 -0
  18. package/dist/components/filters/useFilterState.svelte.d.ts +25 -0
  19. package/dist/components/filters/useFilterState.svelte.js +132 -0
  20. package/dist/components/flowbite/DataTableControls.svelte +525 -0
  21. package/dist/components/flowbite/DataTableControls.svelte.d.ts +39 -0
  22. package/{src/components → dist/components/flowbite}/FiltersModal.svelte +5 -15
  23. package/dist/components/flowbite/FiltersModal.svelte.d.ts +25 -0
  24. package/dist/filterUtils.d.ts +76 -0
  25. package/{src/lib → dist}/filterUtils.js +48 -46
  26. package/dist/index.d.ts +60 -0
  27. package/dist/index.flowbite.d.ts +3 -0
  28. package/dist/index.flowbite.js +9 -0
  29. package/dist/index.js +49 -0
  30. package/{src/lib → dist}/styles.css +26 -3
  31. package/package.json +38 -17
  32. package/src/components/DataTable.svelte +0 -252
  33. package/src/components/DataTableControls.svelte +0 -500
  34. package/src/components/DataTableFilters.svelte +0 -1225
  35. package/src/lib/dist/styles.css +0 -273
  36. package/src/lib/index.js +0 -16
@@ -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>
@@ -0,0 +1,19 @@
1
+ export default FilterDateRange;
2
+ type FilterDateRange = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const FilterDateRange: import("svelte").Component<{
7
+ column: any;
8
+ selection?: Record<string, any>;
9
+ relativeRangePresets?: any[];
10
+ onchange: any;
11
+ onclear: any;
12
+ }, {}, "">;
13
+ type $$ComponentProps = {
14
+ column: any;
15
+ selection?: Record<string, any>;
16
+ relativeRangePresets?: any[];
17
+ onchange: any;
18
+ onclear: any;
19
+ };