Skip to content

Commit

Permalink
feat: Add Pagination component (#2188)
Browse files Browse the repository at this point in the history
* Add Pagination component

* Add test

* Update component to use Link

* Export component

* Update controls for totalPages

* Add optional callbacks

* Update sandbox to use optional callbacks

* Add onClick for clicking on a page number

* Add test for onClick handlers
  • Loading branch information
jcbcapps authored Sep 23, 2022
1 parent 09fe07b commit 4c021d1
Show file tree
Hide file tree
Showing 4 changed files with 647 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/components/Pagination/Pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React, { useEffect, useState } from 'react'
import { ComponentMeta, ComponentStory } from '@storybook/react'
import { Pagination } from './Pagination'

export default {
title: 'Components/Pagination',
component: Pagination,
argTypes: {
currentPage: { control: 'number' },
maxSlots: { control: 'number' },
pathname: { control: 'string' },
totalPages: { control: 'number' },
},
} as ComponentMeta<typeof Pagination>

const pathname = '/test-pathname'
const Template: ComponentStory<typeof Pagination> = (args) => {
const [current, setCurrentPage] = useState<number>(args.currentPage)

useEffect(() => {
if (args.currentPage >= args.totalPages) {
return
}
setCurrentPage(args.currentPage)
}, [args.currentPage])

const handleNext = () => {
const nextPage = current + 1
setCurrentPage(nextPage)
}

const handlePrevious = () => {
const prevPage = current - 1
setCurrentPage(prevPage)
}

const handlePageNumber = (
event: React.MouseEvent<HTMLButtonElement>,
pageNum: number
) => {
setCurrentPage(pageNum)
}

return (
<Pagination
totalPages={args.totalPages || 24}
currentPage={current}
maxSlots={args.maxSlots}
pathname={args.pathname}
onClickNext={handleNext}
onClickPrevious={handlePrevious}
onClickPageNumber={handlePageNumber}
/>
)
}

export const Sandbox = Template.bind({})
Sandbox.args = {
currentPage: 10,
maxSlots: 7,
}

export const Default = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={10} currentPage={10} />
)

export const ThreePagesFirst = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={3} currentPage={1} />
)
export const ThreePages = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={3} currentPage={2} />
)
export const ThreePagesLast = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={3} currentPage={3} />
)

export const SevenPages = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={7} currentPage={4} />
)

export const EightPagesFirst = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={8} currentPage={1} />
)

export const EightPagesFour = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={8} currentPage={4} />
)

export const EightPagesFive = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={8} currentPage={5} />
)

export const EightPagesSix = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={8} currentPage={6} />
)

export const EightPagesLast = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={8} currentPage={8} />
)

export const NinePagesFive = (): React.ReactElement => (
<Pagination pathname={pathname} totalPages={9} currentPage={5} />
)

export const TenSlots = (): React.ReactElement => (
<Pagination
pathname={pathname}
totalPages={24}
currentPage={10}
maxSlots={10}
/>
)
Loading

0 comments on commit 4c021d1

Please sign in to comment.