220 lines
6.3 KiB
Svelte
220 lines
6.3 KiB
Svelte
<script lang="ts">
|
|
export type DataTableColumn = {
|
|
label: string;
|
|
value: (record: Record<string, unknown>) => unknown;
|
|
};
|
|
|
|
let {
|
|
title = '',
|
|
columns = [] as DataTableColumn[],
|
|
records = [] as Record<string, unknown>[],
|
|
newLabel = 'New',
|
|
searchPlaceholder = 'Search...',
|
|
copyableFields = [] as string[],
|
|
draggable = false,
|
|
onRowClick,
|
|
onNew,
|
|
onReorder
|
|
}: {
|
|
title?: string;
|
|
columns?: DataTableColumn[];
|
|
records?: Record<string, unknown>[];
|
|
newLabel?: string;
|
|
searchPlaceholder?: string;
|
|
copyableFields?: string[];
|
|
draggable?: boolean;
|
|
onRowClick?: (record: Record<string, unknown>) => void;
|
|
onNew?: () => void;
|
|
onReorder?: (ids: (string | number)[]) => void;
|
|
} = $props();
|
|
|
|
let search = $state('');
|
|
|
|
const filteredRecords = $derived(
|
|
search
|
|
? records.filter((record) =>
|
|
columns.some((column) =>
|
|
String(column.value(record) ?? '')
|
|
.toLowerCase()
|
|
.includes(search.toLowerCase())
|
|
)
|
|
)
|
|
: records
|
|
);
|
|
|
|
let dragOverride = $state<Record<string, unknown>[] | null>(null);
|
|
let dragIndex = $state<number | null>(null);
|
|
let hoverIndex = $state<number | null>(null);
|
|
|
|
const rows = $derived(dragOverride ?? filteredRecords);
|
|
|
|
function rowKey(record: Record<string, unknown>, index: number): string {
|
|
return String(record.id ?? record.key ?? index);
|
|
}
|
|
|
|
function copyValue(value: unknown) {
|
|
navigator.clipboard.writeText(String(value ?? ''));
|
|
}
|
|
|
|
function handleDragStart(e: DragEvent, i: number) {
|
|
dragOverride = filteredRecords;
|
|
dragIndex = i;
|
|
hoverIndex = i;
|
|
e.dataTransfer!.effectAllowed = 'move';
|
|
e.dataTransfer!.setData('text/plain', '');
|
|
}
|
|
|
|
function handleDragOver(e: DragEvent, i: number) {
|
|
if (dragIndex === null) return;
|
|
e.preventDefault();
|
|
if (i === hoverIndex) return;
|
|
e.dataTransfer!.dropEffect = 'move';
|
|
|
|
const reordered = rows.slice();
|
|
const [moved] = reordered.splice(dragIndex, 1);
|
|
reordered.splice(i, 0, moved);
|
|
dragOverride = reordered;
|
|
dragIndex = i;
|
|
hoverIndex = i;
|
|
}
|
|
|
|
async function handleDrop() {
|
|
if (dragIndex === null) return;
|
|
const ids = rows.map((r) => r.id as string | number);
|
|
await onReorder?.(ids);
|
|
dragIndex = null;
|
|
hoverIndex = null;
|
|
}
|
|
|
|
function handleDragEnd() {
|
|
dragOverride = null;
|
|
dragIndex = null;
|
|
hoverIndex = null;
|
|
}
|
|
</script>
|
|
|
|
<div class="mx-auto w-full">
|
|
<div class="mb-4 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
|
<h2 class="text-xl font-semibold text-slate-900 dark:text-slate-100">{title}</h2>
|
|
|
|
<div class="flex w-full flex-col gap-3 sm:w-auto sm:flex-row sm:items-center">
|
|
<input
|
|
type="search"
|
|
bind:value={search}
|
|
placeholder={searchPlaceholder}
|
|
class="w-full rounded-md border border-slate-300 bg-white px-3 py-2 text-sm text-slate-900 shadow-sm transition focus:border-indigo-500 focus:outline-none sm:w-64 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-100"
|
|
/>
|
|
{#if onNew}
|
|
<button
|
|
type="button"
|
|
onclick={onNew}
|
|
class="inline-flex h-10 items-center justify-center gap-1.5 rounded-md bg-indigo-600 px-4 text-sm font-medium text-white shadow-sm transition hover:bg-indigo-700"
|
|
>
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
width="16"
|
|
height="16"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="2"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
aria-hidden="true"><path d="M12 5v14" /><path d="M5 12h14" /></svg
|
|
>
|
|
{newLabel}
|
|
</button>
|
|
{/if}
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
class="w-full overflow-x-auto rounded-lg border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900"
|
|
>
|
|
<table class="w-full min-w-max text-left text-sm text-slate-700 dark:text-slate-300">
|
|
<thead>
|
|
<tr
|
|
class="border-b border-slate-200 bg-slate-100 text-xs font-semibold tracking-wide text-slate-500 dark:border-slate-800 dark:bg-slate-800 dark:text-slate-400"
|
|
>
|
|
{#if draggable}
|
|
<th scope="col" class="w-10 px-4 py-3"></th>
|
|
{/if}
|
|
{#each columns as column (column.label)}
|
|
<th scope="col" class="px-4 py-3">{column.label}</th>
|
|
{/each}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{#each rows as record, i (rowKey(record, i))}
|
|
<tr
|
|
class="transition {i % 2 === 0
|
|
? 'bg-white dark:bg-slate-900'
|
|
: 'bg-slate-50 dark:bg-slate-950'} {onRowClick
|
|
? 'cursor-pointer hover:bg-indigo-50 dark:hover:bg-indigo-950'
|
|
: ''}"
|
|
{draggable}
|
|
ondragstart={(e) => handleDragStart(e, i)}
|
|
ondragover={(e) => handleDragOver(e, i)}
|
|
ondrop={handleDrop}
|
|
ondragend={handleDragEnd}
|
|
onclick={() => onRowClick?.(record)}
|
|
>
|
|
{#if draggable}
|
|
<td class="px-4 py-3" onclick={(e) => e.stopPropagation()} aria-hidden="true">
|
|
<span
|
|
class="cursor-grab text-slate-400 select-none active:cursor-grabbing dark:text-slate-500"
|
|
title="Drag to reorder">⠿</span
|
|
>
|
|
</td>
|
|
{/if}
|
|
{#each columns as column (column.label)}
|
|
<td class="px-4 py-3 whitespace-nowrap">
|
|
{#if copyableFields.includes(column.label) && column.value(record) != null}
|
|
<span>
|
|
{String(column.value(record))}
|
|
<button
|
|
type="button"
|
|
onclick={(e) => {
|
|
e.stopPropagation();
|
|
copyValue(column.value(record));
|
|
}}
|
|
title="Copy to clipboard"
|
|
class="ml-1 inline-flex text-slate-400 transition hover:text-indigo-600 dark:text-slate-500 dark:hover:text-indigo-400"
|
|
>
|
|
<svg
|
|
viewBox="0 0 16 16"
|
|
width="14"
|
|
height="14"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
stroke-width="1.5"
|
|
stroke-linecap="round"
|
|
stroke-linejoin="round"
|
|
aria-hidden="true"
|
|
>
|
|
<rect x="3.5" y="2.5" width="11" height="11" rx="1" />
|
|
<path d="M5.5 2.5V1.5a1 1 0 011-1h3a1 1 0 011 1v1" />
|
|
</svg>
|
|
</button>
|
|
</span>
|
|
{:else}
|
|
{String(column.value(record) ?? '')}
|
|
{/if}
|
|
</td>
|
|
{/each}
|
|
</tr>
|
|
{:else}
|
|
<tr>
|
|
<td
|
|
colspan={columns.length + (draggable ? 1 : 0)}
|
|
class="bg-white dark:bg-slate-900 px-4 py-10 text-center text-slate-500 dark:text-slate-400"
|
|
>
|
|
No results found.
|
|
</td>
|
|
</tr>
|
|
{/each}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|