Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions reposilite-frontend/src/components/md/MdDataTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,23 @@ describe('MdDataTable for a page that owns its rows', () => {
expect(headers(wrapper)[0].find('button').classes()).toEqual(expect.arrayContaining(['min-h-6', 'min-w-6']))
})
})

describe('MdDataTable row header column', () => {
it('makes the column named by rowHeader the row header, in its own position', () => {
const wrapper = mountTable({ rowHeader: 'type' })

const firstRow = wrapper.findAll('tbody tr')[0]

expect(firstRow.findAll('th').map((cell) => cell.text()), 'the row header').toEqual(['maven'])
expect(firstRow.findAll('th')[0].attributes('scope')).toBe('row')
expect(firstRow.findAll('td').map((cell) => cell.text()), 'the other cells').toEqual(['releases', '30'])
expect(firstRow.element.children[1].tagName, 'the row header keeps its place in the row').toBe('TH')
})

it('keeps the first column as the row header without the prop', () => {
const wrapper = mountTable()

expect(wrapper.findAll('tbody tr')[0].element.children[0].tagName).toBe('TH')
})
})

37 changes: 16 additions & 21 deletions reposilite-frontend/src/components/md/MdDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* caption the visible caption, and the name of the scroll region
* columns { key, label, sortable?, priority? }, the priority being high (the default), low, base, medium,
* expanded, large or xlarge
* rows one object per row, keyed by the column keys. The first column is the row header (th scope row).
* rows one object per row, keyed by the column keys.
* rowHeader the key of the column that is the row header (th scope row), in its own position. Default: the first column.
* modelValue the sort, { key, direction } with direction ascending or descending (v-model). Without it the table
* keeps the sort itself.
* captionHidden the caption stays in the DOM for screen readers but is visually hidden (default false)
Expand Down Expand Up @@ -71,6 +72,7 @@ const props = withDefaults(
label?: string
presorted?: boolean
rowKey?: string
rowHeader?: string
rowAttrs?: Record<string, string>
// eslint-disable-next-line no-unused-vars -- the parameter of a function type is a name, not a variable
rowClass?: (row: Record<string, unknown>) => string | undefined
Expand All @@ -82,6 +84,7 @@ const props = withDefaults(
label: undefined,
presorted: false,
rowKey: undefined,
rowHeader: undefined,
rowAttrs: undefined,
rowClass: undefined,
},
Expand Down Expand Up @@ -128,6 +131,7 @@ const chevronClass = (column: Column) => (ariaSort(column) === 'descending' ? 'r
const ALIGN_CLASS: Record<Align, string> = { start: 'text-start', end: 'text-end', center: 'text-center' }
const alignClass = (column?: Column) => ALIGN_CLASS[column?.align ?? 'start']
const fillClass = (column?: Column) => (column?.fill ? 'w-full max-w-0' : '')
const isRowHeader = (column: Column) => column.key === (props.rowHeader ?? props.columns[0]?.key)
const keyOf = (row: Record<string, unknown>, index: number) => (props.rowKey ? String(row[props.rowKey]) : index)
const attrsOf = (row: Record<string, unknown>) =>
Object.fromEntries(Object.entries(props.rowAttrs ?? {}).map(([attribute, key]) => [attribute, String(row[key])]))
Expand Down Expand Up @@ -188,26 +192,17 @@ const headerClass = computed(() => (props.stickyHeader ? 'sticky top-0 z-md-stic
data-ingot-part="row"
v-bind="attrsOf(row)"
>
<th
v-if="columns[0]"
scope="row"
class="md-body-medium px-4 py-3 align-middle text-on-surface"
:class="[alignClass(columns[0]), fillClass(columns[0])]"
>
<slot
name="cell"
:row="row"
:column="columns[0]"
:value="row[columns[0].key]"
>
{{ row[columns[0].key] }}
</slot>
</th>
<td
v-for="column in columns.slice(1)"
<component
:is="isRowHeader(column) ? 'th' : 'td'"
v-for="column in columns"
:key="column.key"
class="md-body-medium px-4 py-3 align-middle text-on-surface-variant"
:class="[priorityClass(column), alignClass(column), fillClass(column)]"
:scope="isRowHeader(column) ? 'row' : undefined"
class="md-body-medium px-4 py-3 align-middle"
:class="[
isRowHeader(column) ? 'text-on-surface' : ['text-on-surface-variant', priorityClass(column)],
alignClass(column),
fillClass(column),
]"
>
<slot
name="cell"
Expand All @@ -217,7 +212,7 @@ const headerClass = computed(() => (props.stickyHeader ? 'sticky top-0 z-md-stic
>
{{ row[column.key] }}
</slot>
</td>
</component>
</tr>
<tr
v-if="sortedRows.length === 0 && $slots.empty"
Expand Down
Loading