Skip to content

Commit

Permalink
feat(pagination): added support to programmatically change active page
Browse files Browse the repository at this point in the history
There are cases where users needs to programmatically change the active page number, eg. when
searching, the page should change to first page.

feat estevanmaito#42
  • Loading branch information
altamira-sykora committed Mar 17, 2021
1 parent b7afcfd commit a716486
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions src/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit a716486

Please sign in to comment.