usePagination
Press Enter to start editing
<ds-pagination
class="ds-pagination"
aria-label="Page navigation"
>
<ul>
<li>
<button
class="ds-button"
type="button"
aria-label="Previous page"
aria-hidden="false"
>Previous</button>
</li>
<li>
<button class="ds-button" type="button" aria-label="Page 1">1</button>
</li>
<li>
<button
class="ds-button"
type="button"
aria-label="Page 2"
aria-current="true"
>2</button>
</li>
<li>
<button class="ds-button" type="button" aria-label="Page 3">3</button>
</li>
<li></li>
<li>
<button class="ds-button" type="button" aria-label="Page 10">10</button>
</li>
<li>
<button
class="ds-button"
type="button"
aria-label="Next page"
aria-hidden="false"
>Next</button>
</li>
</ul>
</ds-pagination>const Preview = () => { const [currentPage, setCurrentPage] = useState(2); const { pages, nextButtonProps, prevButtonProps } = usePagination({ totalPages: 10, currentPage, setCurrentPage: setCurrentPage, showPages: 5, }); return ( <Pagination aria-label='Page navigation'> <Pagination.List> <Pagination.Item> <Pagination.Button aria-label='Previous page' {...prevButtonProps}> Previous </Pagination.Button> </Pagination.Item> {pages.map(({ page, itemKey, buttonProps }) => ( <Pagination.Item key={itemKey}> {typeof page === 'number' && ( <Pagination.Button aria-label={`Page ${page}`} {...buttonProps}> {page} </Pagination.Button> )} </Pagination.Item> ))} <Pagination.Item> <Pagination.Button aria-label='Next page' {...nextButtonProps}> Next </Pagination.Button> </Pagination.Item> </Pagination.List> </Pagination> ); }; render(<Preview />)
A React hook to handle pagination.
Props
In response you get the object:
const pagination = {
hasPrev: boolean, // Indicates if there is a previous page
hasNext: boolean, // Indicates if there is a next page
prevButtonProps: PaginationButtonProps, // Properties you can spread on the previous button
nextButtonProps: PaginationButtonProps, // Properties you can spread on the next button
pages: Array<{
page: number | string; // Number of page, or "ellipsis" string if the button should be ...
itemKey: string; // key you can pass to Pagination.Item to ensure correct focus handling
buttonProps: PaginationButtonProps; // Properties you can spread on Pagination.Button
}>,
};
Use
const onChange = (event, page: number) => console.log(`Going to page ${page}`);
const [currentPage, setCurrentPage] = useState(4);
const { pages, prevButtonProps, nextButtonProps, hasNext, hasPrevious } =
usePagination({
currentPage,
setCurrentPage,
onChange,
totalPages: 10,
showPages: 7,
});
On Pagination.Button
You loop through pages from the response object, and spread buttonProps on Pagination.Button.
Notice that we check typeof on page. This is because ellipses should be an empty li.
pages.map(({ page, itemKey, buttonProps }) => (
<Pagination.Item key={itemKey}>
{typeof page === "number" && (
<Pagination.Button asChild aria-label={`Page ${page}`} {...buttonProps}>
<a href={`#page-${page}`}>{page}</a>
</Pagination.Button>
)}
</Pagination.Item>
));