Skip to content

Commit

Permalink
Merge pull request #456 from storyblok/feat/work-1344
Browse files Browse the repository at this point in the history
feat(work-1344): introduce data-testids to SbDataTable components
  • Loading branch information
lisilinhart authored Oct 19, 2023
2 parents 85c979a + 0a996b3 commit 67a2318
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/components/DataTable/components/SbDataTableBody.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<template>
<tbody>
<tbody :data-testid="dataTestid">
<SbDataTableBodyRow
v-for="(row, i) in items"
v-bind="{ allowSelection, headers, row, selectedRows }"
:key="i"
:data-testid="`${dataTestid}-row__${i}`"
/>
</tbody>
</template>
Expand All @@ -23,5 +24,10 @@ export default {
items: sharedProps.items,
selectedRows: sharedProps.selectedItems,
},
computed: {
dataTestid() {
return this.$attrs['data-testid'] || 'sb-data-table-body'
},
},
}
</script>
4 changes: 2 additions & 2 deletions src/components/DataTable/components/SbDataTableColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
:class="computedClasses"
:width="width"
:is-sort-icon-always-visible="isSortIconAlwaysVisible"
:data-testid="$attrs['data-testid']"
>
<span
v-if="showLabel" class="sb-data-table__body-cell-label"
<span v-if="showLabel" class="sb-data-table__body-cell-label"
>{{ label }}:</span
>
<slot :row="row" />
Expand Down
19 changes: 16 additions & 3 deletions src/components/DataTable/components/SbDataTableHeader.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<template>
<thead>
<thead :data-testid="dataTestid">
<tr>
<th v-if="allowSelection && isMultiple" class="sb-data-table__head-cell">
<SbCheckbox v-model="isActive" :indeterminate="isIndeterminate" />
<th
v-if="allowSelection && isMultiple"
class="sb-data-table__head -cell"
:data-testid="`${dataTestid}-checkbox-cell`"
>
<SbCheckbox
v-model="isActive"
:indeterminate="isIndeterminate"
:data-testid="`${dataTestid}-checkbox`"
/>
</th>
<SbDataTableHeaderCell
v-for="(elem, index) in headers"
:key="index"
:column="elem"
:sorted-key="sortedKey"
:data-testid="`${dataTestid}-cell__${index}`"
:is-sort-icon-always-visible="isSortIconAlwaysVisible"
/>
</tr>
Expand Down Expand Up @@ -51,6 +60,10 @@ export default {
},

computed: {
dataTestid() {
return this.$attrs['data-testid'] || 'sb-data-table-header'
},

context() {
return this.dataTableContext()
},
Expand Down
16 changes: 14 additions & 2 deletions src/components/DataTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const SbDataTable = {
},

computed: {
dataTestid() {
return this.$attrs['data-testid'] || 'sb-data-table'
},

allRowsSelected() {
const selectableItems = this.items.filter(
(item) => item.selectable !== false
Expand Down Expand Up @@ -85,7 +89,9 @@ const SbDataTable = {
const items = [...this.sortedData]
const pageChunkIndex = this.pagination.currentPage - 1
while (items.length > 0) {
const pageParam = Object.keys(this.pagination).includes('pageSize') ? 'pageSize' : 'perPage'
const pageParam = Object.keys(this.pagination).includes('pageSize')
? 'pageSize'
: 'perPage'
const pageChunk = items.splice(0, this.pagination[pageParam])
paginatedItems = [...paginatedItems, pageChunk]
}
Expand Down Expand Up @@ -231,12 +237,13 @@ const SbDataTable = {
})

bodyData = items.map((tableRow, index) => {
const columns = children.map((tableData) => {
const columns = children.map((tableData, index) => {
return h(
SbDataTableColumn,
{
...tableData.props,
row: tableRow,
'data-testid': `${this.dataTestid}-column__${index}`,
},
tableData.children
)
Expand All @@ -250,6 +257,7 @@ const SbDataTable = {
row: tableRow,
selectedRows: this.hasSelectedRowsInList,
rowId: `${this.rowIdPrefix}-${index}`,
'data-testid': `${this.dataTestid}-body-row__${index}`,
},
() => [columns]
)
Expand All @@ -260,6 +268,7 @@ const SbDataTable = {
'table',
{
class: 'sb-data-table__container',
'data-testid': this.dataTestid,
},
[
this.showHeader
Expand All @@ -271,6 +280,7 @@ const SbDataTable = {
selectionMode: this.selectionMode,
sortedKey: this.sortKey,
isSortIconAlwaysVisible: this.isSortIconAlwaysVisible,
'data-testid': `${this.dataTestid}-header`,
})
: null,
this.sortedData.length && !this.$slots.default
Expand All @@ -279,6 +289,7 @@ const SbDataTable = {
headers: this.headers,
items: this.sortedData,
selectedRows: this.hasSelectedRowsInList,
'data-testid': `${this.dataTestid}-body`,
})
: null,
this.$slots.default
Expand All @@ -292,6 +303,7 @@ const SbDataTable = {
selectionMode: this.selectionMode,
sortedKey: this.sortKey,
isSortIconAlwaysVisible: this.isSortIconAlwaysVisible,
'data-testid': `${this.dataTestid}-header`,
})
: null,
h('tbody', bodyData),
Expand Down

0 comments on commit 67a2318

Please sign in to comment.