@zhangt58/svelte-vtable 0.2.0 → 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 (34) hide show
  1. package/README.md +39 -2
  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/{src → dist}/components/DataTableFilters.svelte +18 -3
  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.d.ts +15 -0
  11. package/{src → dist}/components/filters/FilterColumn.svelte +10 -3
  12. package/dist/components/filters/FilterColumn.svelte.d.ts +19 -0
  13. package/dist/components/filters/FilterDateRange.svelte.d.ts +19 -0
  14. package/{src → dist}/components/filters/FilterValueList.svelte +28 -10
  15. package/dist/components/filters/FilterValueList.svelte.d.ts +33 -0
  16. package/dist/components/filters/useFilterState.svelte.d.ts +25 -0
  17. package/dist/components/flowbite/DataTableControls.svelte +525 -0
  18. package/dist/components/flowbite/DataTableControls.svelte.d.ts +39 -0
  19. package/{src/components → dist/components/flowbite}/FiltersModal.svelte +2 -2
  20. package/dist/components/flowbite/FiltersModal.svelte.d.ts +25 -0
  21. package/dist/filterUtils.d.ts +76 -0
  22. package/dist/index.d.ts +60 -0
  23. package/dist/index.flowbite.d.ts +3 -0
  24. package/dist/index.flowbite.js +9 -0
  25. package/{src/lib → dist}/index.js +7 -7
  26. package/{src/lib → dist}/styles.css +9 -0
  27. package/package.json +38 -17
  28. package/src/components/DataTable.svelte +0 -234
  29. package/src/components/DataTableControls.svelte +0 -270
  30. package/src/lib/dist/styles.css +0 -287
  31. /package/{src → dist}/components/PaginationBar.svelte +0 -0
  32. /package/{src → dist}/components/filters/FilterDateRange.svelte +0 -0
  33. /package/{src → dist}/components/filters/useFilterState.svelte.js +0 -0
  34. /package/{src/lib → dist}/filterUtils.js +0 -0
@@ -1,270 +0,0 @@
1
- <script>
2
- import { Search, Badge, Select } from 'flowbite-svelte';
3
- import { FilterOutline } from 'flowbite-svelte-icons';
4
- import FiltersModal from './FiltersModal.svelte';
5
- import PaginationBar from './PaginationBar.svelte';
6
-
7
- // Props for search, pagination (perPage made bindable using Svelte 5 rune $bindable)
8
- let {
9
- search = '',
10
- currentPage = 1,
11
- perPage = $bindable(25),
12
- totalItems = 0,
13
- onpage = undefined,
14
- onsearch = undefined,
15
- // control visibility of DataTableFilters above the controls
16
- filtersVisible = false,
17
- // callback when toggle changes: onfilterstoggle({ visible })
18
- onfilterstoggle = undefined,
19
- // optional DataTableFilters inputs - when provided, this component will render the filters
20
- columnFilters = [],
21
- activeFilters = {},
22
- // onfilter({ key, values, allFilters }) — passed through to DataTableFilters
23
- /** @type {((..._args: any[]) => void) | undefined} */
24
- onfilter = undefined,
25
- direction = 'horizontal',
26
- showCounts = true,
27
- className = '',
28
- // callback when perPage changes: onperpage({ perPage })
29
- onperpage = undefined,
30
- } = $props();
31
-
32
- const perPageOptions = [
33
- { value: 10, name: '10 rows' },
34
- { value: 25, name: '25 rows' },
35
- { value: 50, name: '50 rows' },
36
- { value: 100, name: '100 rows' },
37
- ];
38
-
39
- // count of active filters (sum of selected values per column)
40
- const activeFilterCount = $derived.by(() => {
41
- try {
42
- return Object.values(activeFilters || {}).reduce(
43
- (sum, v) => sum + (Array.isArray(v) ? v.length : 0),
44
- 0,
45
- );
46
- } catch (e) {
47
- return 0;
48
- }
49
- });
50
-
51
- // totalPages derived from totalItems and perPage
52
- const totalPages = $derived(Math.max(1, Math.ceil(totalItems / perPage)));
53
-
54
- // clamp currentPage into valid range whenever currentPage or totalPages change
55
- $effect(() => {
56
- currentPage = Math.min(Math.max(1, +currentPage || 1), totalPages);
57
- });
58
-
59
- function goTo(p) {
60
- const np = Math.min(Math.max(1, Math.floor(p)), totalPages);
61
- if (np !== currentPage) {
62
- currentPage = np;
63
- if (onpage !== undefined) {
64
- onpage({ page: np });
65
- }
66
- }
67
- }
68
-
69
- // emit search changes for parent when search updates
70
- $effect(() => {
71
- if (typeof search !== 'undefined') {
72
- if (onsearch !== undefined) {
73
- onsearch({ search });
74
- }
75
- }
76
- });
77
-
78
- // call onfilterstoggle when filtersVisible changes (avoid on:change typing issues)
79
- let _prevFiltersVisible = undefined;
80
- $effect(() => {
81
- // initialize previous value on first run without emitting
82
- if (_prevFiltersVisible === undefined) {
83
- _prevFiltersVisible = filtersVisible;
84
- return;
85
- }
86
- if (filtersVisible !== _prevFiltersVisible) {
87
- if (onfilterstoggle !== undefined) {
88
- try {
89
- onfilterstoggle({ visible: filtersVisible });
90
- } catch (err) {}
91
- }
92
- _prevFiltersVisible = filtersVisible;
93
- }
94
- });
95
-
96
- // Watch perPage changes: reset to first page and notify parent
97
- let _prevPerPage = undefined;
98
- $effect(() => {
99
- if (_prevPerPage === undefined) {
100
- _prevPerPage = perPage;
101
- return;
102
- }
103
- if (perPage !== _prevPerPage) {
104
- // coerce to number just in case
105
- perPage = +perPage || 25;
106
- _prevPerPage = perPage;
107
- // reset to page 1 (common UX) and notify parent
108
- currentPage = 1;
109
- if (onperpage !== undefined) {
110
- try {
111
- onperpage({ perPage });
112
- } catch (err) {}
113
- }
114
- if (onpage !== undefined) {
115
- try {
116
- onpage({ page: currentPage });
117
- } catch (err) {}
118
- }
119
- }
120
- });
121
-
122
- // Calculate range for display
123
- const startItem = $derived(totalItems === 0 ? 0 : (currentPage - 1) * perPage + 1);
124
- const endItem = $derived(Math.min(currentPage * perPage, totalItems));
125
-
126
- // visiblePages: number of middle numeric buttons to show (not counting first/last)
127
- const visiblePages = 5;
128
-
129
- const perPageSelectId = `dpc-perpage-${Math.random().toString(36).slice(2, 7)}`;
130
-
131
- // pages array for Pagination component (condensed with ellipses)
132
- const pages = $derived.by(() => {
133
- const total = totalPages;
134
- const current = Math.min(Math.max(1, Math.floor(currentPage)), total);
135
-
136
- // If small number of pages, show all
137
- if (total <= visiblePages + 2) {
138
- return Array.from({ length: total }, (_, i) => ({
139
- name: String(i + 1),
140
- active: i + 1 === current,
141
- }));
142
- }
143
-
144
- const pagesArr = [];
145
- // always show first
146
- pagesArr.push({ name: '1', active: current === 1 });
147
-
148
- const half = Math.floor(visiblePages / 2);
149
- let start = Math.max(2, current - half);
150
- let end = Math.min(total - 1, current + half);
151
-
152
- // adjust window when near the boundaries
153
- if (current - 1 <= half) {
154
- start = 2;
155
- end = 1 + visiblePages;
156
- }
157
- if (total - current <= half) {
158
- end = total - 1;
159
- start = total - visiblePages;
160
- }
161
-
162
- if (start > 2) {
163
- // compute left jump (midpoint of hidden range 2..start-1)
164
- const leftHiddenStart = 2;
165
- const leftHiddenEnd = start - 1;
166
- const leftJump = Math.max(leftHiddenStart, Math.floor((leftHiddenStart + leftHiddenEnd) / 2));
167
- pagesArr.push({ name: '…', active: false, ellipsis: 'left', jump: leftJump });
168
- }
169
-
170
- for (let i = start; i <= end; i++) pagesArr.push({ name: String(i), active: i === current });
171
-
172
- if (end < total - 1) {
173
- // compute right jump (midpoint of hidden range end+1..total-1)
174
- const rightHiddenStart = end + 1;
175
- const rightHiddenEnd = total - 1;
176
- const rightJump = Math.min(
177
- rightHiddenEnd,
178
- Math.ceil((rightHiddenStart + rightHiddenEnd) / 2),
179
- );
180
- pagesArr.push({ name: '…', active: false, ellipsis: 'right', jump: rightJump });
181
- }
182
-
183
- // always show last
184
- pagesArr.push({ name: String(total), active: current === total });
185
-
186
- return pagesArr;
187
- });
188
-
189
- // No external tooltip component in use; we rely on native title attribute for ellipses
190
- </script>
191
-
192
- <div class="w-full">
193
- <div class="flex items-center w-full gap-3 px-0 py-0">
194
- <!-- Search field on the left -->
195
- <div class="flex-1">
196
- <div class="flex items-center gap-2">
197
- <!-- Styled button to open/close filters modal -->
198
- <div class="flex items-center gap-1 relative">
199
- <button
200
- type="button"
201
- class={'inline-flex items-center gap-2 px-3 py-1 text-sm rounded-md border transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 ' +
202
- (filtersVisible
203
- ? 'bg-slate-100 dark:bg-slate-700 border-slate-300 dark:border-slate-600 text-slate-900 dark:text-slate-100'
204
- : 'bg-transparent border-transparent text-slate-700 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700')}
205
- aria-pressed={filtersVisible}
206
- aria-label="Toggle filters"
207
- title="Filters"
208
- onclick={() => {
209
- filtersVisible = !filtersVisible;
210
- }}
211
- >
212
- <!-- Funnel icon (Flowbite icon component) -->
213
- <FilterOutline class="h-4 w-4 shrink-0" />
214
- <span class="text-sm">Filters</span>
215
- </button>
216
-
217
- {#if activeFilterCount > 0}
218
- <!-- positioned badge -->
219
- <span
220
- class="absolute top-0 right-0 -translate-y-1/2 translate-x-1/2 inline-flex items-center justify-center h-5 min-w-[1.25rem] px-1.5 rounded-full text-white text-xs font-medium"
221
- style="background-color: var(--vtable-color-accent);">{activeFilterCount}</span
222
- >
223
- {/if}
224
- </div>
225
-
226
- <Search
227
- size="sm"
228
- bind:value={search}
229
- placeholder="Search..."
230
- clearable
231
- clearableOnClick={() => {
232
- search = '';
233
- if (onsearch !== undefined) {
234
- onsearch({ search: '' });
235
- }
236
- }}
237
- />
238
- </div>
239
- </div>
240
-
241
- <!-- Range count badge + per-page selector -->
242
- <div class="flex-2 flex items-center gap-2">
243
- <Badge rounded color="gray">
244
- Showing {startItem} to {endItem} of {totalItems}
245
- </Badge>
246
-
247
- <!-- Per-page selector -->
248
- <div class="flex items-center gap-2">
249
- <Select id={perPageSelectId} size="sm" items={perPageOptions} bind:value={perPage} />
250
- </div>
251
- </div>
252
-
253
- <!-- Pagination navigation -->
254
- <PaginationBar {pages} {currentPage} onpage={goTo} />
255
- </div>
256
- </div>
257
-
258
- <FiltersModal
259
- bind:open={filtersVisible}
260
- {columnFilters}
261
- {activeFilters}
262
- {onfilter}
263
- {direction}
264
- {showCounts}
265
- {className}
266
- />
267
-
268
- <style>
269
- @import '../lib/dist/styles.css';
270
- </style>
@@ -1,287 +0,0 @@
1
- /* ── Theming contract — override these on :root or any wrapper element ── */
2
- :root {
3
- --vtable-color-accent: #16a34a; /* green-600 */
4
- --vtable-color-accent-light: #dcfce7; /* green-100 */
5
- --vtable-color-accent-border: #22c55e; /* green-500 */
6
- --vtable-color-accent-text: #15803d; /* green-700 */
7
-
8
- --vtable-color-danger: #dc2626; /* red-600 */
9
- --vtable-color-info: #3b82f6; /* blue-500 */
10
-
11
- --vtable-radius: 0.375rem; /* rounded-md */
12
- --vtable-row-height: 2.25rem; /* default row height hint for virtual scroll */
13
- }
14
-
15
- .virtual-data-table {
16
- display: block;
17
- min-width: 0;
18
- height: 100%;
19
- overflow: auto;
20
- }
21
-
22
- .datatable-table {
23
- display: table;
24
- width: 100%;
25
- min-width: 0;
26
- border-collapse: collapse;
27
- box-sizing: border-box;
28
- font-size: 0.875rem; /* 14px - smaller, more compact */
29
- font-family:
30
- -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
31
- line-height: 1.4;
32
- border: none;
33
- }
34
-
35
- /* Sticky header support */
36
- .datatable-table thead.sticky-header {
37
- position: sticky;
38
- top: 0;
39
- z-index: 20;
40
- }
41
-
42
- /* Header visual styles - only bottom border */
43
- .datatable-table thead th {
44
- background-color: #f8f9fa;
45
- font-size: 0.8125rem; /* 13px */
46
- font-weight: 600;
47
- text-transform: uppercase;
48
- letter-spacing: 0.025em;
49
- border-top: none;
50
- border-left: none;
51
- border-right: none;
52
- border-bottom: 1px solid #dee2e6;
53
- padding: 0.5rem 0.5rem;
54
- text-align: left;
55
- white-space: normal; /* Allow header text to wrap */
56
- overflow: visible;
57
- box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1); /* Keep border visible on scroll */
58
- }
59
-
60
- /* Row striping - alternating colors */
61
- /* VirtualList may wrap rows, so we target tr directly */
62
- .datatable-table tbody tr:nth-of-type(even) {
63
- background-color: #ffffff;
64
- }
65
-
66
- .datatable-table tbody tr:nth-of-type(odd) {
67
- background-color: #f8f9fa;
68
- }
69
-
70
- /* Also ensure tr elements themselves have background */
71
- .datatable-table tbody tr {
72
- background-color: inherit;
73
- }
74
-
75
- /* Hover effect */
76
- .datatable-table tbody tr:hover {
77
- background-color: #e7f1ff !important;
78
- transition: background-color 0.15s ease-in-out;
79
- }
80
-
81
- /* Selesky-500 row overrides striping and hover */
82
- .datatable-table tbody tr.selected-row {
83
- background-color: #e0f2fe !important; /* light sky background */
84
- box-shadow: inset 4px 0 0 0 #0369a1; /* left accent */
85
- }
86
-
87
- .dark .datatable-table tbody tr.selected-row {
88
- /* brighter, higher-contrast selection in dark mode */
89
- background-color: rgba(14, 165, 233, 0.22) !important; /* stronger visible blue */
90
- box-shadow: inset 6px 0 0 0 rgba(56, 189, 248, 0.85); /* stronger left accent */
91
- transition:
92
- background-color 0.12s ease-in-out,
93
- box-shadow 0.12s ease-in-out;
94
- }
95
-
96
- /* Also apply to cells to defeat any cell-level background from striping and ensure readable text */
97
- .datatable-table tbody tr.selected-row td {
98
- background-color: #e0f2fe !important;
99
- }
100
-
101
- .dark .datatable-table tbody tr.selected-row td {
102
- background-color: rgba(14, 165, 233, 0.22) !important;
103
- color: #f0fbff !important; /* ensure high-contrast, very light text */
104
- border-bottom-color: rgba(255, 255, 255, 0.06);
105
- }
106
-
107
- /* Body cell defaults - only bottom border */
108
- .datatable-table tbody td {
109
- padding: 0.5rem 0.5rem;
110
- border-top: none;
111
- border-left: none;
112
- border-right: none;
113
- border-bottom: 1px solid #e9ecef;
114
- vertical-align: middle;
115
- text-align: left;
116
- font-size: 0.875rem; /* 14px */
117
- overflow: hidden;
118
- white-space: normal; /* Allow wrapping by default */
119
- word-wrap: break-word;
120
- box-sizing: border-box;
121
- }
122
-
123
- /* No-wrap variant for cells that should truncate */
124
- .datatable-table tbody td.nowrap {
125
- white-space: nowrap;
126
- text-overflow: ellipsis;
127
- }
128
-
129
- /* Compact variant */
130
- .datatable-table.compact tbody td {
131
- padding: 0.1rem 0.3rem;
132
- }
133
-
134
- /* Header content alignment helper */
135
- .datatable-table thead th > div {
136
- display: inline-flex;
137
- align-items: center;
138
- gap: 0.25rem;
139
- }
140
-
141
- /* Allow certain long cells to wrap fully if explicitly requested */
142
- .datatable-table td.wrap {
143
- white-space: normal;
144
- word-break: break-word;
145
- }
146
-
147
- /* Dark mode variants for the datatable */
148
- .dark .datatable-table thead th {
149
- background-color: #1f2937;
150
- border-bottom-color: #374151;
151
- color: #f9fafb;
152
- box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.3); /* Darker shadow for dark mode */
153
- }
154
-
155
- .dark .datatable-table tbody tr:nth-of-type(even) {
156
- background-color: #111827;
157
- }
158
-
159
- .dark .datatable-table tbody tr:nth-of-type(odd) {
160
- background-color: #1f2937;
161
- }
162
-
163
- .dark .datatable-table tbody tr:hover {
164
- background-color: #374151 !important;
165
- }
166
-
167
- .dark .datatable-table tbody td {
168
- border-bottom-color: #374151;
169
- color: #e5e7eb;
170
- }
171
-
172
- /* Shared control styles (inputs, pagination, column filters) used by page components */
173
- .datatable-input {
174
- padding: 0.375rem 0.75rem;
175
- font-size: 0.95rem;
176
- border: 1px solid #ced4da;
177
- border-radius: 0.25rem;
178
- }
179
-
180
- .datatable-input:focus {
181
- border-color: #86b7fe;
182
- outline: 0;
183
- box-shadow: 0 0 0.1rem 0.2rem rgba(13, 110, 253, 0.25);
184
- }
185
-
186
- .search-filtering-row {
187
- background-color: #f8f9fa;
188
- }
189
-
190
- .search-filtering-row th {
191
- padding: 0.1rem 0.1rem;
192
- border-bottom: 1px solid #dee2e6;
193
- }
194
-
195
- .column-filter {
196
- width: 95%;
197
- padding: 0.2rem 0.25rem;
198
- font-size: 0.8rem;
199
- border: 1px solid #ced4da;
200
- border-radius: 0.25rem;
201
- }
202
-
203
- .column-filter::placeholder {
204
- color: #adb5bd;
205
- font-size: 0.75rem;
206
- font-style: italic;
207
- }
208
-
209
- .datatable-pagination {
210
- display: flex;
211
- gap: 0.1rem;
212
- margin-top: 0.6rem;
213
- }
214
-
215
- .datatable-pagination li a,
216
- .datatable-pagination li button {
217
- padding: 0.3rem 0.7rem;
218
- border: 1px solid #dee2e6;
219
- border-radius: 0;
220
- background-color: #fff;
221
- color: #0d6efd;
222
- text-decoration: none;
223
- }
224
-
225
- .datatable-pagination li a:hover,
226
- .datatable-pagination li button:hover {
227
- background-color: #e9ecef;
228
- }
229
-
230
- .datatable-pagination .datatable-active a,
231
- .datatable-pagination .datatable-active button {
232
- background-color: #0d6efd;
233
- border-color: #0d6efd;
234
- color: #fff;
235
- }
236
-
237
- /* Force the active/current page to use the accent tokens (override flowbite's primary/orange).
238
- Intentionally specific to reliably win over third-party rules. */
239
- [data-pagination-id] ul li [aria-current='page'] {
240
- background-color: var(--vtable-color-accent-light) !important;
241
- color: var(--vtable-color-accent) !important;
242
- border-color: transparent !important;
243
- }
244
-
245
- /* Ensure Prev/Next controls visually match the numeric buttons' border and rhythm
246
- (keeps border, radius and min-width consistent). This is minimal and won't
247
- override hover/text colors managed by Tailwind classes applied at runtime. */
248
- [data-pagination-id] ul li:first-child > button,
249
- [data-pagination-id] ul li:first-child > a,
250
- [data-pagination-id] ul li:last-child > button,
251
- [data-pagination-id] ul li:last-child > a {
252
- border: 1px solid #d1d5db; /* border-gray-300 */
253
- min-width: 2rem; /* align with numeric buttons' min width */
254
- background: transparent;
255
- padding: 0.25rem 0.5rem; /* keep comfortable hit area */
256
- }
257
-
258
- /* Light mode: hover/focus for Prev/Next match numeric button hover */
259
- [data-pagination-id] ul li:first-child > button:hover,
260
- [data-pagination-id] ul li:first-child > a:hover,
261
- [data-pagination-id] ul li:last-child > button:hover,
262
- [data-pagination-id] ul li:last-child > a:hover,
263
- [data-pagination-id] ul li:first-child > button:focus,
264
- [data-pagination-id] ul li:first-child > a:focus,
265
- [data-pagination-id] ul li:last-child > button:focus,
266
- [data-pagination-id] ul li:last-child > a:focus {
267
- background-color: #f3f4f6; /* tailwind gray-100 */
268
- outline: none;
269
- }
270
-
271
- /* Ellipsis styling: make clickable and visually lighter (JS sets data-ellipsis/data-jump) */
272
- [data-pagination-id] ul li [data-ellipsis='true'] {
273
- cursor: pointer;
274
- background: transparent !important;
275
- color: inherit;
276
- opacity: 0.9;
277
- font-size: 0.875rem;
278
- padding: 0.25rem 0.5rem;
279
- }
280
-
281
- /* Chevron sizing for Prev/Next so icons sit comfortably in the buttons */
282
- [data-pagination-id] ul li:first-child svg,
283
- [data-pagination-id] ul li:last-child svg {
284
- height: 1.25rem;
285
- width: 1.25rem;
286
- display: block;
287
- }
File without changes
File without changes