Skip to content

Commit 9794ead

Browse files
committed
refactor(DsfrPagination): ♻️ remanie les stories de DsfrPagination en CSF3
## Pourquoi les changements ont été faits : - Migration des stories de `DsfrPagination` vers le format CSF3 pour la cohérence et la maintenabilité. - Correction des contrôles interactifs pour le `v-model` qui ne fonctionnaient pas dans l'ancien format. ## Quelles modifications ont été apportées : - Remplacement de l'export par défaut par un objet `meta` typé. - Création d'une fonction `render` commune qui gère l'état de `currentPage` avec `ref` et `watch`. - Transformation des stories en objets `StoryObj`. - L'événement `update:currentPage` est maintenant tracé via `onUpdate:currentPage` dans `argTypes`. - La fonction de test `play` a été mise à jour pour utiliser les `args` du contexte.
1 parent 3d4f632 commit 9794ead

File tree

2 files changed

+118
-115
lines changed

2 files changed

+118
-115
lines changed
Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { expect, within } from 'storybook/test'
1+
import type { Meta, StoryObj } from '@storybook/vue3'
2+
3+
import { expect, fn, within } from '@storybook/test'
4+
import { ref, watch } from 'vue'
25

36
import DsfrPagination from './DsfrPagination.vue'
47

58
/**
69
* [Voir quand l’utiliser sur la documentation du DSFR](https://www.systeme-de-design.gouv.fr/version-courante/fr/composants/pagination)
710
*/
8-
export default {
11+
const meta = {
912
component: DsfrPagination,
1013
title: 'Composants/DsfrPagination',
1114
argTypes: {
@@ -40,133 +43,130 @@ export default {
4043
description:
4144
'Permet de limiter le nombre de pages affichées dans la pagination (avec un maximum de 5 pages)',
4245
},
43-
'update:currentPage': {
44-
description:
45-
'Événement émis lors du changement de page courante, avec en argument le numéro de page sélectionné',
46-
},
46+
'onUpdate:currentPage': fn(),
4747
},
48-
}
48+
} satisfies Meta<typeof DsfrPagination>
49+
50+
export default meta
51+
type Story = StoryObj<typeof meta>
4952

50-
export const Pagination = (args) => ({
53+
const render = (args: any) => ({
5154
components: {
5255
DsfrPagination,
5356
},
54-
55-
data () {
56-
return { ...args }
57+
setup () {
58+
const currentPage = ref(args.currentPage)
59+
watch(currentPage, (newPage) => {
60+
args['onUpdate:currentPage'](newPage)
61+
})
62+
return {
63+
args,
64+
currentPage,
65+
}
5766
},
58-
5967
template: `
6068
<DsfrPagination
61-
:pages="pages"
69+
:pages="args.pages"
6270
v-model:current-page="currentPage"
6371
/>
6472
`,
6573
})
66-
Pagination.args = {
67-
pages: [
68-
{
69-
label: '1',
70-
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:0',
71-
title: 'Page 1',
72-
},
73-
{
74-
label: '2',
75-
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:1',
76-
title: 'Page 2',
77-
},
78-
{
79-
label: '3',
80-
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:2',
81-
title: 'Page 3',
82-
},
83-
{
84-
label: '4',
85-
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:3',
86-
title: 'Page 4',
87-
},
88-
{
89-
label: '5',
90-
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:4',
91-
title: 'Page 5',
92-
},
93-
],
94-
currentPage: 0,
95-
}
96-
Pagination.play = async ({ canvasElement }) => {
97-
const canvas = within(canvasElement)
98-
const links = canvas.getAllByRole('link')
99-
expect(links).toHaveLength(9)
10074

101-
const currentPageLink = canvas.getByText(`${Pagination.args.currentPage + 1}`)
102-
expect(currentPageLink).toHaveAttribute('aria-current', 'page')
103-
const secondPageLink = canvas.getByText(`${Pagination.args.currentPage + 2}`)
104-
expect(secondPageLink).not.toHaveAttribute('aria-current', 'page')
105-
}
106-
107-
export const PaginationTruncated = (args) => ({
108-
components: {
109-
DsfrPagination,
75+
export const Pagination: Story = {
76+
render,
77+
args: {
78+
pages: [
79+
{
80+
label: '1',
81+
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:0',
82+
title: 'Page 1',
83+
},
84+
{
85+
label: '2',
86+
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:1',
87+
title: 'Page 2',
88+
},
89+
{
90+
label: '3',
91+
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:2',
92+
title: 'Page 3',
93+
},
94+
{
95+
label: '4',
96+
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:3',
97+
title: 'Page 4',
98+
},
99+
{
100+
label: '5',
101+
href: '/?path=/story/composants-pagination-pagination--pagination&args=currentPage:4',
102+
title: 'Page 5',
103+
},
104+
],
105+
currentPage: 0,
110106
},
107+
play: async ({ canvasElement, args }) => {
108+
const canvas = within(canvasElement)
109+
const links = canvas.getAllByRole('link')
110+
expect(links).toHaveLength(9)
111111

112-
data () {
113-
return { ...args }
112+
const currentPageLink = canvas.getByText(`${(args.currentPage || 0) + 1}`)
113+
expect(currentPageLink).toHaveAttribute('aria-current', 'page')
114+
const secondPageLink = canvas.getByText(`${(args.currentPage || 0) + 2}`)
115+
expect(secondPageLink).not.toHaveAttribute('aria-current', 'page')
114116
},
117+
}
115118

116-
template: `
117-
<DsfrPagination
118-
:pages="pages"
119-
v-model:currentPage="currentPage"
120-
/>
121-
`,
122-
})
123-
PaginationTruncated.args = {
124-
pages: [
125-
{
126-
label: '1',
127-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:0',
128-
title: 'Page 1',
129-
},
130-
{
131-
label: '2',
132-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:1',
133-
title: 'Page 2',
134-
},
135-
{
136-
label: '3',
137-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:2',
138-
title: 'Page 3',
139-
},
140-
{
141-
label: '4',
142-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:3',
143-
title: 'Page 4',
144-
},
145-
{
146-
label: '5',
147-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:4',
148-
title: 'Page 5',
149-
},
150-
{
151-
label: '6',
152-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:5',
153-
title: 'Page 6',
154-
},
155-
{
156-
label: '7',
157-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:6',
158-
title: 'Page 7',
159-
},
160-
{
161-
label: '8',
162-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:7',
163-
title: 'Page 8',
164-
},
165-
{
166-
label: '9',
167-
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:8',
168-
title: 'Page 9',
169-
},
170-
],
171-
currentPage: 4,
119+
export const PaginationTronquee: Story = {
120+
name: 'Pagination tronquée',
121+
render,
122+
args: {
123+
pages: [
124+
{
125+
label: '1',
126+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:0',
127+
title: 'Page 1',
128+
},
129+
{
130+
label: '2',
131+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:1',
132+
title: 'Page 2',
133+
},
134+
{
135+
label: '3',
136+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:2',
137+
title: 'Page 3',
138+
},
139+
{
140+
label: '4',
141+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:3',
142+
title: 'Page 4',
143+
},
144+
{
145+
label: '5',
146+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:4',
147+
title: 'Page 5',
148+
},
149+
{
150+
label: '6',
151+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:5',
152+
title: 'Page 6',
153+
},
154+
{
155+
label: '7',
156+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:6',
157+
title: 'Page 7',
158+
},
159+
{
160+
label: '8',
161+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:7',
162+
title: 'Page 8',
163+
},
164+
{
165+
label: '9',
166+
href: '?path=/story/composants-pagination-pagination--pagination-truncated&args=currentPage:8',
167+
title: 'Page 9',
168+
},
169+
],
170+
currentPage: 4,
171+
},
172172
}

src/components/DsfrPagination/DsfrPagination.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ const props = withDefaults(defineProps<DsfrPaginationProps>(), {
1515
ariaLabel: 'Pagination',
1616
})
1717
18-
const emit = defineEmits<{ (e: 'update:current-page', payload: number): void }>()
18+
const emit = defineEmits<{
19+
/** Émis lors de la mise à jour de la page courante */
20+
'update:current-page': [payload: number]
21+
}>()
1922
2023
const startIndex = computed(() => {
2124
return Math.min(props.pages.length - 1 - props.truncLimit, Math.max(props.currentPage - (props.truncLimit - props.truncLimit % 2) / 2, 0))

0 commit comments

Comments
 (0)