import * as stylex from '@stylexjs/stylex'; import React from 'react'; import { ChevronDoubleLeft } from '../../icons/glyphs/chevron-double-left'; import { ChevronDoubleRight } from '../../icons/glyphs/chevron-double-right'; import { ChevronLeft } from '../../icons/glyphs/chevron-left'; import { ChevronRight } from '../../icons/glyphs/chevron-right'; import { fill } from '../../localization/messages'; import type { MosaicElementProps } from '../../props'; import { mergeStyleProps, themeProps } from '../../props'; import { reset } from '../../styles/reset.styles'; import { rtl } from '../../styles/rtl.styles'; import { Button } from '../button'; import { Icon } from '../icon'; import { Select } from '../select'; import { styles } from './pagination.styles'; export interface PaginationProps extends Omit, 'onChange'> { page: number; totalItems: number; pageSize: number; onChange?: (page: number) => void; pageSizeOptions?: number[]; onPageSizeChange?: (pageSize: number) => void; hasFirstLast?: boolean; step?: number; siblingCount?: number; disabled?: boolean; label?: string; /** The item range, with `{start}`, `{end}` and `{total}` filled in. */ rangeLabel?: string; pageSizeLabel?: string; /** Stands in for `pageSizeLabel` when the pagination is too narrow to carry it. */ pageSizeLabelCompact?: string; firstPageLabel?: string; previousPageLabel?: string; nextPageLabel?: string; lastPageLabel?: string; } function atLeast(value: number, min: number): number { return Number.isFinite(value) && value > min ? Math.floor(value) : min; } const defaultPageSizeOptions = [10, 15, 20, 100]; /** * Page navigation for a paged list. Renders the current item range, a results-per-page control, * first/previous/next/last controls, and the current page over the total. Pages are 1-based; * `onChange` receives the page the user asked for. * * @example * */ export const Pagination = React.forwardRef(function MosaicPagination( { page, totalItems, pageSize, onChange, pageSizeOptions = defaultPageSizeOptions, onPageSizeChange, hasFirstLast = true, step = 1, siblingCount = 1, disabled = false, label = 'Pagination', rangeLabel = '{start}–{end} of {total}', pageSizeLabel = 'Results per page', pageSizeLabelCompact = 'Show', firstPageLabel = 'First page', previousPageLabel = 'Previous page', nextPageLabel = 'Next page', lastPageLabel = 'Last page', xstyle, ...rest }, ref, ) { const itemsPerPage = atLeast(pageSize, 1); const pageStep = atLeast(step, 1); const siblings = atLeast(siblingCount, 0); const itemCount = atLeast(totalItems, 0); const pageCount = Math.max(1, Math.ceil(itemCount / itemsPerPage)); const current = Math.min(atLeast(page, 1), pageCount); const start = itemCount === 0 ? 0 : (current - 1) * itemsPerPage + 1; const end = Math.min(current * itemsPerPage, itemCount); const pageSizeItems = React.useMemo(() => { const sizes = new Set( [...pageSizeOptions, itemsPerPage].filter(size => Number.isFinite(size) && size >= 1).map(Math.floor), ); return [...sizes].sort((a, b) => a - b).map(size => ({ value: size, label: String(size) })); }, [pageSizeOptions, itemsPerPage]); const isFirst = current <= 1; const isLast = current >= pageCount; const goTo = (next: number) => onChange?.(Math.min(Math.max(next, 1), pageCount)); return ( ); });