refactor(ui): build the task list on DataTable with sortable columns and row links

This commit is contained in:
sam committed 2026-10-24 13:29:57 +11:00
1 parent ffafc3a989
commit 793569f6e1
15 files changed
+349 -518

No files matched your search

+93 -47
View File
@@ -1,7 +1,8 @@
<script setup lang="ts" generic="Row">
import { computed } from "vue";
import { computed, ref } from "vue";
import { useRouter } from "vue-router";
import { useLocalStorage } from "@vueuse/core";
import { Columns3 } from "@lucide/vue";
import { ArrowDown, ArrowUp, ArrowUpDown, Columns3 } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import {
@@ -11,71 +12,116 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
export interface DataTableColumn {
id: string;
label: string;
/** Always shown and not offered in the column picker. */
fixed?: boolean;
/** Hidden until the operator turns it on. */
hidden?: boolean;
class?: string;
}
import type { DataTableColumn } from "./types";
/**
* A table whose optional columns the operator chooses, remembered per browser
* under `storageKey`. Cells render through `#cell-<id>` slots, given the row.
* under `storageKey`. Cells render through `#cell-<id>` slots, given the row;
* `#actions` fills a trailing cell and `#empty` replaces the body when there
* are no rows. With `rowLink`, the whole row opens that route, so controls
* inside a cell must stop their click and keydown events.
*/
const props = defineProps<{
columns: DataTableColumn[];
columns: DataTableColumn<Row>[];
rows: Row[];
rowKey: (row: Row) => string;
storageKey: string;
rowLink?: (row: Row) => string;
rowLabel?: (row: Row) => string;
}>();
const slots = defineSlots<Record<string, (props: { row: Row }) => unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown }>();
const router = useRouter();
const chosen = useLocalStorage<string[]>(
`archie.columns.${props.storageKey}`,
props.columns.filter((c) => !c.fixed && !c.hidden).map((c) => c.id),
);
const shown = computed(() => props.columns.filter((c) => c.fixed || chosen.value.includes(c.id)));
const optional = computed(() => props.columns.filter((c) => !c.fixed));
const trailing = computed(() => optional.value.length > 0 || !!slots.actions);
function toggle(id: string, on: boolean): void {
chosen.value = on ? [...chosen.value, id] : chosen.value.filter((c) => c !== id);
}
// Sorting covers the loaded rows only.
const sortBy = ref<{ id: string; desc: boolean } | null>(null);
function cycleSort(id: string): void {
const current = sortBy.value;
sortBy.value = current?.id !== id ? { id, desc: false } : current.desc ? null : { id, desc: true };
}
const sorted = computed(() => {
const key = sortBy.value && props.columns.find((c) => c.id === sortBy.value?.id)?.sort;
if (!key) return props.rows;
const sign = sortBy.value?.desc ? -1 : 1;
return [...props.rows].sort((a, b) => {
const x = key(a);
const y = key(b);
return sign * (typeof x === "number" && typeof y === "number" ? x - y : String(x).localeCompare(String(y)));
});
});
const ariaSort = (id: string) => (sortBy.value?.id !== id ? "none" : sortBy.value.desc ? "descending" : "ascending");
const sortIcon = (id: string) => (sortBy.value?.id !== id ? ArrowUpDown : sortBy.value.desc ? ArrowDown : ArrowUp);
function open(row: Row): void {
if (props.rowLink) void router.push(props.rowLink(row));
}
</script>
<template>
<div>
<Table>
<TableHeader>
<TableRow>
<TableHead v-for="column in shown" :key="column.id" :class="column.class">{{ column.label }}</TableHead>
<TableHead v-if="optional.length" class="w-0 text-right">
<DropdownMenu>
<DropdownMenuTrigger as-child>
<Button variant="ghost" size="icon-sm" aria-label="Columns" title="Columns"><Columns3 /></Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuCheckboxItem
v-for="column in optional"
:key="column.id"
:model-value="chosen.includes(column.id)"
@update:model-value="toggle(column.id, $event)"
@select.prevent
>{{ column.label }}</DropdownMenuCheckboxItem>
</DropdownMenuContent>
</DropdownMenu>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="row in rows" :key="rowKey(row)" class="align-top">
<TableCell v-for="column in shown" :key="column.id" :class="column.class">
<slot :name="`cell-${column.id}`" :row="row" />
</TableCell>
<TableCell v-if="optional.length" />
</TableRow>
</TableBody>
</Table>
</div>
<Table>
<TableHeader>
<TableRow>
<TableHead
v-for="column in shown"
:key="column.id"
:class="column.class"
:aria-sort="column.sort ? ariaSort(column.id) : undefined"
>
<Button v-if="column.sort" variant="ghost" size="sm" class="-ml-2.5" @click="cycleSort(column.id)">
{{ column.label }}<component :is="sortIcon(column.id)" data-icon="inline-end" />
</Button>
<template v-else>{{ column.label }}</template>
</TableHead>
<TableHead v-if="trailing" class="w-0 text-right">
<DropdownMenu v-if="optional.length">
<DropdownMenuTrigger as-child>
<Button variant="ghost" size="icon-sm" aria-label="Columns" title="Columns"><Columns3 /></Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuCheckboxItem
v-for="column in optional"
:key="column.id"
:model-value="chosen.includes(column.id)"
@update:model-value="toggle(column.id, $event)"
@select.prevent
>{{ column.label }}</DropdownMenuCheckboxItem>
</DropdownMenuContent>
</DropdownMenu>
<span v-else class="sr-only">Actions</span>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-if="!rows.length && slots.empty">
<TableCell :colspan="shown.length + (trailing ? 1 : 0)"><slot name="empty" /></TableCell>
</TableRow>
<TableRow
v-for="row in sorted"
:key="rowKey(row)"
:data-row-key="rowKey(row)"
:tabindex="rowLink ? 0 : undefined"
:role="rowLink ? 'link' : undefined"
:aria-label="rowLink && rowLabel ? rowLabel(row) : undefined"
:class="['align-top', rowLink && 'cursor-pointer']"
@click="open(row)"
@keydown.enter.self.prevent="open(row)"
@keydown.space.self.prevent="open(row)"
>
<TableCell v-for="column in shown" :key="column.id" :class="column.class">
<slot :name="`cell-${column.id}`" :row="row" />
</TableCell>
<TableCell v-if="trailing"><slot name="actions" :row="row" /></TableCell>
</TableRow>
</TableBody>
</Table>
</template>
+1 -1
View File
@@ -1,2 +1,2 @@
export { default as DataTable } from "./DataTable.vue";
export type { DataTableColumn } from "./DataTable.vue";
export type { DataTableColumn } from "./types";
+11
View File
@@ -0,0 +1,11 @@
export interface DataTableColumn<Row = never> {
id: string;
label: string;
/** Always shown and not offered in the column picker. */
fixed?: boolean;
/** Hidden until the operator turns it on. */
hidden?: boolean;
class?: string;
/** Makes the header sort the loaded rows by this value. */
sort?: (row: Row) => string | number;
}