-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): refactor pagination into low-level
- Loading branch information
1 parent
bbca4da
commit e14281f
Showing
7 changed files
with
190 additions
and
138 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/clay-pagination/src/PaginationWithBasicItems.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/** | ||
* © 2019 Liferay, Inc. <https://liferay.com> | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
import ClayIcon from '@clayui/icon'; | ||
import {getEllipsisItems} from '@clayui/shared'; | ||
import React from 'react'; | ||
|
||
import Pagination from './Pagination'; | ||
|
||
const ELLIPSIS_BUFFER = 2; | ||
|
||
interface IProps extends React.ComponentProps<typeof Pagination> { | ||
/** | ||
* The page that is currently active. The first page is `1`. | ||
*/ | ||
activePage: number; | ||
|
||
/** | ||
* The number of pages to show on each side of the active page before | ||
* using an ellipsis dropdown. | ||
*/ | ||
ellipsisBuffer?: number; | ||
|
||
/** | ||
* The page numbers that should be disabled. For example, `[2,5,6]`. | ||
*/ | ||
disabledPages?: Array<number>; | ||
|
||
/** | ||
* Function used to create the href provided for each page link. | ||
*/ | ||
hrefConstructor?: (page?: number) => string; | ||
|
||
/** | ||
* Callback for when the active page changes. This is only used if | ||
* an href is not provided. | ||
*/ | ||
onPageChange?: (page?: number) => void; | ||
|
||
/** | ||
* The total number of pages in the pagination list. | ||
*/ | ||
totalPages: number; | ||
|
||
/** | ||
* Path to spritemap from clay-css. | ||
*/ | ||
spritemap?: string; | ||
} | ||
|
||
export const ClayPaginationWithBasicItems = React.forwardRef< | ||
HTMLUListElement, | ||
IProps | ||
>( | ||
( | ||
{ | ||
activePage, | ||
disabledPages = [], | ||
ellipsisBuffer = ELLIPSIS_BUFFER, | ||
hrefConstructor, | ||
onPageChange, | ||
spritemap, | ||
totalPages, | ||
...otherProps | ||
}, | ||
ref | ||
) => { | ||
const previousPage = activePage - 1; | ||
const previousHref = hrefConstructor && hrefConstructor(previousPage); | ||
|
||
const nextPage = activePage + 1; | ||
const nextHref = hrefConstructor && hrefConstructor(nextPage); | ||
|
||
const pages = Array(totalPages) | ||
.fill(0) | ||
.map((item, index) => index + 1); | ||
|
||
return ( | ||
<Pagination {...otherProps} ref={ref}> | ||
<Pagination.Item | ||
data-testid="prevArrow" | ||
disabled={activePage === 1} | ||
href={previousHref} | ||
onClick={() => onPageChange && onPageChange(previousPage)} | ||
> | ||
<ClayIcon spritemap={spritemap} symbol="angle-left" /> | ||
</Pagination.Item> | ||
|
||
{(ellipsisBuffer | ||
? getEllipsisItems( | ||
{ | ||
EllipsisComponent: Pagination.Ellipsis, | ||
ellipsisProps: { | ||
disabledPages, | ||
hrefConstructor, | ||
onPageChange, | ||
}, | ||
items: pages, | ||
}, | ||
ellipsisBuffer, | ||
activePage - 1 | ||
) | ||
: pages | ||
).map((page: number | JSX.Element | Object, index: number) => | ||
React.isValidElement(page) ? ( | ||
React.cloneElement(page, {key: `ellipsis${index}`}) | ||
) : ( | ||
<Pagination.Item | ||
active={page === activePage} | ||
disabled={disabledPages.includes(page as number)} | ||
href={ | ||
hrefConstructor && | ||
hrefConstructor(page as number) | ||
} | ||
key={page as number} | ||
onClick={() => | ||
onPageChange && onPageChange(page as number) | ||
} | ||
> | ||
{page} | ||
</Pagination.Item> | ||
) | ||
)} | ||
|
||
<Pagination.Item | ||
data-testid="nextArrow" | ||
disabled={activePage === totalPages} | ||
href={nextHref} | ||
onClick={() => onPageChange && onPageChange(nextPage)} | ||
> | ||
<ClayIcon spritemap={spritemap} symbol="angle-right" /> | ||
</Pagination.Item> | ||
</Pagination> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.