Dynamic styling.
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
export type Column<T> = {
|
||||
[K in keyof T]: {
|
||||
key: K;
|
||||
label: string;
|
||||
label?: string;
|
||||
wrap?: boolean;
|
||||
ratio?: number;
|
||||
minWidth?: Width;
|
||||
@@ -26,11 +26,15 @@
|
||||
};
|
||||
}[keyof T];
|
||||
|
||||
export type Style = 'highlight';
|
||||
export type StyleFn<T> = (key: keyof T, row?: T) => Style[];
|
||||
|
||||
export type TableProps<T> = {
|
||||
columns: Column<T>[];
|
||||
data: T[];
|
||||
header?: Snippet<[Column<T>]>;
|
||||
cell?: Snippet<[Cell<T>]>;
|
||||
style?: StyleFn<T>;
|
||||
};
|
||||
|
||||
type Sizing = {
|
||||
@@ -61,13 +65,14 @@
|
||||
<script lang="ts" generics="T extends { id: string | number }">
|
||||
import type { Snippet } from 'svelte';
|
||||
|
||||
let { columns, data, header, cell }: TableProps<T> = $props();
|
||||
let { columns, data, header, cell, style }: TableProps<T> = $props();
|
||||
|
||||
const sizedColumns = $derived(
|
||||
columns.map((column) => ({ column, sizing: width(column.minWidth) }))
|
||||
);
|
||||
|
||||
const gridTemplate = $derived(
|
||||
'auto ' +
|
||||
sizedColumns
|
||||
.map(
|
||||
({ column, sizing }) =>
|
||||
@@ -84,10 +89,12 @@
|
||||
<thead class="col-span-full grid grid-cols-subgrid">
|
||||
<tr class="col-span-full grid grid-cols-subgrid">
|
||||
{#each sizedColumns as { column: c, sizing: s } (c.key)}
|
||||
{@const styles = style?.(c.key) ?? []}
|
||||
<th
|
||||
class={'px-4 py-3 text-sm font-bold text-neutral-400' +
|
||||
(c.wrap ? ' wrap-break-words' : ' truncate') +
|
||||
(s.target === 'cell' ? ' contain-[inline-size]' : '')}
|
||||
(s.target === 'cell' ? ' contain-[inline-size]' : '') +
|
||||
(styles.includes('highlight') ? ' bg-neutral-900' : '')}
|
||||
>
|
||||
{#if header}
|
||||
{@render header(c)}
|
||||
@@ -102,10 +109,12 @@
|
||||
{#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: s } (c.key)}
|
||||
{@const styles = style?.(c.key, d) ?? []}
|
||||
<td
|
||||
class={'border-t border-neutral-700 px-4 py-2.5 tabular-nums' +
|
||||
(c.wrap ? ' wrap-break-words' : ' truncate') +
|
||||
(s.target === 'header' ? ' contain-[inline-size]' : '')}
|
||||
(s.target === 'header' ? ' contain-[inline-size]' : '') +
|
||||
(styles.includes('highlight') ? ' bg-neutral-900' : '')}
|
||||
>
|
||||
{#if cell}
|
||||
{@render cell({
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<script lang="ts">
|
||||
import Table, { type Column } from '#lib/components/table.svelte';
|
||||
|
||||
<script lang="ts" module>
|
||||
type ScheduledTransaction = {
|
||||
checked: boolean;
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
@@ -10,12 +9,20 @@
|
||||
};
|
||||
|
||||
const columns: Column<ScheduledTransaction>[] = [
|
||||
{ key: 'checked' },
|
||||
{ key: 'id', label: '#' },
|
||||
{ key: 'name', label: 'Name', ratio: 1 },
|
||||
{ key: 'account', label: 'Account' },
|
||||
{ key: 'amount', label: 'Amount' }
|
||||
];
|
||||
const data: ScheduledTransaction[] = [
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import Table, { type Column } from '#lib/components/table.svelte';
|
||||
|
||||
const data: ScheduledTransaction[] = $state([
|
||||
{
|
||||
checked: true,
|
||||
id: 1,
|
||||
name: 'Some transaction',
|
||||
description: 'Some description',
|
||||
@@ -23,6 +30,7 @@
|
||||
amount: 100
|
||||
},
|
||||
{
|
||||
checked: false,
|
||||
id: 2,
|
||||
name: 'Another transaction',
|
||||
description: 'Another description',
|
||||
@@ -30,17 +38,37 @@
|
||||
amount: 200
|
||||
},
|
||||
{
|
||||
checked: true,
|
||||
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} />
|
||||
<Table {columns} {data} style={(_key, row) => (row?.checked ? ['highlight'] : [])}>
|
||||
{#snippet header({ key, label })}
|
||||
{#if key === 'checked'}
|
||||
<input
|
||||
type="checkbox"
|
||||
onchange={(e) => data.forEach((row) => (row.checked = e.currentTarget.checked))}
|
||||
checked={data.every((row) => row.checked)}
|
||||
/>
|
||||
{:else}
|
||||
{label}
|
||||
{/if}
|
||||
{/snippet}
|
||||
{#snippet cell({ key, value, row })}
|
||||
{#if key === 'checked'}
|
||||
<input type="checkbox" bind:checked={row.checked} />
|
||||
{:else}
|
||||
{value}
|
||||
{/if}
|
||||
{/snippet}
|
||||
</Table>
|
||||
|
||||
Reference in New Issue
Block a user