Improved table logic.

This commit is contained in:
2026-10-23 22:28:52 +02:00
parent cf68405c44
commit 2c2db75bd5
3 changed files with 173 additions and 1 deletions
+121
View File
@@ -0,0 +1,121 @@
<script lang="ts" module>
export type Width =
| 'min-content'
| 'max-content'
| 'min-cell'
| 'max-cell'
| 'min-header'
| 'max-header'
| `${number}rem`;
export type Column<T> = {
[K in keyof T]: {
key: K;
label: string;
wrap?: boolean;
ratio?: number;
minWidth?: Width;
};
}[keyof T];
export type Cell<T> = {
[K in keyof T]: {
key: K;
value: T[K];
row: T;
};
}[keyof T];
export type TableProps<T> = {
columns: Column<T>[];
data: T[];
header: Snippet<[Column<T>]>;
cell: Snippet<[Cell<T>]>;
};
type Sizing = {
target: 'cell' | 'header' | 'either';
size: 'min-content' | 'max-content' | `${number}rem`;
};
function width(width: Width = 'min-content'): Sizing {
switch (width) {
case 'min-content':
return { target: 'either', size: 'min-content' };
case 'max-content':
return { target: 'either', size: 'max-content' };
case 'min-cell':
return { target: 'cell', size: 'min-content' };
case 'max-cell':
return { target: 'cell', size: 'max-content' };
case 'min-header':
return { target: 'header', size: 'min-content' };
case 'max-header':
return { target: 'header', size: 'max-content' };
default:
return { target: 'either', size: width };
}
}
</script>
<script lang="ts" generics="T extends { id: string | number }">
import type { Snippet } from 'svelte';
let { columns, data, header, cell }: TableProps<T> = $props();
const sizedColumns = $derived(
columns.map((column) => ({ column, sizing: width(column.minWidth) }))
);
const gridTemplate = $derived(
sizedColumns
.map(
({ column, sizing }) =>
`minmax(${sizing.size}, ${column.ratio ? `${column.ratio}fr` : 'max-content'})`
)
.join(' ')
);
const containClass = ' contain-[inline-size]';
const headerClass = (s: Sizing) => (s.target === 'cell' ? containClass : '');
const cellClass = (s: Sizing) => (s.target === 'header' ? containClass : '');
</script>
<div
class="overflow-auto overscroll-x-none rounded border border-neutral-600 bg-neutral-950 text-left text-neutral-100"
>
<table class="grid w-full" style:grid-template-columns={gridTemplate}>
<thead class="col-span-full grid grid-cols-subgrid">
<tr class="col-span-full grid grid-cols-subgrid">
{#each sizedColumns as { column: c, sizing } (c.key)}
<th
class={'px-4 py-3 text-sm font-bold text-neutral-400' +
(c.wrap ? ' wrap-break-words' : ' truncate') +
headerClass(sizing)}
>
{@render header(c)}
</th>
{/each}
</tr>
</thead>
<tbody class="col-span-full grid grid-cols-subgrid">
{#each data as d (d.id)}
<tr class="col-span-full grid grid-cols-subgrid hover:bg-neutral-900">
{#each sizedColumns as { column: c, sizing } (c.key)}
<td
class={'border-t border-neutral-700 px-4 py-2.5 tabular-nums' +
(c.wrap ? ' wrap-break-words' : ' truncate') +
cellClass(sizing)}
>
{@render cell({
key: c.key,
value: d[c.key],
row: d
})}
</td>
{/each}
</tr>
{/each}
</tbody>
</table>
</div>
+1 -1
View File
@@ -39,7 +39,7 @@
</nav>
</aside>
<main id="main-content" tabindex="-1" class="min-w-0 px-4 py-8 focus:outline-none sm:px-10">
<main class="min-w-0 px-4 py-8">
<div class="mx-auto max-w-6xl">
{@render children()}
</div>
@@ -1,6 +1,57 @@
<script lang="ts">
import Table, { type Column } from '#lib/components/table.svelte';
type ScheduledTransaction = {
id: number;
name: string;
description: string;
account: string;
amount: number;
};
const columns: Column<ScheduledTransaction>[] = [
{ key: 'name', label: 'Name', ratio: 1 },
{ key: 'account', label: 'Account' },
{ key: 'amount', label: 'Amount' }
];
const data: ScheduledTransaction[] = [
{
id: 1,
name: 'Some transaction',
description: 'Some description',
account: 'Some account',
amount: 100
},
{
id: 2,
name: 'Another transaction',
description: 'Another description',
account: 'Another account',
amount: 200
},
{
id: 3,
name: 'Yet another transaction',
description: 'Yet another description, but longer than the others',
account: 'Yet another account',
amount: 300
}
];
</script>
<svelte:head>
<title>Scheduled transactions | Expenses</title>
</svelte:head>
<Table {columns} {data}>
{#snippet header(col)}
{col.label}
{/snippet}
{#snippet cell(ctx)}
{#if ctx.key === 'name'}
{ctx.value}
{:else}
{ctx.value}
{/if}
{/snippet}
</Table>