Skip to content

Commit acb8c41

Browse files
authored
fix: Minor cleanup in Pagination stories (#3320)
1 parent 112226d commit acb8c41

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/components/Pagination/Pagination.stories.tsx

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React, { useState } from 'react'
22
import { Pagination } from './Pagination'
3-
import type { Meta, StoryObj } from '@storybook/react-vite'
3+
import type { Meta, StoryFn, StoryObj } from '@storybook/react-vite'
44

55
const pathname = '/test-pathname'
66

@@ -9,25 +9,36 @@ const meta: Meta<typeof Pagination> = {
99
component: Pagination,
1010
args: {
1111
pathname,
12+
currentPage: 1,
1213
},
1314
argTypes: {
14-
currentPage: { control: 'number' },
15-
maxSlots: { control: 'number' },
16-
totalPages: { control: 'number' },
15+
currentPage: {
16+
control: { type: 'number', min: 1 },
17+
},
18+
maxSlots: {
19+
control: { type: 'number', min: 1 },
20+
},
21+
totalPages: {
22+
control: { type: 'number', min: 1 },
23+
},
1724
},
1825
}
1926
export default meta
2027
type Story = StoryObj<typeof Pagination>
2128

22-
const Template = ({ ...args }) => {
23-
const [current, setCurrentPage] = useState<number>(args.currentPage)
29+
const Template: StoryFn<typeof Pagination> = (args) => {
30+
const argPage =
31+
args.totalPages !== undefined
32+
? Math.min(args.currentPage, args.totalPages)
33+
: args.currentPage
2434

25-
useEffect(() => {
26-
if (args.totalPages && args.currentPage >= args.totalPages) {
27-
return
28-
}
29-
setCurrentPage(args.currentPage)
30-
}, [args.currentPage])
35+
const [current, setCurrentPage] = useState<number>(argPage)
36+
37+
const [prevArgPage, setPrevArgPage] = useState(argPage)
38+
if (argPage !== prevArgPage) {
39+
setPrevArgPage(argPage)
40+
setCurrentPage(argPage)
41+
}
3142

3243
const handleNext = () => {
3344
const nextPage = current + 1
@@ -51,7 +62,7 @@ const Template = ({ ...args }) => {
5162
totalPages={args.totalPages}
5263
currentPage={current}
5364
maxSlots={args.maxSlots}
54-
pathname={pathname}
65+
pathname={args.pathname}
5566
onClickNext={handleNext}
5667
onClickPrevious={handlePrevious}
5768
onClickPageNumber={handlePageNumber}

0 commit comments

Comments
 (0)