// @flow strict import * as React from 'react'; import {usePagination} from '../../hooks/usePagination'; import {useWindowSize} from '../../hooks/useWindowSize'; import classify from '../../utils/classify'; import {range} from '../../utils/helpers'; import {Dropdown} from '../Dropdown'; import {SubTitleExtraSmall} from '../Text'; import {PaginationItem} from './PaginationItem'; import css from './Pagination.module.css'; type ClassNames = $ReadOnly<{ wrapper?: string, children?: string, paginator?: string, }>; type MixedEvent = SyntheticEvent | Event; type PaginationItemType = | 'first' | 'previous' | 'page' | 'start-ellipsis' | 'end-ellipsis' | 'last' | 'next'; export type PaginationBaseProps = { currentPage?: number, disabled?: boolean, onChange?: (value?: ?number, event?: ?MixedEvent) => void, totalPages?: number, showFirstButton?: boolean, showLastButton?: boolean, hideNextButton?: boolean, hidePrevButton?: boolean, boundaryCount?: number, siblingCount?: number, style?: 'primary' | 'secondary', staticPaginationLabel?: string, }; export type PaginationProps = { children?: React.Node, ...PaginationBaseProps, classNames?: ClassNames, }; export type PaginationItemProps = { id: string, disabled: boolean, onClick: (event: SyntheticEvent) => void, page: ?number, selected: boolean, type: PaginationItemType, }; const LARGE_LIST_DROPDOWN_OPTIONS = 50; const TABLET_SCREEN_SIZE = 640; export const Pagination: React$AbstractComponent< PaginationProps, HTMLDivElement, > = React.forwardRef( (props: PaginationProps, ref) => { const {width} = useWindowSize(); const showExtraSecPaginationButtons = width > TABLET_SCREEN_SIZE; const { classNames, style = 'primary', children, currentPage = 1, totalPages = 1, onChange, showFirstButton = style !== 'primary' && showExtraSecPaginationButtons, showLastButton = style !== 'primary' && showExtraSecPaginationButtons, staticPaginationLabel, ...restPaginationProps } = props; const showPageSelectionDropDown = style !== 'primary' && showExtraSecPaginationButtons; const {items} = usePagination({ style, showFirstButton, showLastButton, currentPage, totalPages, onChange, ...restPaginationProps, }); const menuVirtualization = { enable: totalPages > LARGE_LIST_DROPDOWN_OPTIONS ? true : false, }; const allPages = range(1, totalPages); const options = style !== 'secondary' ? [] : allPages.map((page) => ({ key: page.toString(), label: page.toString(), })); return (
{children}
{style === 'secondary' && showPageSelectionDropDown && ( <> onChange?.(parseInt(option.key))} /> {!!totalPages && !!currentPage && ( {staticPaginationLabel ? staticPaginationLabel : `of ${totalPages} ${totalPages > 1 ? 'Pages' : 'Page'}`} )} )}
{items?.map((item) => ( ))}
); }, );