Skip to content

Commit

Permalink
feat(pagination): add currentPageText prop
Browse files Browse the repository at this point in the history
  • Loading branch information
bmestanov committed Nov 10, 2020
1 parent 1b53901 commit ed1ee00
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,27 @@ export const PageButton: React.FC<PageButtonProps> = function PageButton({

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

export interface CurrentPageTextParams {
pageStart: number,
pageEnd: number,
totalResults: number
}

const defaultCurrentPageText = ({ pageStart, pageEnd, totalResults }: CurrentPageTextParams): string =>
`Showing ${pageStart}-${pageEnd} of ${totalResults}`;

interface PaginationProps {
totalResults: number
resultsPerPage?: number
label: string
currentPageText?: (params: CurrentPageTextParams) => string,
onChange: (activePage: number) => void
}

type Ref = HTMLDivElement

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

Expand Down Expand Up @@ -150,9 +160,12 @@ const Pagination = React.forwardRef<Ref, PaginationProps>(function Pagination(pr
{/*
* This (label) should probably be an option, and not the default
*/}
<span className="flex items-center font-semibold tracking-wide uppercase">
Showing {activePage * resultsPerPage - resultsPerPage + 1}-
{Math.min(activePage * resultsPerPage, totalResults)} of {totalResults}
<span data-testid="current-page-text" className="flex items-center font-semibold tracking-wide uppercase">
{currentPageText({
pageStart: activePage * resultsPerPage - resultsPerPage + 1,
pageEnd: Math.min(activePage * resultsPerPage, totalResults),
totalResults,
})}
</span>

<div className="flex mt-2 sm:mt-auto sm:justify-end">
Expand Down
25 changes: 24 additions & 1 deletion src/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { mount } from 'enzyme'
import Pagination, { PageButton, NavigationButton, EmptyPageButton } from '../Pagination'
import Pagination, { PageButton, NavigationButton, EmptyPageButton, CurrentPageTextParams } from '../Pagination'

describe('NavigationButton', () => {
it('should render without crashing', () => {
Expand Down Expand Up @@ -298,4 +298,27 @@ describe('Pagination', () => {
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})

it('should work without currentPageText prop', () => {
const onChange = () => {}

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

expect(wrapper.find('[data-testid="current-page-text"]').text()).toEqual('Showing 1-5 of 30')
})

it('should use currentPageText prop for custom text formatting', () => {
const onChange = () => {}

const currentPageText = ({ pageStart, pageEnd, totalResults }: CurrentPageTextParams) =>
`Total results: ${totalResults}. Showing between ${pageStart} and ${pageEnd}.`

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

expect(wrapper.find('[data-testid="current-page-text"]').text()).toEqual('Total results: 30. Showing between 1 and 5.')
})
})

0 comments on commit ed1ee00

Please sign in to comment.