svelte-comp 1.3.3 → 1.3.6
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/LICENSE.md +21 -21
- package/README.md +101 -100
- package/dist/App.svelte +507 -507
- package/dist/Container.svelte +59 -59
- package/dist/app.css +234 -235
- package/dist/app.d.ts +10 -0
- package/dist/lib/Accordion.svelte +155 -155
- package/dist/lib/Badge.svelte +44 -44
- package/dist/lib/Button.svelte +185 -170
- package/dist/lib/Calendar.svelte +384 -384
- package/dist/lib/Card.svelte +103 -103
- package/dist/lib/Carousel.svelte +293 -293
- package/dist/lib/Carousel.svelte.d.ts +1 -1
- package/dist/lib/CheckBox.svelte +210 -210
- package/dist/lib/CodeView.svelte +308 -307
- package/dist/lib/ColorPicker.svelte +159 -159
- package/dist/lib/ContextMenu.svelte +328 -322
- package/dist/lib/DatePicker.svelte +246 -246
- package/dist/lib/Dialog.svelte +233 -233
- package/dist/lib/Field.svelte +299 -299
- package/dist/lib/FilePicker.svelte +295 -240
- package/dist/lib/FilePicker.svelte.d.ts +6 -1
- package/dist/lib/Form.svelte +438 -438
- package/dist/lib/Hamburger.svelte +217 -217
- package/dist/lib/InstallPWA.svelte +94 -94
- package/dist/lib/Menu.svelte +623 -623
- package/dist/lib/NoticeBase.svelte +140 -140
- package/dist/lib/PaginatedCard.svelte +73 -73
- package/dist/lib/Pagination.svelte +119 -119
- package/dist/lib/PrimaryColorSelect.svelte +111 -111
- package/dist/lib/ProgressBar.svelte +141 -141
- package/dist/lib/ProgressCircle.svelte +190 -190
- package/dist/lib/Radio.svelte +189 -189
- package/dist/lib/SearchInput.svelte +104 -104
- package/dist/lib/Select.svelte +524 -524
- package/dist/lib/Slider.svelte +253 -253
- package/dist/lib/Splitter.svelte +159 -150
- package/dist/lib/Switch.svelte +168 -167
- package/dist/lib/Table.svelte +299 -299
- package/dist/lib/Tabs.svelte +213 -213
- package/dist/lib/ThemeToggle.svelte +128 -127
- package/dist/lib/TimePicker.svelte +312 -312
- package/dist/lib/TimePickerNew.svelte +634 -0
- package/dist/lib/TimePickerNew.svelte.d.ts +49 -0
- package/dist/lib/Toast.svelte +123 -123
- package/dist/lib/Tooltip.svelte +110 -110
- package/dist/lib/Topbar.svelte +107 -107
- package/dist/lib/__tests__/Accordion.test.d.ts +1 -0
- package/dist/lib/__tests__/Accordion.test.js +171 -0
- package/dist/lib/__tests__/Badge.test.d.ts +1 -0
- package/dist/lib/__tests__/Badge.test.js +41 -0
- package/dist/lib/__tests__/Button.test.d.ts +1 -0
- package/dist/lib/__tests__/Button.test.js +269 -0
- package/dist/lib/__tests__/Calendar.test.d.ts +1 -0
- package/dist/lib/__tests__/Calendar.test.js +171 -0
- package/dist/lib/__tests__/Card.test.d.ts +1 -0
- package/dist/lib/__tests__/Card.test.js +148 -0
- package/dist/lib/__tests__/Carousel.test.d.ts +1 -0
- package/dist/lib/__tests__/Carousel.test.js +439 -0
- package/dist/lib/__tests__/CheckBox.test.d.ts +1 -0
- package/dist/lib/__tests__/CheckBox.test.js +152 -0
- package/dist/lib/__tests__/CodeView.test.d.ts +1 -0
- package/dist/lib/__tests__/CodeView.test.js +157 -0
- package/dist/lib/__tests__/ColorPicker.test.d.ts +1 -0
- package/dist/lib/__tests__/ColorPicker.test.js +93 -0
- package/dist/lib/__tests__/ContextMenu.test.d.ts +1 -0
- package/dist/lib/__tests__/ContextMenu.test.js +67 -0
- package/dist/lib/__tests__/DatePicker.test.d.ts +1 -0
- package/dist/lib/__tests__/DatePicker.test.js +108 -0
- package/dist/lib/__tests__/Dialog.test.d.ts +1 -0
- package/dist/lib/__tests__/Dialog.test.js +183 -0
- package/dist/lib/__tests__/Field.test.d.ts +1 -0
- package/dist/lib/__tests__/Field.test.js +190 -0
- package/dist/lib/__tests__/FilePicker.test.d.ts +1 -0
- package/dist/lib/__tests__/FilePicker.test.js +179 -0
- package/dist/lib/__tests__/Form.integration.test.d.ts +1 -0
- package/dist/lib/__tests__/Form.integration.test.js +158 -0
- package/dist/lib/__tests__/Form.test.d.ts +1 -0
- package/dist/lib/__tests__/Form.test.js +463 -0
- package/dist/lib/__tests__/Hamburger.test.d.ts +1 -0
- package/dist/lib/__tests__/Hamburger.test.js +161 -0
- package/dist/lib/__tests__/InstallPWA.test.d.ts +1 -0
- package/dist/lib/__tests__/InstallPWA.test.js +15 -0
- package/dist/lib/__tests__/Menu.test.d.ts +1 -0
- package/dist/lib/__tests__/Menu.test.js +285 -0
- package/dist/lib/__tests__/NoticeBase.test.d.ts +1 -0
- package/dist/lib/__tests__/NoticeBase.test.js +60 -0
- package/dist/lib/__tests__/PaginatedCard.test.d.ts +1 -0
- package/dist/lib/__tests__/PaginatedCard.test.js +89 -0
- package/dist/lib/__tests__/Pagination.test.d.ts +1 -0
- package/dist/lib/__tests__/Pagination.test.js +168 -0
- package/dist/lib/__tests__/PrimaryColorSelect.test.d.ts +1 -0
- package/dist/lib/__tests__/PrimaryColorSelect.test.js +92 -0
- package/dist/lib/__tests__/ProgressBar.test.d.ts +1 -0
- package/dist/lib/__tests__/ProgressBar.test.js +69 -0
- package/dist/lib/__tests__/ProgressCircle.test.d.ts +1 -0
- package/dist/lib/__tests__/ProgressCircle.test.js +71 -0
- package/dist/lib/__tests__/Radio.test.d.ts +1 -0
- package/dist/lib/__tests__/Radio.test.js +127 -0
- package/dist/lib/__tests__/SearchInput.test.d.ts +1 -0
- package/dist/lib/__tests__/SearchInput.test.js +80 -0
- package/dist/lib/__tests__/Select.test.d.ts +1 -0
- package/dist/lib/__tests__/Select.test.js +408 -0
- package/dist/lib/__tests__/Slider.test.d.ts +1 -0
- package/dist/lib/__tests__/Slider.test.js +213 -0
- package/dist/lib/__tests__/Splitter.test.d.ts +1 -0
- package/dist/lib/__tests__/Splitter.test.js +87 -0
- package/dist/lib/__tests__/Switch.test.d.ts +1 -0
- package/dist/lib/__tests__/Switch.test.js +97 -0
- package/dist/lib/__tests__/Table.test.d.ts +1 -0
- package/dist/lib/__tests__/Table.test.js +349 -0
- package/dist/lib/__tests__/Tabs.test.d.ts +1 -0
- package/dist/lib/__tests__/Tabs.test.js +262 -0
- package/dist/lib/__tests__/ThemeToggle.test.d.ts +1 -0
- package/dist/lib/__tests__/ThemeToggle.test.js +84 -0
- package/dist/lib/__tests__/TimePicker.test.d.ts +1 -0
- package/dist/lib/__tests__/TimePicker.test.js +146 -0
- package/dist/lib/__tests__/TimePickerNew.test.d.ts +1 -0
- package/dist/lib/__tests__/TimePickerNew.test.js +322 -0
- package/dist/lib/__tests__/Toast.test.d.ts +1 -0
- package/dist/lib/__tests__/Toast.test.js +135 -0
- package/dist/lib/__tests__/Tooltip.test.d.ts +1 -0
- package/dist/lib/__tests__/Tooltip.test.js +171 -0
- package/dist/lib/__tests__/Topbar.test.d.ts +1 -0
- package/dist/lib/__tests__/Topbar.test.js +25 -0
- package/dist/lib/__tests__/setupLangContext.d.ts +1 -0
- package/dist/lib/__tests__/setupLangContext.js +65 -0
- package/dist/lib/__tests__/storage.test.d.ts +1 -0
- package/dist/lib/__tests__/storage.test.js +124 -0
- package/dist/lib/__tests__/utils.test.d.ts +1 -0
- package/dist/lib/__tests__/utils.test.js +11 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/lang.d.ts +4 -0
- package/dist/lib/lang.js +4 -0
- package/dist/styles.css +234 -232
- package/dist/utils/index.js +15 -4
- package/package.json +52 -52
package/dist/lib/Table.svelte
CHANGED
|
@@ -1,299 +1,299 @@
|
|
|
1
|
-
<!-- src/lib/Table.svelte -->
|
|
2
|
-
<script lang="ts">
|
|
3
|
-
/**
|
|
4
|
-
* @component Table
|
|
5
|
-
* @description Sortable table with optional zebra striping, sticky header, and external pagination. Compact variants (`dense`, `list`) shrink horizontally to fit content.
|
|
6
|
-
*
|
|
7
|
-
* @prop columns {readonly Column<T>[]} - Column definitions with labels and keys
|
|
8
|
-
* @default []
|
|
9
|
-
*
|
|
10
|
-
* @prop rows {readonly T[]} - Row data objects
|
|
11
|
-
* @default []
|
|
12
|
-
*
|
|
13
|
-
* @prop class {string} - Custom classes for the table container
|
|
14
|
-
* @default ""
|
|
15
|
-
*
|
|
16
|
-
* @prop variant {TableVariant} - Visual style variant
|
|
17
|
-
* @options default|dense|list|noBorder|noTitle|zebra
|
|
18
|
-
* @default default
|
|
19
|
-
*
|
|
20
|
-
* @prop stickyHeader {boolean} - Makes the header row sticky
|
|
21
|
-
* @default false
|
|
22
|
-
*
|
|
23
|
-
* @prop sz {SizeKey} - Size preset for spacing and text
|
|
24
|
-
* @options xs|sm|md|lg|xl
|
|
25
|
-
* @default md
|
|
26
|
-
*
|
|
27
|
-
* @prop pagination {{ currentPage: number; totalPages: number; onPageChange: (p: number) => void }} - External pagination controls
|
|
28
|
-
*
|
|
29
|
-
* @note Click a column header to toggle ascending or descending.
|
|
30
|
-
* @note In `dense` and `list` the table uses content width (`inline-table` + `w-fit`).
|
|
31
|
-
* @note Column `width` is applied only in non-compact variants (`default`, `zebra`, `noBorder`, `noTitle`).
|
|
32
|
-
* @note `list` hides the header row.
|
|
33
|
-
* @note Integrates with `Pagination.svelte` via the `pagination` prop.
|
|
34
|
-
* @note Works in dark mode and supports keyboard sorting (Enter or Space on headers).
|
|
35
|
-
*/
|
|
36
|
-
import Pagination from "./Pagination.svelte";
|
|
37
|
-
import type {
|
|
38
|
-
CellAlign,
|
|
39
|
-
Column,
|
|
40
|
-
SizeKey,
|
|
41
|
-
TableRow,
|
|
42
|
-
TableVariant,
|
|
43
|
-
} from "./types";
|
|
44
|
-
import { TEXT } from "./types";
|
|
45
|
-
import { cx } from "../utils";
|
|
46
|
-
|
|
47
|
-
type Props<T extends TableRow> = {
|
|
48
|
-
columns: readonly Column<T>[];
|
|
49
|
-
rows: readonly T[];
|
|
50
|
-
class?: string;
|
|
51
|
-
variant?: TableVariant;
|
|
52
|
-
stickyHeader?: boolean;
|
|
53
|
-
sz?: SizeKey;
|
|
54
|
-
pagination?: {
|
|
55
|
-
currentPage: number;
|
|
56
|
-
totalPages: number;
|
|
57
|
-
onPageChange: (page: number) => void;
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
let {
|
|
62
|
-
columns = [],
|
|
63
|
-
rows = [],
|
|
64
|
-
class: externalClass = "",
|
|
65
|
-
variant = "default",
|
|
66
|
-
stickyHeader = false,
|
|
67
|
-
sz = "md",
|
|
68
|
-
pagination,
|
|
69
|
-
}: Props<TableRow> = $props();
|
|
70
|
-
|
|
71
|
-
let sortKey = $state<keyof TableRow | null>(null);
|
|
72
|
-
let sortDir = $state<"asc" | "desc" | null>(null);
|
|
73
|
-
|
|
74
|
-
function toggleSort(col: Column<TableRow>) {
|
|
75
|
-
const key = col.key;
|
|
76
|
-
if (sortKey !== key) {
|
|
77
|
-
sortKey = key;
|
|
78
|
-
sortDir = "asc";
|
|
79
|
-
} else {
|
|
80
|
-
sortDir = sortDir === "asc" ? "desc" : "asc";
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function cmp(a: unknown, b: unknown): number {
|
|
85
|
-
const an = a == null || a === "";
|
|
86
|
-
const bn = b == null || b === "";
|
|
87
|
-
if (an && bn) return 0;
|
|
88
|
-
if (an) return 1;
|
|
89
|
-
if (bn) return -1;
|
|
90
|
-
|
|
91
|
-
const na = Number(a);
|
|
92
|
-
const nb = Number(b);
|
|
93
|
-
const aNum = !Number.isNaN(na);
|
|
94
|
-
const bNum = !Number.isNaN(nb);
|
|
95
|
-
|
|
96
|
-
if (aNum && bNum) return na - nb;
|
|
97
|
-
|
|
98
|
-
return String(a).localeCompare(String(b), undefined, {
|
|
99
|
-
numeric: true,
|
|
100
|
-
sensitivity: "base",
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const variants = {
|
|
105
|
-
default: {
|
|
106
|
-
table: "border border-[var(--border-color-default)]",
|
|
107
|
-
header:
|
|
108
|
-
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
109
|
-
body: "",
|
|
110
|
-
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
111
|
-
row: "hover:bg-[var(--color-bg-hover)]",
|
|
112
|
-
padY: "py-2",
|
|
113
|
-
},
|
|
114
|
-
dense: {
|
|
115
|
-
table: "border border-[var(--border-color-default)]",
|
|
116
|
-
header:
|
|
117
|
-
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
118
|
-
body: "",
|
|
119
|
-
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
120
|
-
row: "hover:bg-[var(--color-bg-hover)]",
|
|
121
|
-
padY: "py-1.5",
|
|
122
|
-
},
|
|
123
|
-
list: {
|
|
124
|
-
table: "border-none",
|
|
125
|
-
header: "hidden",
|
|
126
|
-
body: "",
|
|
127
|
-
cell: "border-none",
|
|
128
|
-
row: "hover:bg-[var(--color-bg-hover)]",
|
|
129
|
-
padY: "py-1.5",
|
|
130
|
-
},
|
|
131
|
-
noBorder: {
|
|
132
|
-
table: "border-none",
|
|
133
|
-
header: "bg-transparent border-none",
|
|
134
|
-
body: "",
|
|
135
|
-
cell: "border-none",
|
|
136
|
-
row: "hover:bg-[var(--color-bg-hover)]",
|
|
137
|
-
padY: "py-2",
|
|
138
|
-
},
|
|
139
|
-
zebra: {
|
|
140
|
-
table: "border border-[var(--border-color-default)]",
|
|
141
|
-
header:
|
|
142
|
-
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
143
|
-
body: "",
|
|
144
|
-
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
145
|
-
row: "even:bg-[var(--color-bg-muted)] hover:bg-[var(--color-bg-hover)]",
|
|
146
|
-
padY: "py-2",
|
|
147
|
-
},
|
|
148
|
-
noTitle: {
|
|
149
|
-
table: "border border-[var(--border-color-default)]",
|
|
150
|
-
header: "hidden",
|
|
151
|
-
body: "",
|
|
152
|
-
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
153
|
-
row: "hover:bg-[var(--color-bg-hover)]",
|
|
154
|
-
padY: "py-2",
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const displayRows = $derived.by(() => {
|
|
159
|
-
const base = Array.isArray(rows) ? rows : [];
|
|
160
|
-
if (!sortKey || !sortDir) return base as TableRow[];
|
|
161
|
-
const key = sortKey;
|
|
162
|
-
const withIdx = base.map((r, i) => [r as TableRow, i] as const);
|
|
163
|
-
withIdx.sort(([r1, i1], [r2, i2]) => {
|
|
164
|
-
const res = cmp(r1[key], r2[key]);
|
|
165
|
-
if (res !== 0) return sortDir === "asc" ? res : -res;
|
|
166
|
-
return i1 - i2;
|
|
167
|
-
});
|
|
168
|
-
return withIdx.map(([r]) => r);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const alignTd = (a?: CellAlign) =>
|
|
172
|
-
a === "end" ? "text-right" : a === "start" ? "text-left" : "text-center";
|
|
173
|
-
|
|
174
|
-
const alignTh = (a?: CellAlign) =>
|
|
175
|
-
a === "end"
|
|
176
|
-
? "justify-end"
|
|
177
|
-
: a === "center"
|
|
178
|
-
? "justify-center"
|
|
179
|
-
: "justify-start";
|
|
180
|
-
|
|
181
|
-
const wrapperClass = $derived(cx("overflow-x-auto", externalClass));
|
|
182
|
-
const tableTextSize = $derived(TEXT[sz]);
|
|
183
|
-
const currentVariant = $derived(variant);
|
|
184
|
-
const isCompact = $derived(
|
|
185
|
-
currentVariant === "list" || currentVariant === "dense"
|
|
186
|
-
);
|
|
187
|
-
const variantStyles = $derived(variants[currentVariant] ?? variants.default);
|
|
188
|
-
|
|
189
|
-
const tableClass = $derived(
|
|
190
|
-
cx(
|
|
191
|
-
isCompact ? "inline-table w-fit" : "min-w-full",
|
|
192
|
-
"border-collapse",
|
|
193
|
-
variantStyles.table,
|
|
194
|
-
tableTextSize,
|
|
195
|
-
"text-[var(--color-text-default)]"
|
|
196
|
-
)
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
const getRowKey = (row: TableRow, idx: number) => {
|
|
200
|
-
const candidate =
|
|
201
|
-
typeof row.id === "string" || typeof row.id === "number"
|
|
202
|
-
? row.id
|
|
203
|
-
: typeof row.key === "string" || typeof row.key === "number"
|
|
204
|
-
? row.key
|
|
205
|
-
: idx;
|
|
206
|
-
return String(candidate);
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const getColumnKey = (col: Column<TableRow>, idx: number) =>
|
|
210
|
-
String(col.key ?? idx);
|
|
211
|
-
</script>
|
|
212
|
-
|
|
213
|
-
<div class={wrapperClass} tabindex="-1">
|
|
214
|
-
<table class={tableClass}>
|
|
215
|
-
<thead class={variantStyles.header}>
|
|
216
|
-
{#if currentVariant !== "noTitle" && currentVariant !== "list"}
|
|
217
|
-
<tr class={cx(stickyHeader && "sticky top-0 z-10")}>
|
|
218
|
-
{#each columns as col, idx (getColumnKey(col, idx))}
|
|
219
|
-
<th
|
|
220
|
-
style={!isCompact && col.width ? `width:${col.width}` : undefined}
|
|
221
|
-
class={cx(
|
|
222
|
-
"px-3 font-semibold",
|
|
223
|
-
variantStyles.padY,
|
|
224
|
-
alignTd(col.align)
|
|
225
|
-
)}
|
|
226
|
-
aria-sort={sortKey === col.key
|
|
227
|
-
? sortDir === "asc"
|
|
228
|
-
? "ascending"
|
|
229
|
-
: "descending"
|
|
230
|
-
: "none"}
|
|
231
|
-
scope="col"
|
|
232
|
-
>
|
|
233
|
-
<button
|
|
234
|
-
type="button"
|
|
235
|
-
class={cx(
|
|
236
|
-
"flex w-full items-center gap-2 select-none hover:bg-[var(--color-bg-hover)] rounded-none px-1 py-0.5 border border-transparent focus-visible:border-[var(--border-color-focus)] focus-visible:ring-2 focus-visible:ring-[var(--border-color-focus)] focus-visible:outline-none",
|
|
237
|
-
alignTh(col.align)
|
|
238
|
-
)}
|
|
239
|
-
onclick={() => toggleSort(col)}
|
|
240
|
-
onkeydown={(e) => {
|
|
241
|
-
if (e.key === "Enter" || e.key === " ") {
|
|
242
|
-
e.preventDefault();
|
|
243
|
-
toggleSort(col);
|
|
244
|
-
}
|
|
245
|
-
}}
|
|
246
|
-
aria-label={`Sort by ${col.label}`}
|
|
247
|
-
>
|
|
248
|
-
<span class={cx("truncate min-w-0", alignTd(col.align))}>
|
|
249
|
-
{col.label}
|
|
250
|
-
</span>
|
|
251
|
-
<svg
|
|
252
|
-
class={cx(
|
|
253
|
-
"h-4 w-4 opacity-60 transition-transform",
|
|
254
|
-
sortKey === col.key && sortDir === "desc" && "rotate-180"
|
|
255
|
-
)}
|
|
256
|
-
viewBox="0 0 20 20"
|
|
257
|
-
aria-hidden="true"
|
|
258
|
-
>
|
|
259
|
-
<path
|
|
260
|
-
d="M5.23 11.77a.75.75 0 0 1 0-1.06l4-4a.75.75 0 0 1 1.06 0l4 4a.75.75 0 1 1-1.06 1.06L10 8.31l-3.77 3.46a.75.75 0 0 1-1 0z"
|
|
261
|
-
fill="currentColor"
|
|
262
|
-
/>
|
|
263
|
-
</svg>
|
|
264
|
-
</button>
|
|
265
|
-
</th>
|
|
266
|
-
{/each}
|
|
267
|
-
</tr>
|
|
268
|
-
{/if}
|
|
269
|
-
</thead>
|
|
270
|
-
|
|
271
|
-
<tbody class={variantStyles.body}>
|
|
272
|
-
{#each displayRows as row, rowIdx (getRowKey(row, rowIdx))}
|
|
273
|
-
<tr class={cx("first:[&>td]:border-t-0", variantStyles.row)}>
|
|
274
|
-
{#each columns as col, idx (getColumnKey(col, idx))}
|
|
275
|
-
<td
|
|
276
|
-
style={!isCompact && col.width ? `width:${col.width}` : undefined}
|
|
277
|
-
class={cx(
|
|
278
|
-
"px-3",
|
|
279
|
-
variantStyles.padY,
|
|
280
|
-
alignTd(col.align),
|
|
281
|
-
variantStyles.cell
|
|
282
|
-
)}
|
|
283
|
-
>
|
|
284
|
-
<div class={cx(isCompact && "break-words whitespace-normal")}>
|
|
285
|
-
{row[col.key]}
|
|
286
|
-
</div>
|
|
287
|
-
</td>
|
|
288
|
-
{/each}
|
|
289
|
-
</tr>
|
|
290
|
-
{/each}
|
|
291
|
-
</tbody>
|
|
292
|
-
</table>
|
|
293
|
-
|
|
294
|
-
{#if pagination && pagination.totalPages > 0}
|
|
295
|
-
<div class="mt-3 flex justify-between items-center">
|
|
296
|
-
<Pagination {...pagination} />
|
|
297
|
-
</div>
|
|
298
|
-
{/if}
|
|
299
|
-
</div>
|
|
1
|
+
<!-- src/lib/Table.svelte -->
|
|
2
|
+
<script lang="ts">
|
|
3
|
+
/**
|
|
4
|
+
* @component Table
|
|
5
|
+
* @description Sortable table with optional zebra striping, sticky header, and external pagination. Compact variants (`dense`, `list`) shrink horizontally to fit content.
|
|
6
|
+
*
|
|
7
|
+
* @prop columns {readonly Column<T>[]} - Column definitions with labels and keys
|
|
8
|
+
* @default []
|
|
9
|
+
*
|
|
10
|
+
* @prop rows {readonly T[]} - Row data objects
|
|
11
|
+
* @default []
|
|
12
|
+
*
|
|
13
|
+
* @prop class {string} - Custom classes for the table container
|
|
14
|
+
* @default ""
|
|
15
|
+
*
|
|
16
|
+
* @prop variant {TableVariant} - Visual style variant
|
|
17
|
+
* @options default|dense|list|noBorder|noTitle|zebra
|
|
18
|
+
* @default default
|
|
19
|
+
*
|
|
20
|
+
* @prop stickyHeader {boolean} - Makes the header row sticky
|
|
21
|
+
* @default false
|
|
22
|
+
*
|
|
23
|
+
* @prop sz {SizeKey} - Size preset for spacing and text
|
|
24
|
+
* @options xs|sm|md|lg|xl
|
|
25
|
+
* @default md
|
|
26
|
+
*
|
|
27
|
+
* @prop pagination {{ currentPage: number; totalPages: number; onPageChange: (p: number) => void }} - External pagination controls
|
|
28
|
+
*
|
|
29
|
+
* @note Click a column header to toggle ascending or descending.
|
|
30
|
+
* @note In `dense` and `list` the table uses content width (`inline-table` + `w-fit`).
|
|
31
|
+
* @note Column `width` is applied only in non-compact variants (`default`, `zebra`, `noBorder`, `noTitle`).
|
|
32
|
+
* @note `list` hides the header row.
|
|
33
|
+
* @note Integrates with `Pagination.svelte` via the `pagination` prop.
|
|
34
|
+
* @note Works in dark mode and supports keyboard sorting (Enter or Space on headers).
|
|
35
|
+
*/
|
|
36
|
+
import Pagination from "./Pagination.svelte";
|
|
37
|
+
import type {
|
|
38
|
+
CellAlign,
|
|
39
|
+
Column,
|
|
40
|
+
SizeKey,
|
|
41
|
+
TableRow,
|
|
42
|
+
TableVariant,
|
|
43
|
+
} from "./types";
|
|
44
|
+
import { TEXT } from "./types";
|
|
45
|
+
import { cx } from "../utils";
|
|
46
|
+
|
|
47
|
+
type Props<T extends TableRow> = {
|
|
48
|
+
columns: readonly Column<T>[];
|
|
49
|
+
rows: readonly T[];
|
|
50
|
+
class?: string;
|
|
51
|
+
variant?: TableVariant;
|
|
52
|
+
stickyHeader?: boolean;
|
|
53
|
+
sz?: SizeKey;
|
|
54
|
+
pagination?: {
|
|
55
|
+
currentPage: number;
|
|
56
|
+
totalPages: number;
|
|
57
|
+
onPageChange: (page: number) => void;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
let {
|
|
62
|
+
columns = [],
|
|
63
|
+
rows = [],
|
|
64
|
+
class: externalClass = "",
|
|
65
|
+
variant = "default",
|
|
66
|
+
stickyHeader = false,
|
|
67
|
+
sz = "md",
|
|
68
|
+
pagination,
|
|
69
|
+
}: Props<TableRow> = $props();
|
|
70
|
+
|
|
71
|
+
let sortKey = $state<keyof TableRow | null>(null);
|
|
72
|
+
let sortDir = $state<"asc" | "desc" | null>(null);
|
|
73
|
+
|
|
74
|
+
function toggleSort(col: Column<TableRow>) {
|
|
75
|
+
const key = col.key;
|
|
76
|
+
if (sortKey !== key) {
|
|
77
|
+
sortKey = key;
|
|
78
|
+
sortDir = "asc";
|
|
79
|
+
} else {
|
|
80
|
+
sortDir = sortDir === "asc" ? "desc" : "asc";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function cmp(a: unknown, b: unknown): number {
|
|
85
|
+
const an = a == null || a === "";
|
|
86
|
+
const bn = b == null || b === "";
|
|
87
|
+
if (an && bn) return 0;
|
|
88
|
+
if (an) return 1;
|
|
89
|
+
if (bn) return -1;
|
|
90
|
+
|
|
91
|
+
const na = Number(a);
|
|
92
|
+
const nb = Number(b);
|
|
93
|
+
const aNum = !Number.isNaN(na);
|
|
94
|
+
const bNum = !Number.isNaN(nb);
|
|
95
|
+
|
|
96
|
+
if (aNum && bNum) return na - nb;
|
|
97
|
+
|
|
98
|
+
return String(a).localeCompare(String(b), undefined, {
|
|
99
|
+
numeric: true,
|
|
100
|
+
sensitivity: "base",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const variants = {
|
|
105
|
+
default: {
|
|
106
|
+
table: "border border-[var(--border-color-default)]",
|
|
107
|
+
header:
|
|
108
|
+
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
109
|
+
body: "",
|
|
110
|
+
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
111
|
+
row: "hover:bg-[var(--color-bg-hover)]",
|
|
112
|
+
padY: "py-2",
|
|
113
|
+
},
|
|
114
|
+
dense: {
|
|
115
|
+
table: "border border-[var(--border-color-default)]",
|
|
116
|
+
header:
|
|
117
|
+
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
118
|
+
body: "",
|
|
119
|
+
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
120
|
+
row: "hover:bg-[var(--color-bg-hover)]",
|
|
121
|
+
padY: "py-1.5",
|
|
122
|
+
},
|
|
123
|
+
list: {
|
|
124
|
+
table: "border-none",
|
|
125
|
+
header: "hidden",
|
|
126
|
+
body: "",
|
|
127
|
+
cell: "border-none",
|
|
128
|
+
row: "hover:bg-[var(--color-bg-hover)]",
|
|
129
|
+
padY: "py-1.5",
|
|
130
|
+
},
|
|
131
|
+
noBorder: {
|
|
132
|
+
table: "border-none",
|
|
133
|
+
header: "bg-transparent border-none",
|
|
134
|
+
body: "",
|
|
135
|
+
cell: "border-none",
|
|
136
|
+
row: "hover:bg-[var(--color-bg-hover)]",
|
|
137
|
+
padY: "py-2",
|
|
138
|
+
},
|
|
139
|
+
zebra: {
|
|
140
|
+
table: "border border-[var(--border-color-default)]",
|
|
141
|
+
header:
|
|
142
|
+
"bg-[var(--color-bg-secondary)] border border-[var(--border-color-default)]",
|
|
143
|
+
body: "",
|
|
144
|
+
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
145
|
+
row: "even:bg-[var(--color-bg-muted)] hover:bg-[var(--color-bg-hover)]",
|
|
146
|
+
padY: "py-2",
|
|
147
|
+
},
|
|
148
|
+
noTitle: {
|
|
149
|
+
table: "border border-[var(--border-color-default)]",
|
|
150
|
+
header: "hidden",
|
|
151
|
+
body: "",
|
|
152
|
+
cell: "border-t border-r last:border-r-0 border-[var(--border-color-default)]",
|
|
153
|
+
row: "hover:bg-[var(--color-bg-hover)]",
|
|
154
|
+
padY: "py-2",
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const displayRows = $derived.by(() => {
|
|
159
|
+
const base = Array.isArray(rows) ? rows : [];
|
|
160
|
+
if (!sortKey || !sortDir) return base as TableRow[];
|
|
161
|
+
const key = sortKey;
|
|
162
|
+
const withIdx = base.map((r, i) => [r as TableRow, i] as const);
|
|
163
|
+
withIdx.sort(([r1, i1], [r2, i2]) => {
|
|
164
|
+
const res = cmp(r1[key], r2[key]);
|
|
165
|
+
if (res !== 0) return sortDir === "asc" ? res : -res;
|
|
166
|
+
return i1 - i2;
|
|
167
|
+
});
|
|
168
|
+
return withIdx.map(([r]) => r);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const alignTd = (a?: CellAlign) =>
|
|
172
|
+
a === "end" ? "text-right" : a === "start" ? "text-left" : "text-center";
|
|
173
|
+
|
|
174
|
+
const alignTh = (a?: CellAlign) =>
|
|
175
|
+
a === "end"
|
|
176
|
+
? "justify-end"
|
|
177
|
+
: a === "center"
|
|
178
|
+
? "justify-center"
|
|
179
|
+
: "justify-start";
|
|
180
|
+
|
|
181
|
+
const wrapperClass = $derived(cx("overflow-x-auto", externalClass));
|
|
182
|
+
const tableTextSize = $derived(TEXT[sz]);
|
|
183
|
+
const currentVariant = $derived(variant);
|
|
184
|
+
const isCompact = $derived(
|
|
185
|
+
currentVariant === "list" || currentVariant === "dense"
|
|
186
|
+
);
|
|
187
|
+
const variantStyles = $derived(variants[currentVariant] ?? variants.default);
|
|
188
|
+
|
|
189
|
+
const tableClass = $derived(
|
|
190
|
+
cx(
|
|
191
|
+
isCompact ? "inline-table w-fit" : "min-w-full",
|
|
192
|
+
"border-collapse",
|
|
193
|
+
variantStyles.table,
|
|
194
|
+
tableTextSize,
|
|
195
|
+
"text-[var(--color-text-default)]"
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const getRowKey = (row: TableRow, idx: number) => {
|
|
200
|
+
const candidate =
|
|
201
|
+
typeof row.id === "string" || typeof row.id === "number"
|
|
202
|
+
? row.id
|
|
203
|
+
: typeof row.key === "string" || typeof row.key === "number"
|
|
204
|
+
? row.key
|
|
205
|
+
: idx;
|
|
206
|
+
return String(candidate);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const getColumnKey = (col: Column<TableRow>, idx: number) =>
|
|
210
|
+
String(col.key ?? idx);
|
|
211
|
+
</script>
|
|
212
|
+
|
|
213
|
+
<div class={wrapperClass} tabindex="-1">
|
|
214
|
+
<table class={tableClass}>
|
|
215
|
+
<thead class={variantStyles.header}>
|
|
216
|
+
{#if currentVariant !== "noTitle" && currentVariant !== "list"}
|
|
217
|
+
<tr class={cx(stickyHeader && "sticky top-0 z-10")}>
|
|
218
|
+
{#each columns as col, idx (getColumnKey(col, idx))}
|
|
219
|
+
<th
|
|
220
|
+
style={!isCompact && col.width ? `width:${col.width}` : undefined}
|
|
221
|
+
class={cx(
|
|
222
|
+
"px-3 font-semibold",
|
|
223
|
+
variantStyles.padY,
|
|
224
|
+
alignTd(col.align)
|
|
225
|
+
)}
|
|
226
|
+
aria-sort={sortKey === col.key
|
|
227
|
+
? sortDir === "asc"
|
|
228
|
+
? "ascending"
|
|
229
|
+
: "descending"
|
|
230
|
+
: "none"}
|
|
231
|
+
scope="col"
|
|
232
|
+
>
|
|
233
|
+
<button
|
|
234
|
+
type="button"
|
|
235
|
+
class={cx(
|
|
236
|
+
"flex w-full items-center gap-2 select-none hover:bg-[var(--color-bg-hover)] rounded-none px-1 py-0.5 border border-transparent focus-visible:border-[var(--border-color-focus)] focus-visible:ring-2 focus-visible:ring-[var(--border-color-focus)] focus-visible:outline-none",
|
|
237
|
+
alignTh(col.align)
|
|
238
|
+
)}
|
|
239
|
+
onclick={() => toggleSort(col)}
|
|
240
|
+
onkeydown={(e) => {
|
|
241
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
242
|
+
e.preventDefault();
|
|
243
|
+
toggleSort(col);
|
|
244
|
+
}
|
|
245
|
+
}}
|
|
246
|
+
aria-label={`Sort by ${col.label}`}
|
|
247
|
+
>
|
|
248
|
+
<span class={cx("truncate min-w-0", alignTd(col.align))}>
|
|
249
|
+
{col.label}
|
|
250
|
+
</span>
|
|
251
|
+
<svg
|
|
252
|
+
class={cx(
|
|
253
|
+
"h-4 w-4 opacity-60 transition-transform",
|
|
254
|
+
sortKey === col.key && sortDir === "desc" && "rotate-180"
|
|
255
|
+
)}
|
|
256
|
+
viewBox="0 0 20 20"
|
|
257
|
+
aria-hidden="true"
|
|
258
|
+
>
|
|
259
|
+
<path
|
|
260
|
+
d="M5.23 11.77a.75.75 0 0 1 0-1.06l4-4a.75.75 0 0 1 1.06 0l4 4a.75.75 0 1 1-1.06 1.06L10 8.31l-3.77 3.46a.75.75 0 0 1-1 0z"
|
|
261
|
+
fill="currentColor"
|
|
262
|
+
/>
|
|
263
|
+
</svg>
|
|
264
|
+
</button>
|
|
265
|
+
</th>
|
|
266
|
+
{/each}
|
|
267
|
+
</tr>
|
|
268
|
+
{/if}
|
|
269
|
+
</thead>
|
|
270
|
+
|
|
271
|
+
<tbody class={variantStyles.body}>
|
|
272
|
+
{#each displayRows as row, rowIdx (getRowKey(row, rowIdx))}
|
|
273
|
+
<tr class={cx("first:[&>td]:border-t-0", variantStyles.row)}>
|
|
274
|
+
{#each columns as col, idx (getColumnKey(col, idx))}
|
|
275
|
+
<td
|
|
276
|
+
style={!isCompact && col.width ? `width:${col.width}` : undefined}
|
|
277
|
+
class={cx(
|
|
278
|
+
"px-3",
|
|
279
|
+
variantStyles.padY,
|
|
280
|
+
alignTd(col.align),
|
|
281
|
+
variantStyles.cell
|
|
282
|
+
)}
|
|
283
|
+
>
|
|
284
|
+
<div class={cx(isCompact && "break-words whitespace-normal")}>
|
|
285
|
+
{row[col.key]}
|
|
286
|
+
</div>
|
|
287
|
+
</td>
|
|
288
|
+
{/each}
|
|
289
|
+
</tr>
|
|
290
|
+
{/each}
|
|
291
|
+
</tbody>
|
|
292
|
+
</table>
|
|
293
|
+
|
|
294
|
+
{#if pagination && pagination.totalPages > 0}
|
|
295
|
+
<div class="mt-3 flex justify-between items-center">
|
|
296
|
+
<Pagination {...pagination} />
|
|
297
|
+
</div>
|
|
298
|
+
{/if}
|
|
299
|
+
</div>
|