Skip to content

feat(pagination): added support to programmatically change active page #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -4,4 +4,6 @@ dist
dev
style/output.css
lib
.env
.env
.DS_Store
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -73,6 +73,7 @@ export default App
- Fork
- Clone
- `npm install`
- `npm rum predev`
- `npm run storybook`

It will start a local server at `localhost:6006` with all components rendered.
10 changes: 7 additions & 3 deletions src/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ export const PageButton: React.FC<PageButtonProps> = function PageButton({

export const EmptyPageButton = () => <span className="px-2 py-1">...</span>

interface PaginationProps {
export interface PaginationProps {
/**
* The total number of results
*/
@@ -89,6 +89,10 @@ interface PaginationProps {
* The accessible name of the pagination (what does it refer to?)
*/
label: string
/**
* The current active page
*/
activePage?: number
/**
* The function executed on page change
*/
@@ -98,9 +102,9 @@ interface PaginationProps {
type Ref = HTMLDivElement

const Pagination = React.forwardRef<Ref, PaginationProps>(function Pagination(props, ref) {
const { totalResults, resultsPerPage = 10, label, onChange, ...other } = props
const { totalResults, activePage: page = 1, resultsPerPage = 10, label, onChange, ...other } = props
const [pages, setPages] = useState<(number | string)[]>([])
const [activePage, setActivePage] = useState(1)
const [activePage, setActivePage] = useState(page)

const TOTAL_PAGES = Math.ceil(totalResults / resultsPerPage)
const FIRST_PAGE = 1
12 changes: 12 additions & 0 deletions src/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
@@ -298,4 +298,16 @@ describe('Pagination', () => {
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})

it('should start with different active page', () => {
const onChange = () => {}
const expected = 'bg-purple-600'

const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} activePage={3} label="Navigation" onChange={onChange} />
)

// Active page
expect(wrapper.find('button').at(3).getDOMNode().getAttribute('class')).toContain(expected)
})
})
30 changes: 30 additions & 0 deletions src/stories/Pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'

import { Story, Meta } from '@storybook/react/types-6-0'

import Pagination, { PaginationProps } from '../Pagination'

export default {
title: 'Pagination',
component: Pagination,
} as Meta

const Template: Story<PaginationProps> = (args) => <Pagination {...args} />

export const Current = Template.bind({})
Current.args = {
totalResults: 120,
resultsPerPage: 10,
onChange: () => {},
label: "Table navigation",
}

// With programmatically adjusted active page
export const ActivePage = Template.bind({})
ActivePage.args = {
totalResults: 50,
resultsPerPage: 15,
onChange: () => {},
label: "Table navigation",
activePage: 3,
}