import { type ComponentPropsWithRef, type JSX, forwardRef, useEffect, useState } from 'react'; import { Pagination, PaginationPageSelector, PaginationPageSizeSelector, PaginationPages, type PaginationPageUrlDetail } from '../../src'; export default { component: Pagination, title: 'Tests navigation', }; const PAGE_SIZE = 10; function getPageUrl({ page, pageSize }: PaginationPageUrlDetail): string { return `#page-${page}-size-${pageSize}`; } function readHash(): PaginationPageUrlDetail { const match = window.location.hash.match(/^#page-(\d+)-size-(\d+)$/); return match ? { page: Number(match[1]), pageSize: Number(match[2]) } : { page: 1, pageSize: PAGE_SIZE }; } // The realistic wiring of link mode: the URL holds the page and the size, the component only // renders them. function LinkFromUrl(): JSX.Element { const [{ page, pageSize }, setDetail] = useState(readHash); useEffect(() => { function sync(): void { setDetail(readHash()); } window.addEventListener('hashchange', sync); return () => window.removeEventListener('hashchange', sync); }, []); return ( { window.location.hash = getPageUrl({ page: 1, pageSize: detail.pageSize }); } } page={ page } pageSize={ pageSize } totalItems={ 200 }> ); } export const link = () => ( ); // Stands for the link component of a routing library, and tags what it renders so that a test can // tell which controls were handed to it. const RouterLink = forwardRef>(function RouterLink(props, ref) { return ; }); export const linkAs = () => ( ); // The tooltip labels wrap the triggers in a TooltipTrigger, which is where the role comes from. export const linkWithTooltips = () => ( ); export const linkFromUrl = () => ; // Narrower than its nine cells, so that the row has to overflow. export const linkNarrow = () => ( ); // The same, with page numbers wider than a page button. export const linkNarrowLongPages = () => ( ); // Records every page change reported to the application, so that a test can tell which controls // report and which ones stay silent because the browser already followed a link. Sits on the last // page, so that a page past it brings the form back to the active one. export const linkReporting = () => ( { const store = window as unknown as { __pageChanges?: unknown[] }; store.__pageChanges = [...(store.__pageChanges ?? []), detail]; } } page={ 20 } pageSize={ PAGE_SIZE } totalItems={ 200 }> ); // The size selector has no link to follow either. Sits on page 18 of 20, past the 8 pages a size // of 25 leaves, so that a bigger size would leave the active page outside of the range. export const linkSizeSelection = () => ( { const store = window as unknown as { __pageChanges?: unknown[] }; store.__pageChanges = [...(store.__pageChanges ?? []), detail]; } } onPageSizeChange={ (detail) => { const store = window as unknown as { __pageSizeChanges?: unknown[] }; store.__pageSizeChanges = [...(store.__pageSizeChanges ?? []), detail]; } } page={ 18 } pageSize={ PAGE_SIZE } totalItems={ 200 }> ); // Button mode, already past the last page, so that submitting that same page brings it back to the // last one. export const buttonPastLastPage = () => ( { const store = window as unknown as { __pageChanges?: unknown[] }; store.__pageChanges = [...(store.__pageChanges ?? []), detail]; } } totalItems={ 200 }> );