--- title: "Pagination" slug: pagination category: component type: examples version: 20.0.0 tokens: 1702 source: https://ovh.github.io/design-system/v20.0.0/llms/react-components-pagination--examples.txt --- # Pagination ## Default ```tsx ``` ## Controlled ```tsx { const [page, setPage] = useState(1); function handlePageChange({ page }: PaginationPageChangeDetail){ setPage(page); } return ( ); } ``` ## Disabled ```tsx ``` ## Items Per Page ```tsx ``` ## Links ```tsx { const [searchParams] = useSearchParams(); const totalItems = 500; // The URL can hold anything: a size that is not a positive integer falls back on 10, and the // page is kept within the listing. const size = Number(searchParams.get('size')); const pageSize = Number.isSafeInteger(size) && size > 0 ? size : 10; const requestedPage = Number(searchParams.get('page')); const page = Number.isInteger(requestedPage) ? Math.min(Math.max(requestedPage, 1), Math.ceil(totalItems / pageSize)) : 1; // Merged into the current query, so that a sort or a filter it holds survives the navigation. function getPageUrl({ page, pageSize }: PaginationPageUrlDetail) { const params = new URLSearchParams(searchParams); params.set('page', String(page)); params.set('size', String(pageSize)); return `?${params}`; } return ( ); } ``` ## With React Router ```tsx { const [searchParams] = useSearchParams(); const totalItems = 500; // The URL can hold anything: a size that is not a positive integer falls back on 10, and the // page is kept within the listing. const size = Number(searchParams.get('size')); const pageSize = Number.isSafeInteger(size) && size > 0 ? size : 10; const requestedPage = Number(searchParams.get('page')); const page = Number.isInteger(requestedPage) ? Math.min(Math.max(requestedPage, 1), Math.ceil(totalItems / pageSize)) : 1; // Merged into the current query, so that a sort or a filter it holds survives the navigation. function getPageUrl({ page, pageSize }: PaginationPageUrlDetail) { const params = new URLSearchParams(searchParams); params.set('page', String(page)); params.set('size', String(pageSize)); return `?${params}`; } // Created once: a new component on every render would remount the links, and lose the focus // of the one just activated. In an application, declare it at module scope. const PaginationLink = useMemo(() => function PaginationLink({ href, ...props }: ComponentPropsWithRef<'a'> & { href: string }) { return ( ); }, []); return ( ); } ``` ## Links With All Controls ```tsx { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const totalItems = 500; // The URL can hold anything: a size that is not a positive integer falls back on 10, and the // page is kept within the listing. const size = Number(searchParams.get('size')); const pageSize = Number.isSafeInteger(size) && size > 0 ? size : 10; const requestedPage = Number(searchParams.get('page')); const page = Number.isInteger(requestedPage) ? Math.min(Math.max(requestedPage, 1), Math.ceil(totalItems / pageSize)) : 1; // Merged into the current query, so that a sort or a filter it holds survives the navigation. function getPageUrl({ page, pageSize }: PaginationPageUrlDetail) { const params = new URLSearchParams(searchParams); params.set('page', String(page)); params.set('size', String(pageSize)); return `?${params}`; } // The pages are links and navigate on their own. // The two controls below are not, and have no href for the browser to follow: they report the // state they want, and the application navigates to the URL that describes it - the same URL // getPageUrl builds for the links. function handlePageChange({ page, pageSize }: PaginationPageChangeDetail) { navigate(getPageUrl({ page, pageSize })); } function handlePageSizeChange({ pageSize }: PaginationPageSizeChangeDetail) { navigate(getPageUrl({ page: 1, pageSize })); } return ( ); } ``` ## Links Vs Buttons ```tsx
`?page=${page}&size=${pageSize}` } totalItems={ 500 }>
``` ## Sibling Count ```tsx ``` ## With Tooltip Labels ```tsx
`?page=${page}&size=${pageSize}` } labelTooltipPrev="Go to previous page" labelTooltipNext="Go to next page" totalItems={ 500 }>
``` ## Page Size Selection ```tsx ``` ## Accessibility Label ```tsx ```