Show page numbers on low page count (#5206)

Shows individual page numbers instead of the page count selector when pages < 4.
This commit is contained in:
WithoutPants 2024-09-04 09:41:53 +10:00 committed by GitHub
parent 7b064ac99e
commit 4e9925fd3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import {
Overlay,
Popover,
} from "react-bootstrap";
import { FormattedMessage, useIntl } from "react-intl";
import { FormattedMessage, FormattedNumber, useIntl } from "react-intl";
import useFocus from "src/utils/focus";
import { Icon } from "../Shared/Icon";
import { faCheck, faChevronDown } from "@fortawesome/free-solid-svg-icons";
@ -143,6 +143,8 @@ interface IPaginationIndexProps {
metadataByline?: React.ReactNode;
}
const minPagesForCompact = 4;
export const Pagination: React.FC<IPaginationProps> = ({
itemsPerPage,
currentPage,
@ -156,6 +158,30 @@ export const Pagination: React.FC<IPaginationProps> = ({
[totalItems, itemsPerPage]
);
const pageButtons = useMemo(() => {
if (totalPages >= minPagesForCompact)
return (
<PageCount
totalPages={totalPages}
currentPage={currentPage}
onChangePage={onChangePage}
/>
);
const pages = [...Array(totalPages).keys()].map((i) => i + 1);
return pages.map((page: number) => (
<Button
variant="secondary"
key={page}
active={currentPage === page}
onClick={() => onChangePage(page)}
>
<FormattedNumber value={page} />
</Button>
));
}, [totalPages, currentPage, onChangePage]);
if (totalPages <= 1) return <div />;
return (
@ -176,13 +202,7 @@ export const Pagination: React.FC<IPaginationProps> = ({
>
&lt;
</Button>
<PageCount
totalPages={totalPages}
currentPage={currentPage}
onChangePage={onChangePage}
/>
{pageButtons}
<Button
variant="secondary"
disabled={currentPage === totalPages}