Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pagination: bug fixes for simplified pagination #581

Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 2 additions & 1 deletion src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ export const Carousel: FC<CarouselProps> = React.forwardRef(
onCurrentChange={(currentPage: number) =>
handleIndicatorClick(currentPage - 1)
}
pageSizes={[1]}
restrictPageSizesPropToSizesLayout
pageSize={1}
total={itemsNumber}
/>
)}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Pagination/Pager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const Pager: FC<PagerProps> = React.forwardRef(
/>
</li>
)}
{pageCount > 1 && showLast && (
{!simplified && pageCount > 1 && showLast && (
<li>
{!simplified ? (
<NeutralButton
Expand All @@ -248,6 +248,11 @@ export const Pager: FC<PagerProps> = React.forwardRef(
)}
</li>
)}
{simplified && pageCount > 0 && showLast && (
<li>
<span>{pageCount.toLocaleString()}</span>
</li>
)}
</ul>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Pagination/Pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const paginationArgs: Object = {
],
pageSize: 10,
pageSizes: [10, 20, 30, 40, 50, 100],
restrictPageSizesPropToSizesLayout: false,
hideWhenSinglePage: false,
total: 50,
'data-test-id': 'myPaginationTestId',
};
Expand Down
56 changes: 36 additions & 20 deletions src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const Pagination: FC<PaginationProps> = React.forwardRef(
currentPage = 1,
dots = false,
goToText: defaultGoToText,
hideWhenSinglePage = false,
layout = [
PaginationLayoutOptions.Previous,
PaginationLayoutOptions.Pager,
Expand All @@ -51,6 +52,7 @@ export const Pagination: FC<PaginationProps> = React.forwardRef(
quickNextIconButtonAriaLabel: defaultQuickNextIconButtonAriaLabel,
quickPreviousIconButtonAriaLabel: defaultQuickPreviousIconButtonAriaLabel,
total = 1,
restrictPageSizesPropToSizesLayout = false,
totalText: defaultTotalText,
selfControlled = true,
'data-test-id': dataTestId,
Expand Down Expand Up @@ -138,9 +140,21 @@ export const Pagination: FC<PaginationProps> = React.forwardRef(

useEffect((): void => {
setTotal(total);
onSizeChangeHandler?.(
pageSizes.indexOf(pageSize) > -1 ? pageSize : pageSizes[0]
);
if (
restrictPageSizesPropToSizesLayout
? layout.includes(PaginationLayoutOptions.Sizes)
: true
) {
onSizeChangeHandler?.(
pageSizes.indexOf(pageSize) > -1 ? pageSize : pageSizes[0]
);
}
if (
restrictPageSizesPropToSizesLayout &&
!layout.includes(PaginationLayoutOptions.Sizes)
) {
setPageCount(Math.ceil(total / _pageSize));
}
jumpToPage?.(currentPage);
}, []);

Expand Down Expand Up @@ -388,23 +402,25 @@ export const Pagination: FC<PaginationProps> = React.forwardRef(
}
/>
) : (
<Pager
currentPage={_currentPage}
key="pager"
locale={locale}
onCurrentChange={handleCurrentChange}
pageCount={getPageCount()}
quickNextIconButtonAriaLabel={
quickNextIconButtonAriaLabel
}
quickPreviousIconButtonAriaLabel={
quickPreviousIconButtonAriaLabel
}
simplified={true}
showLast={
!layout.includes(PaginationLayoutOptions.NoLast)
}
/>
(hideWhenSinglePage ? moreThanOnePage : true) && (
<Pager
currentPage={_currentPage}
key="pager"
locale={locale}
onCurrentChange={handleCurrentChange}
pageCount={getPageCount()}
quickNextIconButtonAriaLabel={
quickNextIconButtonAriaLabel
}
quickPreviousIconButtonAriaLabel={
quickPreviousIconButtonAriaLabel
}
simplified={true}
showLast={
!layout.includes(PaginationLayoutOptions.NoLast)
}
/>
)
)}
{layout.includes(PaginationLayoutOptions.Next) &&
moreThanOnePage && (
Expand Down
14 changes: 13 additions & 1 deletion src/components/Pagination/Pagination.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export interface PaginationProps extends OcBaseProps<HTMLElement> {
* @default 'Go to'
*/
goToText?: string;
/**
* Hide pagination when there is a single page.
* @default false
*/
hideWhenSinglePage?: boolean;
/**
* The Pagination layout options.
* @default {PaginationLayoutOptions.Previous, PaginationLayoutOptions.Pager, PaginationLayoutOptions.Next}
Expand Down Expand Up @@ -132,6 +137,7 @@ export interface PaginationProps extends OcBaseProps<HTMLElement> {
pageSizeButtonAriaLabel?: string;
/**
* The Pagination pageSizes array.
* pageSizes should be defined when layout uses PaginationLayoutOptions.Sizes
* @default {[10, 20, 30, 40, 50, 100]}
*/
pageSizes?: number[];
Expand All @@ -155,6 +161,12 @@ export interface PaginationProps extends OcBaseProps<HTMLElement> {
* @default 'Previous 5'
*/
quickPreviousIconButtonAriaLabel?: string;
/**
* pageSizes should only be defined for Sizes Layout.
* Recommended to turn this on as this is going to default behavior in future
* @default false
*/
restrictPageSizesPropToSizesLayout?: boolean;
/**
* The Page change is controlled internally.
* @default true
Expand All @@ -171,7 +183,7 @@ export interface PaginationProps extends OcBaseProps<HTMLElement> {
*/
simplified?: boolean;
/**
* The Pagination total number of pages.
* The Pagination total number of items.
* @default 1
*/
total: number;
Expand Down
1 change: 0 additions & 1 deletion src/components/Pagination/pagination.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@

.page-tracker {
opacity: 50%;
padding-top: $space-xxs;
}

&.dots {
Expand Down