Skip to content

Commit 9568b87

Browse files
authored
Merge pull request #624 from dnum-mi/tech/improve-dsfr-table-types
chore: 🏷️ improve typing
2 parents a80d9e4 + 1edebe4 commit 9568b87

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/components/DsfrTable/DsfrTable.vue

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import { ref, watch } from 'vue'
33
import DsfrTableRow, { type DsfrTableRowProps } from './DsfrTableRow.vue'
44
import DsfrTableHeaders from './DsfrTableHeaders.vue'
5-
import { type DsfrTableHeaderProps } from './DsfrTableHeader.vue'
5+
import { type DsfrTableHeadersProps } from './DsfrTableHeaders.vue'
66
77
const props = withDefaults(defineProps<{
88
title?: string
9-
headers?:(DsfrTableHeaderProps | string)[]
10-
rows?: (DsfrTableRowProps | string)[]
9+
headers?: DsfrTableHeadersProps
10+
rows?:(DsfrTableRowProps | string[])[]
1111
noCaption?: boolean
1212
pagination?: boolean
1313
defaultCurrentPage?: number
@@ -20,9 +20,8 @@ const props = withDefaults(defineProps<{
2020
defaultOptionSelected: 10,
2121
})
2222
23-
const getRowData = (row: (DsfrTableRowProps | string | ({component: string} & Record<string, any>))) => {
24-
// @ts-ignore TODO: find a way to improve types here
25-
return row.rowData || row
23+
const getRowData = (row: (DsfrTableRowProps | string[])) => {
24+
return Array.isArray(row) ? row : row.rowData
2625
}
2726
2827
const currentPage = ref(props.defaultCurrentPage)
@@ -33,12 +32,12 @@ const returnLowestLimit = () => currentPage.value * optionSelected.value - optio
3332
const returnHighestLimit = () => currentPage.value * optionSelected.value
3433
let truncatedResults = props.rows.slice(returnLowestLimit(), returnHighestLimit())
3534
36-
watch(() => optionSelected.value, (newVal, OldVal) => {
35+
watch(() => optionSelected.value, (newVal) => {
3736
props.rows.length > optionSelected.value ? pageCount.value = Math.ceil(props.rows.length / newVal) : pageCount.value = 1
3837
truncatedResults = props.rows.slice(returnLowestLimit(), returnHighestLimit())
3938
})
4039
41-
watch(() => currentPage.value, (newVal, OldVal) => {
40+
watch(() => currentPage.value, () => {
4241
truncatedResults = props.rows.slice(returnLowestLimit(), returnHighestLimit())
4342
})
4443
@@ -54,7 +53,6 @@ const goNextPage = () => {
5453
}
5554
}
5655
const goLastPage = () => { currentPage.value = pageCount.value }
57-
5856
</script>
5957

6058
<template>
@@ -85,7 +83,7 @@ const goLastPage = () => { currentPage.value = pageCount.value }
8583
v-for="(row, i) of truncatedResults"
8684
:key="i"
8785
:row-data="getRowData(row)"
88-
:row-attrs="typeof row === 'string' ? {} : row.rowAttrs"
86+
:row-attrs="'rowAttrs' in row ? row.rowAttrs : {}"
8987
/>
9088
</template>
9189
<tr v-if="pagination">

src/components/DsfrTable/DsfrTableHeaders.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script setup lang="ts">
22
import DsfrTableHeader, { DsfrTableHeaderProps } from './DsfrTableHeader.vue'
33
4-
type Header = DsfrTableHeaderProps & { text?: string }
4+
export type DsfrTableHeadersProps = (string | (DsfrTableHeaderProps & { text?: string }))[]
55
66
defineProps<{
7-
headers:(DsfrTableHeaderProps | string)[]
7+
headers: DsfrTableHeadersProps
88
}>()
99
</script>
1010

@@ -15,7 +15,7 @@ defineProps<{
1515
<DsfrTableHeader
1616
v-for="(header, i) of headers"
1717
:key="i"
18-
:header="(header as Header).text || (header as string)"
18+
:header="(typeof header === 'object' ? header : {}).text || (header as string)"
1919
:header-attrs="(header as DsfrTableHeaderProps).headerAttrs"
2020
/>
2121
</tr>

0 commit comments

Comments
 (0)