import Icon from "../../common/Icon"; import { PaginationPageProps, PaginationProps } from "./types"; import { LinkComponentProps } from "../../shared-types"; import clsx from 'clsx'; export const Page = ({ ariaLabel, children, className, href, isCurrent = false, linkComponent, onClick }: PaginationPageProps) => { function handleClick(event: React.MouseEvent) { /* istanbul ignore else */ if (typeof onClick === 'function') { onClick(event); } } function processChildren(children: React.ReactNode) { const classNames = clsx([ 'ds_pagination__link', className, isCurrent && 'ds_current' ]); const linkProps: LinkComponentProps = { 'aria-label': ariaLabel, href: href, onClick: handleClick }; if (isCurrent) { linkProps['aria-current'] = 'page'; } if (linkComponent) { return linkComponent({ className: classNames, children: children, ...linkProps }); } else if (href) { return ( {children} ); } } return (
  • {processChildren(children)}
  • ); }; export const Ellipsis = () => { return ( ); }; const Pagination = ({ ariaLabel = 'Pages', className, onClick, padding = 1, page = 1, pattern = '/search?page=$1', totalPages, linkComponent, ...props }: PaginationProps) => { padding = Number(padding); page = Number(page); const minToShow = padding + 2; let includeFirst, includeLast; let pages = []; if (page <= minToShow) { for (let i = 1; i <= minToShow + 1; i++) { pages.push(Number(i)); } } else if (page <= totalPages - minToShow) { pages = [page]; for (let i = 0; i < padding; i++) { pages.unshift(page - 1 - i); pages.push(Number(page) + 1 + i); } } else { for (let i = totalPages; i > totalPages - minToShow - 1; i--) { pages.push(Number(i)); } pages.reverse(); } // filter out pages that are out of bounds pages = pages.filter(item => item > 0 && item <= totalPages); if ((page - padding) > 2) { includeFirst = true; } if ((page + padding < totalPages - 1)) { includeLast = true; } return ( ) } Pagination.displayName = 'Pagination'; export default Pagination;