-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #743 from petermakowski/feat-add-usePagination-hook
feat: add usePagination hook
- Loading branch information
Showing
4 changed files
with
217 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
|
||
import { usePagination } from "./usePagination"; | ||
|
||
it("returns correct data", () => { | ||
const { result } = renderHook(() => | ||
usePagination([1, 2, 3], { itemsPerPage: 2 }) | ||
); | ||
const { pageData, currentPage, paginate, itemsPerPage, totalItems } = | ||
result.current; | ||
expect(currentPage).toBe(1); | ||
expect(pageData).toEqual([1, 2]); | ||
expect(paginate).toBeInstanceOf(Function); | ||
expect(itemsPerPage).toBe(2); | ||
expect(totalItems).toBe(3); | ||
}); | ||
|
||
it("correctly sets the initial page", () => { | ||
const { result } = renderHook(() => | ||
usePagination([1, 2], { itemsPerPage: 1, initialPage: 2 }) | ||
); | ||
const { pageData, currentPage } = result.current; | ||
expect(currentPage).toBe(2); | ||
expect(pageData).toEqual([2]); | ||
}); | ||
|
||
it("goes to the last available page if the current page is out of bounds", () => { | ||
const options = { itemsPerPage: 1, initialPage: 3 }; | ||
const { result, rerender } = renderHook( | ||
({ data, options }) => usePagination(data, options), | ||
{ | ||
initialProps: { | ||
data: [1, 2, 3], | ||
options, | ||
}, | ||
} | ||
); | ||
expect(result.current.currentPage).toBe(3); | ||
expect(result.current.pageData).toEqual([3]); | ||
rerender({ data: [1, 2], options }); | ||
expect(result.current.currentPage).toBe(2); | ||
expect(result.current.pageData).toEqual([2]); | ||
}); | ||
|
||
it("go to the initial page if autoResetPage is true", () => { | ||
const options = { itemsPerPage: 1, initialPage: 3, autoResetPage: true }; | ||
const { result, rerender } = renderHook( | ||
({ data, options }) => usePagination(data, options), | ||
{ | ||
initialProps: { | ||
data: [1, 2, 3], | ||
options, | ||
}, | ||
} | ||
); | ||
expect(result.current.currentPage).toBe(3); | ||
expect(result.current.pageData).toEqual([3]); | ||
rerender({ data: [1, 2], options }); | ||
expect(result.current.currentPage).toBe(1); | ||
expect(result.current.pageData).toEqual([1]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useMemo, useEffect, useState } from "react"; | ||
|
||
/** | ||
* A hook that handles pagination. | ||
* @param data - The data array to paginate. | ||
* @param {Object} options | ||
* @param {number} [options.itemsPerPage] - Number of items per page. Returns all items if no value has been provided. | ||
* @param {number} [options.initialPage=1] - Initial page number. Defaults to 1. | ||
* @param {boolean} [options.autoResetPage=false] - Whether to reset the page number to 1 when the data changes. | ||
*/ | ||
|
||
export function usePagination<D, I = number | null>( | ||
data: Array<D>, | ||
options?: { | ||
itemsPerPage: I; | ||
initialPage?: number; | ||
autoResetPage?: boolean; | ||
} | ||
): { | ||
pageData: Array<D>; | ||
currentPage: number; | ||
paginate: (pageNumber: number) => void; | ||
itemsPerPage: I; | ||
totalItems: number; | ||
} { | ||
const defaultOptions = { | ||
initialPage: 1, | ||
autoResetPage: false, | ||
}; | ||
const { itemsPerPage, initialPage, autoResetPage } = Object.assign( | ||
defaultOptions, | ||
options | ||
); | ||
const totalItems = data?.length ?? 0; | ||
const initialPageIndex = initialPage > 0 ? initialPage - 1 : 0; | ||
const [pageIndex, setPageIndex] = useState(initialPageIndex); | ||
const startIndex = | ||
typeof itemsPerPage === "number" ? pageIndex * itemsPerPage : 0; | ||
const paginate = (pageNumber: number) => setPageIndex(pageNumber - 1); | ||
|
||
useEffect(() => { | ||
if (typeof itemsPerPage === "number" && startIndex >= totalItems) { | ||
!autoResetPage && Math.floor(totalItems / itemsPerPage) > 0 | ||
? // go to the last available page if the current page is out of bounds | ||
setPageIndex(Math.floor(totalItems / itemsPerPage) - 1) | ||
: // go to the initial page if autoResetPage is true | ||
setPageIndex(0); | ||
} | ||
}, [ | ||
pageIndex, | ||
startIndex, | ||
setPageIndex, | ||
totalItems, | ||
itemsPerPage, | ||
autoResetPage, | ||
]); | ||
|
||
const pageData = useMemo( | ||
() => | ||
typeof itemsPerPage === "number" | ||
? data?.slice(startIndex, startIndex + itemsPerPage) | ||
: data, | ||
[startIndex, data, itemsPerPage] | ||
); | ||
|
||
return { | ||
pageData, | ||
currentPage: pageIndex + 1, | ||
paginate, | ||
itemsPerPage, | ||
totalItems, | ||
}; | ||
} |