Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions packages/fuselage/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type PaginationProps = ComponentProps<typeof Box> & {
}) => string;
onSetCurrent?: Dispatch<SetStateAction<number>>;
onSetItemsPerPage?: Dispatch<SetStateAction<ItemsPerPage>>;
paginationAriaLabel?: string;
pageAriaLabel?: string;
};

const defaultItemsPerPageLabel = () => 'Items per page:';
Expand All @@ -42,6 +44,17 @@ const defaultShowingResultsLabel = ({
)} of ${count}`;

const itemsPerPageOptions = [25, 50, 100] as ItemsPerPage[];

function getPageAriaLabel(
pageAriaLabel: string | undefined,
pageNumber: number,
) {
if (pageAriaLabel) {
return `${pageAriaLabel} ${pageNumber}`;
}
return undefined;
}

export const Pagination = ({
count,
current = 0,
Expand All @@ -51,6 +64,8 @@ export const Pagination = ({
onSetItemsPerPage,
onSetCurrent,
divider,
paginationAriaLabel,
pageAriaLabel,
...props
}: PaginationProps) => {
const hasItemsPerPageSelection = itemsPerPageOptions.length > 1;
Expand Down Expand Up @@ -96,7 +111,14 @@ export const Pagination = ({
};

return (
<Box is='nav' rcx-pagination rcx-pagination--divider={divider} {...props}>
<Box
is='nav'
rcx-pagination
rcx-pagination--divider={divider}
role='navigation'
aria-label={`${paginationAriaLabel ?? undefined}`}
{...props}
>
{hasItemsPerPageSelection && (
<Box rcx-pagination__left>
<Box is='span' rcx-pagination__label>
Expand All @@ -120,15 +142,24 @@ export const Pagination = ({
</Box>
)}
<Box rcx-pagination__right>
<Box is='span' rcx-pagination__label>
<Box is='span' rcx-pagination__label id='paginationResultLabelId'>
{showingResultsLabel(renderingContext)}
</Box>
<Box is='ol' rcx-pagination__list>
<Box
is='ol'
rcx-pagination__list
aria-describedby='paginationResultLabelId'
>
<Box is='li' rcx-pagination__list-item>
<Box
is='button'
rcx-pagination__back
disabled={currentPage === 0}
aria-label={
currentPage === 0
? undefined
: getPageAriaLabel(pageAriaLabel, currentPage)
}
onClick={handleSetPageLinkClick(currentPage - 1)}
>
<Chevron left size='x16' />
Expand All @@ -143,6 +174,10 @@ export const Pagination = ({
is='button'
rcx-pagination__link
disabled={currentPage === page}
aria-label={getPageAriaLabel(
pageAriaLabel,
(page as number) + 1,
)}
onClick={handleSetPageLinkClick(page as number)}
>
{(page as number) + 1}
Expand All @@ -155,6 +190,11 @@ export const Pagination = ({
is='button'
rcx-pagination__forward
disabled={currentPage === pages - 1}
aria-label={
currentPage === pages - 1
? undefined
: getPageAriaLabel(pageAriaLabel, currentPage + 2)
}
onClick={handleSetPageLinkClick(currentPage + 1)}
>
<Chevron right size='x16' />
Expand Down