feat(ui): task logs as a table with expandable fields
This commit is contained in:
1 parent
e143362479
commit
849e24757d
4 files changed
+175
-126
No files matched your search
@@ -19,7 +19,8 @@ import type { DataTableColumn } from "./types";
|
||||
* 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.
|
||||
* inside a cell must stop their click and keydown events. With a `#detail`
|
||||
* slot and no `rowLink`, a row opens in place to show its detail beneath it.
|
||||
*/
|
||||
const props = defineProps<{
|
||||
columns: DataTableColumn<Row>[];
|
||||
@@ -29,7 +30,7 @@ const props = defineProps<{
|
||||
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 slots = defineSlots<Record<string, (props: { row: Row }) => unknown> & { empty?: () => unknown; actions?: (props: { row: Row }) => unknown; detail?: (props: { row: Row }) => unknown }>();
|
||||
|
||||
const router = useRouter();
|
||||
const chosen = useLocalStorage<string[]>(
|
||||
@@ -62,8 +63,18 @@ const sorted = computed(() => {
|
||||
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);
|
||||
|
||||
const expandable = computed(() => !props.rowLink && !!slots.detail);
|
||||
const expanded = ref(new Set<string>());
|
||||
function open(row: Row): void {
|
||||
if (props.rowLink) void router.push(props.rowLink(row));
|
||||
if (props.rowLink) {
|
||||
void router.push(props.rowLink(row));
|
||||
return;
|
||||
}
|
||||
if (!expandable.value) return;
|
||||
const key = props.rowKey(row);
|
||||
const next = new Set(expanded.value);
|
||||
if (!next.delete(key)) next.add(key);
|
||||
expanded.value = next;
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -105,23 +116,29 @@ function open(row: Row): void {
|
||||
<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>
|
||||
<template v-for="row in sorted" :key="rowKey(row)">
|
||||
<TableRow
|
||||
:data-row-key="rowKey(row)"
|
||||
:tabindex="rowLink || expandable ? 0 : undefined"
|
||||
:role="rowLink ? 'link' : undefined"
|
||||
:aria-label="rowLink && rowLabel ? rowLabel(row) : undefined"
|
||||
:aria-expanded="expandable ? expanded.has(rowKey(row)) : undefined"
|
||||
:class="['align-top', (rowLink || expandable) && '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>
|
||||
<TableRow v-if="expandable && expanded.has(rowKey(row))" class="hover:bg-transparent">
|
||||
<TableCell :colspan="shown.length + (trailing ? 1 : 0)" class="bg-muted/40">
|
||||
<slot name="detail" :row="row" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</template>
|
||||
</TableBody>
|
||||
</Table>
|
||||
</template>
|
||||
Reference in new issue
Block a user