forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explorer TX pagination (MystenLabs#2377)
* pagination of tx by sequence number * max cal * update * update mock total txcount * prettier fix * fixed edge case where last pag number does not show * mobile optimazation * fix * lint fix
- Loading branch information
Showing
5 changed files
with
289 additions
and
75 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
explorer/client/src/components/pagination/Pagination.module.css
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,22 @@ | ||
.pagination { | ||
@apply flex flex-row items-center justify-center list-none; | ||
} | ||
|
||
.pagination ul { | ||
@apply flex flex-row items-center justify-center list-none pl-0; | ||
} | ||
|
||
.pagination ul li { | ||
@apply mr-2; | ||
} | ||
|
||
.pagination ul li button { | ||
@apply bg-gray-200 hover:bg-gray-300 focus:bg-gray-300 text-gray-800 hover:text-gray-800 focus:text-gray-800 font-medium py-2 px-4 rounded-none border-0 cursor-pointer | ||
md:pr-3 md:pl-3 pr-1.5 pl-1.5; | ||
|
||
transition: all 0.4s ease; | ||
} | ||
|
||
.activepag { | ||
@apply bg-sui text-white !important; | ||
} |
125 changes: 125 additions & 0 deletions
125
explorer/client/src/components/pagination/Pagination.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,125 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { useState, useCallback } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
|
||
import styles from './Pagination.module.css'; | ||
function generatePaginationArr( | ||
startAt: number, | ||
itemsPerPage: number, | ||
totalItems: number | ||
) { | ||
// number of list items to show before truncating | ||
const range: number = 4; | ||
const max = Math.ceil((totalItems - 1) / itemsPerPage); | ||
const maxRange = (Math.floor(startAt / range) + 1) * range; | ||
// set the min range to be the max range minus the range if it is less than the max - range | ||
const minRange = startAt <= max - range ? maxRange - range : max - range; | ||
return { | ||
max, | ||
maxRange, | ||
// generate array of numbers to show in the pagination where the total number of pages is the total tx value / items per page | ||
// show only the range eg if startAt is 5 and range is 5 then show 5, 6, 7, 8, 9, 10 | ||
listItems: Array.from({ length: max }, (_, i) => i + 1).filter( | ||
(x: number) => x >= minRange && x <= maxRange | ||
), | ||
range, | ||
}; | ||
} | ||
|
||
function Pagination({ | ||
totalTxCount, | ||
txNum, | ||
}: { | ||
totalTxCount: number; | ||
txNum: number; | ||
}) { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
const [pageIndex, setPage] = useState( | ||
parseInt(searchParams.get('p') || '1', 10) || 1 | ||
); | ||
const [pagiData, setPagiData] = useState( | ||
generatePaginationArr(pageIndex, txNum, totalTxCount) | ||
); | ||
|
||
const changePage = useCallback( | ||
(pageNum: number) => () => { | ||
setPage(pageNum); | ||
setSearchParams({ p: pageNum.toString() }); | ||
setPagiData(generatePaginationArr(pageNum, txNum, totalTxCount)); | ||
}, | ||
[setSearchParams, txNum, totalTxCount] | ||
); | ||
|
||
return ( | ||
<> | ||
<nav className={styles.pagination}> | ||
<ul> | ||
{pageIndex > 1 && ( | ||
<li className="page-item"> | ||
<button | ||
className={ | ||
pageIndex === 1 ? styles.activepag : '' | ||
} | ||
onClick={changePage(pageIndex - 1)} | ||
> | ||
← | ||
</button> | ||
</li> | ||
)} | ||
{pageIndex > pagiData.range - 1 && ( | ||
<li className="page-item"> | ||
<button | ||
className="page-link" | ||
onClick={changePage(1)} | ||
> | ||
1 | ||
</button> | ||
{' ... '} | ||
</li> | ||
)} | ||
{pagiData.listItems.map((itm: any, index: number) => ( | ||
<li className="page-item" key={index}> | ||
<button | ||
className={ | ||
pageIndex === itm ? styles.activepag : '' | ||
} | ||
onClick={changePage(itm)} | ||
> | ||
{itm} | ||
</button> | ||
</li> | ||
))} | ||
|
||
{pageIndex < pagiData.max - 1 && ( | ||
<li className="page-item"> | ||
{' ... '} | ||
<button | ||
className={ | ||
pageIndex === pagiData.max | ||
? styles.activepag | ||
: '' | ||
} | ||
onClick={changePage(pagiData.max)} | ||
> | ||
{pagiData.max} | ||
</button> | ||
</li> | ||
)} | ||
{pageIndex < pagiData.max && ( | ||
<li className="page-item"> | ||
<button | ||
className="page-link" | ||
onClick={changePage(pageIndex + 1)} | ||
> | ||
→ | ||
</button> | ||
</li> | ||
)} | ||
</ul> | ||
</nav> | ||
</> | ||
); | ||
} | ||
|
||
export default 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
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.