Skip to content

Commit

Permalink
feat: added prop to hide\show first\last pages for Pagination (#1616)
Browse files Browse the repository at this point in the history
feat: added prop to hide\show first\last pages

Co-authored-by: Denys Fedorov <[email protected]>
  • Loading branch information
denysfedorov and Denys Fedorov authored Aug 7, 2024
1 parent a3f374a commit eb0a056
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
17 changes: 17 additions & 0 deletions docs/pages/components/Pagination.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ import { Pagination } from '@deque/cauldron-react'
<Pagination totalItems={15} thin />
```

### Hiding Start and End Pagination Buttons

Using `hideStartEndPagination` prop, you can hide the "First" and "Last" page buttons.

```jsx example
<Pagination
totalItems={30}
hideStartEndPagination
/>
```

### Customizing Pagination Labels

The pagination component sets labels by default, but these labels can be overridden if you need to customize the labels based on the types of items that are being displayed or to provide the labels in a different locale.
Expand Down Expand Up @@ -157,6 +168,12 @@ The pagination hook returns an object containing the properties `pagination`, a
name: 'thin',
type: 'boolean',
description: 'Displays pagination with "thin" modifier (reduces the height of buttons and spacing)'
},
{
name: 'hideStartEndPagination',
type: 'boolean',
description: 'Hides "First page" and "Last page" buttons. Shows only "Next\Previous" buttons',
defaultValue: 'false'
}
]}
/>
42 changes: 24 additions & 18 deletions packages/react/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Props extends React.HTMLAttributes<HTMLDivElement> {
totalItems: number;
itemsPerPage?: number;
currentPage?: number;
hideStartEndPagination?: boolean;
statusLabel?: ContentNode;
firstPageLabel?: ContentNode;
previousPageLabel?: ContentNode;
Expand All @@ -28,6 +29,7 @@ const Pagination = React.forwardRef<HTMLDivElement, Props>(
totalItems,
itemsPerPage = 10,
currentPage = 1,
hideStartEndPagination = false,
statusLabel,
firstPageLabel = 'First page',
previousPageLabel = 'Previous page',
Expand Down Expand Up @@ -58,15 +60,17 @@ const Pagination = React.forwardRef<HTMLDivElement, Props>(
{...other}
>
<ul>
<li>
<IconButton
icon="chevron-double-left"
tooltipProps={{ placement: tooltipPlacement }}
label={firstPageLabel}
aria-disabled={isFirstPage}
onClick={isFirstPage ? undefined : onFirstPageClick}
/>
</li>
{!hideStartEndPagination && (
<li>
<IconButton
icon="chevron-double-left"
tooltipProps={{ placement: tooltipPlacement }}
label={firstPageLabel}
aria-disabled={isFirstPage}
onClick={isFirstPage ? undefined : onFirstPageClick}
/>
</li>
)}

<li>
<IconButton
Expand Down Expand Up @@ -99,15 +103,17 @@ const Pagination = React.forwardRef<HTMLDivElement, Props>(
/>
</li>

<li>
<IconButton
icon="chevron-double-right"
tooltipProps={{ placement: tooltipPlacement }}
label={lastPageLabel}
aria-disabled={isLastPage}
onClick={isLastPage ? undefined : onLastPageClick}
/>
</li>
{!hideStartEndPagination && (
<li>
<IconButton
icon="chevron-double-right"
tooltipProps={{ placement: tooltipPlacement }}
label={lastPageLabel}
aria-disabled={isLastPage}
onClick={isLastPage ? undefined : onLastPageClick}
/>
</li>
)}
</ul>
</div>
);
Expand Down
18 changes: 18 additions & 0 deletions packages/react/src/components/Pagination/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,21 @@ test('returns no axe violations', async () => {
const { container } = render(<Pagination totalItems={500} currentPage={3} />);
expect(await axe(container)).toHaveNoViolations();
});

test('should show start and end pagination buttons when hideStartEndPagination is not provided', () => {
render(<Pagination totalItems={18} currentPage={1} />);

expect(screen.getByText('First page')).toBeInTheDocument();
expect(screen.getByText('Previous page')).toBeInTheDocument();
expect(screen.getByText('Next page')).toBeInTheDocument();
expect(screen.getByText('Last page')).toBeInTheDocument();
});

test('should hide start and end pagination buttons when hideStartEndPagination is true', () => {
render(<Pagination totalItems={18} currentPage={1} hideStartEndPagination />);

expect(screen.queryByText('First page')).not.toBeInTheDocument();
expect(screen.queryByText('Last page')).not.toBeInTheDocument();
expect(screen.getByText('Previous page')).toBeInTheDocument();
expect(screen.getByText('Next page')).toBeInTheDocument();
});

0 comments on commit eb0a056

Please sign in to comment.