Dynamic styling.
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
export type Column<T> = {
|
export type Column<T> = {
|
||||||
[K in keyof T]: {
|
[K in keyof T]: {
|
||||||
key: K;
|
key: K;
|
||||||
label: string;
|
label?: string;
|
||||||
wrap?: boolean;
|
wrap?: boolean;
|
||||||
ratio?: number;
|
ratio?: number;
|
||||||
minWidth?: Width;
|
minWidth?: Width;
|
||||||
@@ -26,11 +26,15 @@
|
|||||||
};
|
};
|
||||||
}[keyof T];
|
}[keyof T];
|
||||||
|
|
||||||
|
export type Style = 'highlight';
|
||||||
|
export type StyleFn<T> = (key: keyof T, row?: T) => Style[];
|
||||||
|
|
||||||
export type TableProps<T> = {
|
export type TableProps<T> = {
|
||||||
columns: Column<T>[];
|
columns: Column<T>[];
|
||||||
data: T[];
|
data: T[];
|
||||||
header?: Snippet<[Column<T>]>;
|
header?: Snippet<[Column<T>]>;
|
||||||
cell?: Snippet<[Cell<T>]>;
|
cell?: Snippet<[Cell<T>]>;
|
||||||
|
style?: StyleFn<T>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type Sizing = {
|
type Sizing = {
|
||||||
@@ -61,19 +65,20 @@
|
|||||||
<script lang="ts" generics="T extends { id: string | number }">
|
<script lang="ts" generics="T extends { id: string | number }">
|
||||||
import type { Snippet } from 'svelte';
|
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(
|
const sizedColumns = $derived(
|
||||||
columns.map((column) => ({ column, sizing: width(column.minWidth) }))
|
columns.map((column) => ({ column, sizing: width(column.minWidth) }))
|
||||||
);
|
);
|
||||||
|
|
||||||
const gridTemplate = $derived(
|
const gridTemplate = $derived(
|
||||||
sizedColumns
|
'auto ' +
|
||||||
.map(
|
sizedColumns
|
||||||
({ column, sizing }) =>
|
.map(
|
||||||
`minmax(${sizing.size}, ${column.ratio ? `${column.ratio}fr` : 'max-content'})`
|
({ column, sizing }) =>
|
||||||
)
|
`minmax(${sizing.size}, ${column.ratio ? `${column.ratio}fr` : 'max-content'})`
|
||||||
.join(' ')
|
)
|
||||||
|
.join(' ')
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -84,10 +89,12 @@
|
|||||||
<thead class="col-span-full grid grid-cols-subgrid">
|
<thead class="col-span-full grid grid-cols-subgrid">
|
||||||
<tr 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)}
|
{#each sizedColumns as { column: c, sizing: s } (c.key)}
|
||||||
|
{@const styles = style?.(c.key) ?? []}
|
||||||
<th
|
<th
|
||||||
class={'px-4 py-3 text-sm font-bold text-neutral-400' +
|
class={'px-4 py-3 text-sm font-bold text-neutral-400' +
|
||||||
(c.wrap ? ' wrap-break-words' : ' truncate') +
|
(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}
|
{#if header}
|
||||||
{@render header(c)}
|
{@render header(c)}
|
||||||
@@ -102,10 +109,12 @@
|
|||||||
{#each data as d (d.id)}
|
{#each data as d (d.id)}
|
||||||
<tr class="col-span-full grid grid-cols-subgrid hover:bg-neutral-900">
|
<tr class="col-span-full grid grid-cols-subgrid hover:bg-neutral-900">
|
||||||
{#each sizedColumns as { column: c, sizing: s } (c.key)}
|
{#each sizedColumns as { column: c, sizing: s } (c.key)}
|
||||||
|
{@const styles = style?.(c.key, d) ?? []}
|
||||||
<td
|
<td
|
||||||
class={'border-t border-neutral-700 px-4 py-2.5 tabular-nums' +
|
class={'border-t border-neutral-700 px-4 py-2.5 tabular-nums' +
|
||||||
(c.wrap ? ' wrap-break-words' : ' truncate') +
|
(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}
|
{#if cell}
|
||||||
{@render cell({
|
{@render cell({
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts" module>
|
||||||
import Table, { type Column } from '#lib/components/table.svelte';
|
|
||||||
|
|
||||||
type ScheduledTransaction = {
|
type ScheduledTransaction = {
|
||||||
|
checked: boolean;
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
@@ -10,12 +9,20 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const columns: Column<ScheduledTransaction>[] = [
|
const columns: Column<ScheduledTransaction>[] = [
|
||||||
|
{ key: 'checked' },
|
||||||
|
{ key: 'id', label: '#' },
|
||||||
{ key: 'name', label: 'Name', ratio: 1 },
|
{ key: 'name', label: 'Name', ratio: 1 },
|
||||||
{ key: 'account', label: 'Account' },
|
{ key: 'account', label: 'Account' },
|
||||||
{ key: 'amount', label: 'Amount' }
|
{ 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,
|
id: 1,
|
||||||
name: 'Some transaction',
|
name: 'Some transaction',
|
||||||
description: 'Some description',
|
description: 'Some description',
|
||||||
@@ -23,6 +30,7 @@
|
|||||||
amount: 100
|
amount: 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
checked: false,
|
||||||
id: 2,
|
id: 2,
|
||||||
name: 'Another transaction',
|
name: 'Another transaction',
|
||||||
description: 'Another description',
|
description: 'Another description',
|
||||||
@@ -30,17 +38,37 @@
|
|||||||
amount: 200
|
amount: 200
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
checked: true,
|
||||||
id: 3,
|
id: 3,
|
||||||
name: 'Yet another transaction',
|
name: 'Yet another transaction',
|
||||||
description: 'Yet another description, but longer than the others',
|
description: 'Yet another description, but longer than the others',
|
||||||
account: 'Yet another account',
|
account: 'Yet another account',
|
||||||
amount: 300
|
amount: 300
|
||||||
}
|
}
|
||||||
];
|
]);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
<title>Scheduled transactions | Expenses</title>
|
<title>Scheduled transactions | Expenses</title>
|
||||||
</svelte:head>
|
</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