Skip to content

Commit d62a619

Browse files
authored
Merge pull request #988 from vdizengremel/fix/dsfrtable-update-page-count
fix(DsfrTable): mise à jour du nombre de pages quand les lignes du tableau sont mises à jour
2 parents fff77ee + a4f071e commit d62a619

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

src/components/DsfrTable/DsfrTable.spec.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from '@testing-library/vue'
1+
import {fireEvent, render, waitFor} from '@testing-library/vue'
22

33
import DsfrTag from '../DsfrTag/DsfrTag.vue'
44

@@ -99,4 +99,84 @@ describe('DsfrTable', () => {
9999
// Then
100100
expect(trs).toHaveLength(0)
101101
})
102+
103+
it('should update page count when rows change', async () => {
104+
// Given
105+
const title = 'Utilisateurs'
106+
107+
const headers = ['Nom', 'Prénom']
108+
const rows = generateRowsWithTwoColumns(5);
109+
const props = {
110+
title,
111+
headers,
112+
rows,
113+
pagination: true
114+
}
115+
116+
const component = render(DsfrTable, {
117+
global: {
118+
components: {
119+
VIcon,
120+
DsfrTag,
121+
},
122+
},
123+
props,
124+
})
125+
126+
// When
127+
const newRows = generateRowsWithTwoColumns(25);
128+
129+
component.rerender({
130+
...props,
131+
rows: newRows,
132+
})
133+
134+
// Then
135+
await waitFor(() => {
136+
expect(component.getByText('Page 1 sur 3')).toBeDefined()
137+
})
138+
})
139+
140+
it('should update page count when selected different number of results per page', async () => {
141+
// Given
142+
const title = 'Utilisateurs'
143+
144+
const headers = ['Nom', 'Prénom']
145+
const rows = generateRowsWithTwoColumns(50);
146+
const props = {
147+
title,
148+
headers,
149+
rows,
150+
pagination: true
151+
}
152+
153+
const component = render(DsfrTable, {
154+
global: {
155+
components: {
156+
VIcon,
157+
DsfrTag,
158+
},
159+
},
160+
props,
161+
})
162+
163+
// When
164+
const select = component.getByLabelText('Résultats par page :')
165+
await fireEvent.update(select, '25')
166+
167+
// Then
168+
await waitFor(() => {
169+
expect(component.getByText('Page 1 sur 2')).toBeDefined()
170+
})
171+
})
172+
173+
function generateRowsWithTwoColumns(numberOfRows: number): string[][] {
174+
const rows: string[][] = []
175+
176+
for (let i = 0; i < numberOfRows; i++) {
177+
rows.push(['EGAUD', 'Pierre-Louis'])
178+
}
179+
180+
return rows
181+
}
102182
})

src/components/DsfrTable/DsfrTable.vue

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { computed, ref, watch } from 'vue'
44
import DsfrTableHeaders from './DsfrTableHeaders.vue'
55
import DsfrTableRow, { type DsfrTableRowProps } from './DsfrTableRow.vue'
66
import type { DsfrTableProps } from './DsfrTable.types'
7+
import {getRandomId} from "@/utils/random-utils";
78
89
export type { DsfrTableProps }
910
@@ -24,23 +25,15 @@ const getRowData = (row: DsfrTableProps['rows']) => {
2425
2526
const currentPage = ref(props.currentPage)
2627
const optionSelected = ref(props.resultsDisplayed)
27-
const pageCount = ref(
28+
const pageCount = computed(() =>
2829
props.rows.length > optionSelected.value
2930
? Math.ceil(props.rows.length / optionSelected.value)
30-
: 1,
31+
: 1
3132
)
3233
const paginationOptions = [5, 10, 25, 50, 100]
3334
const returnLowestLimit = () => currentPage.value * optionSelected.value - optionSelected.value
3435
const returnHighestLimit = () => currentPage.value * optionSelected.value
3536
36-
watch(
37-
() => optionSelected.value,
38-
(newVal) => {
39-
pageCount.value =
40-
props.rows.length > optionSelected.value ? Math.ceil(props.rows.length / newVal) : 1
41-
},
42-
)
43-
4437
const truncatedResults = computed(() => {
4538
if (props.pagination) {
4639
return props.rows.slice(returnLowestLimit(), returnHighestLimit())
@@ -69,6 +62,8 @@ const goLastPage = () => {
6962
currentPage.value = pageCount.value
7063
emit('update:currentPage')
7164
}
65+
66+
const selectId = getRandomId('resultPerPage')
7267
</script>
7368

7469
<template>
@@ -110,8 +105,9 @@ const goLastPage = () => {
110105
<td :colspan="headers.length">
111106
<div class="flex justify-right">
112107
<div class="self-center">
113-
<span>Résultats par page : </span>
108+
<label :for="selectId">Résultats par page : </label>
114109
<select
110+
:id="selectId"
115111
v-model="optionSelected"
116112
@change="emit('update:currentPage')"
117113
>

0 commit comments

Comments
 (0)