react-bootstrap#Pagination TypeScript Examples
The following examples show how to use
react-bootstrap#Pagination.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuestPage.tsx From apps with MIT License | 8 votes |
PhaseNavigator = (props: {
region: Region;
quest: Quest.QuestPhase;
currentPhase: number;
setPhase: (phase: number) => void;
}) => {
const currentPhase = props.currentPhase,
phases = props.quest.phases.sort((a, b) => a - b);
return (
<Pagination style={{ marginBottom: 0, float: "right" }}>
<Pagination.Prev
disabled={currentPhase === Math.min(...phases)}
onClick={() => {
props.setPhase(currentPhase - 1);
}}
/>
{props.quest.phases.map((phase) => (
<Pagination.Item
key={phase}
active={phase === currentPhase}
onClick={() => {
props.setPhase(phase);
}}
>
{phase}
</Pagination.Item>
))}
<Pagination.Next
disabled={currentPhase === Math.max(...phases)}
onClick={() => {
props.setPhase(currentPhase + 1);
}}
/>
</Pagination>
);
}
Example #2
Source File: BgmsPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] === 2) {
items.push(this.pageItem("2", 1, 1, false, false));
} else if (pages[0] > 2) {
items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
const lastNearbyPage = pages[pages.length - 1];
if (lastNearbyPage < maxPage) {
if (lastNearbyPage === maxPage - 2) {
items.push(this.pageItem(maxPage.toString(), maxPage - 1, maxPage - 1, false, false));
} else if (lastNearbyPage < maxPage - 2) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
}
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return (
<div className="page-navigator">
<Pagination>{items}</Pagination>
</div>
);
}
Example #3
Source File: CraftEssencesPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] === 2) {
items.push(this.pageItem("2", 1, 1, false, false));
} else if (pages[0] > 2) {
items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
const lastNearbyPage = pages[pages.length - 1];
if (lastNearbyPage < maxPage) {
if (lastNearbyPage === maxPage - 2) {
items.push(this.pageItem(maxPage.toString(), maxPage - 1, maxPage - 1, false, false));
} else if (lastNearbyPage < maxPage - 2) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
}
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return (
<div className="page-navigator">
<Pagination>{items}</Pagination>
</div>
);
}
Example #4
Source File: EventsPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] === 2) {
items.push(this.pageItem("2", 1, 1, false, false));
} else if (pages[0] > 2) {
items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
const lastNearbyPage = pages[pages.length - 1];
if (lastNearbyPage < maxPage) {
if (lastNearbyPage === maxPage - 2) {
items.push(this.pageItem(maxPage.toString(), maxPage - 1, maxPage - 1, false, false));
} else if (lastNearbyPage < maxPage - 2) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
}
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return (
<div className="page-navigator">
<Pagination>{items}</Pagination>
</div>
);
}
Example #5
Source File: ItemsPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] > 1) items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
if (pages[pages.length - 1] < maxPage) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
if (pages[pages.length - 1] < maxPage)
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return <Pagination>{items}</Pagination>;
}
Example #6
Source File: ServantsPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] === 2) {
items.push(this.pageItem("2", 1, 1, false, false));
} else if (pages[0] > 2) {
items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
const lastNearbyPage = pages[pages.length - 1];
if (lastNearbyPage < maxPage) {
if (lastNearbyPage === maxPage - 2) {
items.push(this.pageItem(maxPage.toString(), maxPage - 1, maxPage - 1, false, false));
} else if (lastNearbyPage < maxPage - 2) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
}
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return (
<div className="page-navigator">
<Pagination>{items}</Pagination>
</div>
);
}
Example #7
Source File: WarsPage.tsx From apps with MIT License | 5 votes |
private paginator(count: number): JSX.Element {
const items = [],
maxPage = Math.ceil(count / this.state.perPage) - 1,
bounds = 2,
nearbyPrev = [],
nearbyNext = [],
nearbyCount = bounds * 2 + 1;
for (let i = 0; i < bounds * 2; i++) {
const prev = this.state.page - i - 1;
if (prev >= 0) {
nearbyPrev.unshift(prev);
}
const next = this.state.page + i + 1;
if (next <= maxPage) {
nearbyNext.push(next);
}
}
while (nearbyPrev.length + nearbyNext.length + 1 > nearbyCount) {
if (nearbyNext.length > nearbyPrev.length) {
nearbyNext.pop();
} else {
nearbyPrev.shift();
}
}
const pages = nearbyPrev.concat([this.state.page], nearbyNext);
items.push(this.pageItem("<", this.state.page - 1, "prev", false, this.state.page <= 0));
if (pages[0] > 0) {
items.push(this.pageItem("1", 0, "first", false, false));
if (pages[0] === 2) {
items.push(this.pageItem("2", 1, 1, false, false));
} else if (pages[0] > 2) {
items.push(this.pageItem("…", 0, "firstEllipsis", false, true));
}
}
items.push(...pages.map((i) => this.pageItem((i + 1).toString(), i, i, i === this.state.page, false)));
const lastNearbyPage = pages[pages.length - 1];
if (lastNearbyPage < maxPage) {
if (lastNearbyPage === maxPage - 2) {
items.push(this.pageItem(maxPage.toString(), maxPage - 1, maxPage - 1, false, false));
} else if (lastNearbyPage < maxPage - 2) {
items.push(this.pageItem("…", maxPage, "lastEllipsis", false, true));
}
items.push(this.pageItem((maxPage + 1).toString(), maxPage, "last", false, false));
}
items.push(this.pageItem(">", this.state.page + 1, "next", false, this.state.page >= maxPage));
return (
<div className="page-navigator">
<Pagination>{items}</Pagination>
</div>
);
}
Example #8
Source File: AccountDetailsPage.tsx From devex with GNU General Public License v3.0 | 4 votes |
AccountDetailsPage: React.FC<IProps> = ({ addr }) => {
const networkContext = useContext(NetworkContext);
const { dataService, networkUrl, apolloUrl } = networkContext!;
const addrRef = useRef(addr);
const [isLoading, setIsLoading] = useState(false);
const [accData, setAccData] = useState<AccData | null>(null);
const [accContracts, setAccContracts] = useState<ContractObj[] | null>(null);
const [contractPageIndex, setContractPageIndex] = useState<number>(0);
const [transactionsCount, setTransactionsCount] = useState<number>(0);
const generatePagination = useCallback(
(currentPage: number, pageCount: number, delta = 2) => {
const separate = (a: number, b: number, isLower: boolean) => {
const temp = b - a;
if (temp === 0) return [a];
else if (temp === 1) return [a, b];
else if (temp === 2) return [a, a + 1, b];
else return [a, isLower ? -1 : -2, b];
};
return Array(delta * 2 + 1)
.fill(0)
.map((_, index) => currentPage - delta + index)
.filter((page) => 0 < page && page <= pageCount)
.flatMap((page, index, { length }) => {
if (!index) {
return separate(1, page, true);
}
if (index === length - 1) {
return separate(page, pageCount, false);
}
return [page];
});
},
[]
);
// Fetch data
useEffect(() => {
if (!dataService) return;
let accData: AccData;
let accContracts: ContractObj[];
const getData = async () => {
try {
setIsLoading(true);
accData = await dataService.getAccData(addrRef.current);
accContracts = await dataService.getAccContracts(addrRef.current);
if (accData) setAccData(accData);
if (accContracts) setAccContracts(accContracts);
} catch (e) {
setAccData({
balance: "0",
nonce: "-",
});
} finally {
setIsLoading(false);
}
};
getData();
}, [dataService]);
const ACCOUNT_TRANSACTIONS = gql`
query GetTransactions($addr: String!, $page: Int) {
txPagination(
page: $page
filter: {
OR: [
{ fromAddr: $addr }
{ toAddr: $addr }
{ receipt: { transitions: { addr: $addr } } }
{ receipt: { transitions: { msg: { _recipient: $addr } } } }
]
}
sort: TIMESTAMP_DESC
) {
count
items {
ID
receipt {
success
cumulative_gas
transitions {
addr
msg {
_recipient
}
}
}
gasPrice
gasLimit
fromAddr
toAddr
amount
timestamp
type
}
pageInfo {
currentPage
perPage
pageCount
itemCount
hasNextPage
hasPreviousPage
}
}
}
`;
const hexAddr = zilAddrToHexAddr(addr);
const {
loading: transactionsLoading,
error,
data: txData,
fetchMore,
} = useQuery(ACCOUNT_TRANSACTIONS, {
variables: { addr: hexAddr, page: 1 },
context: {
uri: apolloUrl,
},
fetchPolicy: "cache-and-network",
});
if (txData && transactionsCount !== txData.txPagination.count) {
setTransactionsCount(txData.txPagination.count);
}
const localFetch = (page: Number) => {
return fetchMore({
variables: {
page,
},
updateQuery: (prev: any, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;
return fetchMoreResult;
},
});
};
return (
<>
{isLoading ? (
<div className="center-spinner">
<Spinner animation="border" />
</div>
) : null}
{accData && (
<>
<div className="address-header">
<h3 className="mb-1">
<span className="mr-1">
<FontAwesomeIcon className="fa-icon" icon={faWallet} />
</span>
<span className="ml-2">Account</span>
<LabelStar type="Account" />
</h3>
<ViewBlockLink
network={networkUrl}
type="address"
identifier={addrRef.current}
/>
</div>
<div className="subtext">
<AddressDisp isLinked={false} addr={addrRef.current} />
</div>
<Card className="address-details-card">
<Card.Body>
<Container>
<Row>
<Col>
<div className="address-detail">
<span>Balance:</span>
<span>{qaToZil(accData.balance)}</span>
</div>
</Col>
</Row>
<Row>
<Col>
<div className="address-detail">
<span>Nonce:</span>
<span>{accData.nonce}</span>
</div>
</Col>
</Row>
<Row>
<Col>
<div className="address-detail">
<span>Transactions:</span>
<span>{transactionsCount}</span>
</div>
</Col>
</Row>
</Container>
</Card.Body>
</Card>
<h4 className="py-2">Transactions</h4>
<div className="row align-items-center mb-0">
<div className="col subtext">
Items Per Page: <strong>20</strong>
</div>
<div className="col">
{txData && txData.txPagination ? (
<Pagination className="justify-content-end">
<Pagination.Prev
onClick={() =>
localFetch(txData.txPagination.pageInfo.currentPage - 1)
}
disabled={!txData.txPagination.pageInfo.hasPreviousPage}
/>
{generatePagination(
txData.txPagination.pageInfo.currentPage + 1,
txData.txPagination.pageInfo.pageCount
).map((page) => {
if (page === -1)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
localFetch(
txData.txPagination.pageInfo.currentPage - 5
)
}
/>
);
else if (page === -2)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
localFetch(
txData.txPagination.pageInfo.currentPage + 5
)
}
/>
);
else if (page === txData.txPagination.pageInfo.currentPage)
return (
<Pagination.Item key={page} active>
{page}
</Pagination.Item>
);
else
return (
<Pagination.Item
key={page}
onClick={() => localFetch(Number(page))}
>
{page}
</Pagination.Item>
);
})}
<Pagination.Next
onClick={() =>
localFetch(txData.txPagination.pageInfo.currentPage + 1)
}
disabled={!txData.txPagination.pageInfo.hasNextPage}
/>
</Pagination>
) : null}
</div>
</div>
<Card className="txblock-card mt-0 mb-4">
<Card.Body>
{transactionsLoading ? (
<div className="center-spinner">
<Spinner animation="border" />
</div>
) : null}
{txData && txData.txPagination ? (
<TransactionsCard
transactions={txData.txPagination.items}
addr={addrRef.current}
/>
) : null}
{error ? "Error while loading transactions" : null}
</Card.Body>
</Card>
{accContracts && accContracts.length > 0 && (
<>
<h4 className="py-2">Deployed Contracts</h4>
<Card className="address-details-card">
<div className="d-flex justify-content-between">
<span className="num-contracts">
Total: {accContracts.length}{" "}
{accContracts.length === 1 ? "contract" : "contracts"}
</span>
<Pagination className="contract-pagination">
<Pagination.Prev
onClick={() =>
setContractPageIndex(
(contractPageIndex) => contractPageIndex - 1
)
}
disabled={contractPageIndex === 0}
/>
{generatePagination(
contractPageIndex,
Math.ceil(accContracts.length / 10)
).map((page) => {
if (page === -1)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
setContractPageIndex(
(contractPageIndex) => contractPageIndex - 5
)
}
/>
);
else if (page === -2)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
setContractPageIndex(
(contractPageIndex) => contractPageIndex + 5
)
}
/>
);
else if (page === contractPageIndex + 1)
return (
<Pagination.Item key={page} active>
{page}
</Pagination.Item>
);
else
return (
<Pagination.Item
key={page}
onClick={() => setContractPageIndex(page - 1)}
>
{page}
</Pagination.Item>
);
})}
<Pagination.Next
onClick={() =>
setContractPageIndex(
(contractPageIndex) => contractPageIndex + 1
)
}
disabled={
contractPageIndex ===
Math.ceil(accContracts.length / 10) - 1
}
/>
</Pagination>
</div>
<Card.Body>
{accContracts
.slice(10 * contractPageIndex, 10 * contractPageIndex + 10)
.map((contract: ContractObj, index: number) => {
return (
<AccContractCard
key={10 * contractPageIndex + index}
contract={contract}
index={10 * contractPageIndex + index}
/>
);
})}
</Card.Body>
</Card>
</>
)}
</>
)}
</>
);
}
Example #9
Source File: ContractTransactionsTab.tsx From devex with GNU General Public License v3.0 | 4 votes |
ContractTransactionsTab: React.FC<IProps> = ({
contractAddr,
onTransactionsCount,
fungibleToken,
}) => {
const [transactionsCount, setTransactionsCount] = useState<number>(0);
const networkContext = useContext(NetworkContext);
const { apolloUrl } = networkContext!;
const generatePagination = useCallback(
(currentPage: number, pageCount: number, delta = 2) => {
const separate = (a: number, b: number, isLower: boolean) => {
const temp = b - a;
if (temp === 0) return [a];
else if (temp === 1) return [a, b];
else if (temp === 2) return [a, a + 1, b];
else return [a, isLower ? -1 : -2, b];
};
return Array(delta * 2 + 1)
.fill(0)
.map((_, index) => currentPage - delta + index)
.filter((page) => 0 < page && page <= pageCount)
.flatMap((page, index, { length }) => {
if (!index) {
return separate(1, page, true);
}
if (index === length - 1) {
return separate(page, pageCount, false);
}
return [page];
});
},
[]
);
const ACCOUNT_TRANSACTIONS = gql`
query GetTransactions($addr: String!, $page: Int) {
txPagination(
page: $page
filter: {
OR: [
{ fromAddr: $addr }
{ toAddr: $addr }
{ receipt: { transitions: { addr: $addr } } }
{ receipt: { transitions: { msg: { _recipient: $addr } } } }
]
}
sort: TIMESTAMP_DESC
) {
count
items {
ID
receipt {
success
cumulative_gas
transitions {
addr
msg {
_recipient
}
}
}
gasPrice
gasLimit
fromAddr
toAddr
amount
timestamp
type
}
pageInfo {
currentPage
perPage
pageCount
itemCount
hasNextPage
hasPreviousPage
}
}
}
`;
const hexAddr = zilAddrToHexAddr(contractAddr);
const {
loading: transactionsLoading,
error,
data: txData,
fetchMore,
} = useQuery(ACCOUNT_TRANSACTIONS, {
variables: { addr: hexAddr, page: 1 },
context: {
uri: apolloUrl,
},
fetchPolicy: "cache-and-network",
});
if (txData && transactionsCount !== txData.txPagination.count) {
setTransactionsCount(txData.txPagination.count);
onTransactionsCount(txData.txPagination.count);
}
const localFetch = (page: Number) => {
return fetchMore({
variables: {
page,
},
updateQuery: (prev: any, { fetchMoreResult }) => {
if (!fetchMoreResult) return prev;
return fetchMoreResult;
},
});
};
return (
<div>
{transactionsLoading ? (
<div className="center-spinner">
<Spinner animation="border" />
</div>
) : null}
{txData && txData.txPagination ? (
<>
<div className="row align-items-center mb-0">
<div className="col subtext">
Items Per Page: <strong>20</strong>
</div>
<div className="col">
{txData && txData.txPagination ? (
<Pagination className="justify-content-end">
<Pagination.Prev
onClick={() =>
localFetch(txData.txPagination.pageInfo.currentPage - 1)
}
disabled={!txData.txPagination.pageInfo.hasPreviousPage}
/>
{generatePagination(
txData.txPagination.pageInfo.currentPage + 1,
txData.txPagination.pageInfo.pageCount
).map((page) => {
if (page === -1)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
localFetch(
txData.txPagination.pageInfo.currentPage - 5
)
}
/>
);
else if (page === -2)
return (
<Pagination.Ellipsis
key={page}
onClick={() =>
localFetch(
txData.txPagination.pageInfo.currentPage + 5
)
}
/>
);
else if (page === txData.txPagination.pageInfo.currentPage)
return (
<Pagination.Item key={page} active>
{page}
</Pagination.Item>
);
else
return (
<Pagination.Item
key={page}
onClick={() => localFetch(Number(page))}
>
{page}
</Pagination.Item>
);
})}
<Pagination.Next
onClick={() =>
localFetch(txData.txPagination.pageInfo.currentPage + 1)
}
disabled={!txData.txPagination.pageInfo.hasNextPage}
/>
</Pagination>
) : null}
</div>
</div>
<TransactionsCard
transactions={txData.txPagination.items}
fungibleToken={fungibleToken}
addr={contractAddr}
/>
</>
) : null}
{error ? "Error while loading transactions" : null}
</div>
);
}
Example #10
Source File: MinerTable.tsx From devex with GNU General Public License v3.0 | 4 votes |
MinerTable: React.FC<IMinerTableParams> = ({ addresses }) => {
const generatePagination = useCallback((currentPage: number, pageCount: number, delta = 1) => {
const separate = (a: number, b: number, isLower: boolean) => {
const temp = b - a
if (temp === 0)
return [a]
else if (temp === 1)
return [a, b]
else if (temp === 2)
return [a, a + 1, b]
else
return [a, isLower ? -1 : -2, b]
}
return Array(delta * 2 + 1)
.fill(0)
.map((_, index) => currentPage - delta + index)
.filter(page => 0 < page && page <= pageCount)
.flatMap((page, index, { length }) => {
if (!index) {
return separate(1, page, true)
}
if (index === length - 1) {
return separate(page, pageCount, false)
}
return [page]
})
}, [])
const columns = useMemo(
() => [{
id: 'address-col',
Header: 'Addresses',
accessor: 'address',
// eslint-disable-next-line react/display-name
Cell: (props: Cell<IMinerObj>) =>
(<>
[{props.row.index}]
{' '}
<QueryPreservingLink to={`/address/${pubKeyToZilAddr(props.value)}`}>{pubKeyToZilAddr(props.value)}</QueryPreservingLink>
</>)
}], []
) as Array<Column<IMinerObj>>
const data = useMemo(() => (addresses.map((x) => ({ address: x })) as IMinerObj[]), [addresses])
const {
getTableProps,
getTableBodyProps,
prepareRow,
page,
canPreviousPage,
canNextPage,
pageCount,
gotoPage,
nextPage,
previousPage,
state: { pageIndex },
} = useTable<IMinerObj>({
columns,
data,
initialState: { pageIndex: 0 },
}, usePagination)
return (
<>
<div className='py-3'>
<table {...getTableProps()}>
<tbody {...getTableBodyProps()}>
{page.map((row: Row<IMinerObj>) => {
prepareRow(row)
return (
<tr {...row.getRowProps()} key={row.getRowProps().key}>
{row.cells.map((cell: Cell<IMinerObj>) => {
return (
<td {...cell.getCellProps()} key={cell.getCellProps().key}>
{cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
</div>
<div className='mx-auto'>
{data.length !== 0 &&
<Pagination className='viewall-pagination'>
<Pagination.Prev onClick={() => previousPage()} disabled={!canPreviousPage} />
{generatePagination(pageIndex + 1, pageCount).map((page) => {
if (page === -1)
return <Pagination.Ellipsis key={page} onClick={() => gotoPage(pageIndex - 5)} />
else if (page === -2)
return <Pagination.Ellipsis key={page} onClick={() => gotoPage(pageIndex + 5)} />
else if (page === pageIndex + 1)
return <Pagination.Item key={page} active>{page}</Pagination.Item>
else
return <Pagination.Item key={page} onClick={() => gotoPage(Number(page) - 1)}>{page}</Pagination.Item>
})}
<Pagination.Next onClick={() => nextPage()} disabled={!canNextPage} />
</Pagination>
}
</div>
</>
)
}
Example #11
Source File: ViewAllTable.tsx From devex with GNU General Public License v3.0 | 4 votes |
ViewAllTable: React.FC<IViewAllTableParams<DsBlockObj | TxBlockObj | TransactionDetails>> =
({ columns, data, isLoading, fetchData, pageCount: controlledPageCount }) => {
const { getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page,
canPreviousPage,
canNextPage,
pageCount,
gotoPage,
nextPage,
previousPage,
// Get the state from the instance
state: { pageIndex } } = useTable<DsBlockObj | TxBlockObj | TransactionDetails>({
columns,
data,
initialState: { pageIndex: 0 },
manualPagination: true,
pageCount: controlledPageCount,
}, usePagination)
const fetchDataDebounce = useAsyncDebounce(fetchData, 300)
useEffect(() => {
fetchDataDebounce({ pageIndex })
// fetchDataDebounce changes when fetchData function changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pageIndex, fetchData])
const generatePagination = useCallback((currentPage: number, pageCount: number, delta = 2) => {
const separate = (a: number, b: number, isLower: boolean) => {
const temp = b - a
if (temp === 0)
return [a]
else if (temp === 1)
return [a, b]
else if (temp === 2)
return [a, a + 1, b]
else
return [a, isLower ? -1 : -2, b]
}
return Array(delta * 2 + 1)
.fill(0)
.map((_, index) => currentPage - delta + index)
.filter(page => 0 < page && page <= pageCount)
.flatMap((page, index, { length }) => {
if (!index) {
return separate(1, page, true)
}
if (index === length - 1) {
return separate(page, pageCount, false)
}
return [page]
})
}, [])
return (
<>
<BRow>
<BCol className='align-self-center pl-3'>
{data.length === 0
? null
: <span className='subtext'>Items Per Page: <strong>10</strong></span>}
</BCol>
<BCol>
<Pagination className='justify-content-end'>
<Pagination.Prev onClick={() => previousPage()} disabled={!canPreviousPage} />
{generatePagination(pageIndex + 1, pageCount).map((page) => {
if (page === -1)
return <Pagination.Ellipsis key={page} onClick={() => gotoPage(pageIndex - 5)} />
else if (page === -2)
return <Pagination.Ellipsis key={page} onClick={() => gotoPage(pageIndex + 5)} />
else if (page === pageIndex + 1)
return <Pagination.Item key={page} active>{page}</Pagination.Item>
else
return <Pagination.Item key={page} onClick={() => gotoPage(Number(page) - 1)}>{page}</Pagination.Item>
})}
<Pagination.Next onClick={() => nextPage()} disabled={!canNextPage} />
</Pagination>
</BCol>
</BRow>
<div className='viewall-table table'>
{isLoading ? <div className='center-spinner mt-4'><Spinner animation="border" /></div> : null}
<table {...getTableProps()}>
<thead>
{headerGroups.map((headerGroup: HeaderGroup<DsBlockObj | TxBlockObj | TransactionDetails>) => (
<tr {...headerGroup.getHeaderGroupProps()} key={headerGroup.getHeaderGroupProps().key} >
{headerGroup.headers.map((column) => (
<th {...column.getHeaderProps()} key={column.getHeaderProps().key} id={column.id}>
{column.render('Header')}
</th>
))}
</tr>
))}
</thead>
<tbody style={isLoading ? { opacity: '0.5' } : {}}{...getTableBodyProps()}>
{page.map((row: Row<DsBlockObj | TxBlockObj | TransactionDetails>) => {
prepareRow(row)
return (
<tr {...row.getRowProps()} key={row.getRowProps().key}>
{row.cells.map((cell: Cell<DsBlockObj | TxBlockObj | TransactionDetails>) => {
return (
<td {...cell.getCellProps()}
key={cell.getCellProps().key}>
{cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
</div>
</>
)
}
Example #12
Source File: paged-list.component.tsx From cwa-quick-test-frontend with Apache License 2.0 | 4 votes |
PagedList = (props: any) => {
const displayItemCount = 12;
const { t } = useTranslation();
const [data, setData] = React.useState<IQTArchiv[]>();
const [dataToFilter, setDataToFilter] = React.useState<IQTArchiv[]>([]);
const [dataToShow, setDataToShow] = React.useState<IQTArchiv[]>([]);
const [pages, setPages] = React.useState(0);
const [pageinationItems, setPageinationItems] = React.useState<JSX.Element[]>();
const [curPage, setCurPage] = React.useState(1);
const [filter, setFilter] = React.useState<string>('');
React.useEffect(() => {
setData(undefined);
setDataToShow([]);
setPages(0);
setFilter('');
props.onSelected('');
if (props && props.data) {
setData(props.data);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.data])
React.useEffect(() => {
if (data) {
setDataToFilter(data);
}
}, [data])
React.useEffect(() => {
if (pages) {
setCurPage(1);
}
else {
setCurPage(0)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pages])
React.useEffect(() => {
let _pages = 0;
if (dataToFilter) {
_pages = Math.ceil(dataToFilter.length / displayItemCount);
setPages(_pages);
}
if (curPage && curPage > 0 && curPage <= _pages && dataToFilter) {
const startIndex = (curPage - 1) * displayItemCount;
const endIndex = (curPage === _pages)
? dataToFilter.length
: (curPage * displayItemCount)
setDataToShow(dataToFilter.slice(startIndex, endIndex));
// pagination
const p: JSX.Element[] = [];
let index = (curPage === 1)
? curPage
: curPage - 1;
let max = (curPage + 1 < _pages)
? curPage + 1
: curPage;
let useBeginElipsis: boolean = (index > 2);
let useEndElipsis: boolean = (max + 1 < _pages);
if (1 < index) {
p.push(<Pagination.Item
key={1}
active={curPage === 1}
onClick={(evt: any) => handleClick(parseInt(evt.target.text))
}>
{1}
</Pagination.Item>);
}
if (useBeginElipsis) {
p.push(<Pagination.Ellipsis
key='eb' />);
}
for (index; index <= max; index++) {
p.push(<Pagination.Item
key={index}
active={curPage === index}
onClick={(evt: any) => handleClick(parseInt(evt.target.text))
}>
{index}
</Pagination.Item>);
}
if (useEndElipsis) {
p.push(<Pagination.Ellipsis
key='ee' />);
}
if (_pages > max) {
p.push(<Pagination.Item
key={_pages}
active={curPage === _pages}
onClick={(evt: any) => handleClick(parseInt(evt.target.text))
}>
{_pages}
</Pagination.Item>);
}
setPageinationItems(p);
}
else {
setDataToShow([]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [curPage, dataToFilter])
React.useEffect(() => {
let filterData = data;
if (filter && data) {
filterData = data.filter((item) => item.hashedGuid.startsWith(filter));
}
if (!filterData) {
filterData = [];
}
setDataToFilter(filterData);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filter])
const handleListSelect = (evt: any) => {
try {
const hash = evt.target.dataset['rbEventKey'];
if (hash) {
props.onSelected(hash);
}
} catch (error) {
}
}
const handleClick = (num: number) => {
if (!isNaN(num)) {
setCurPage(num);
}
}
return (dataToShow === undefined
? <CwaSpinner background='#eeeeee' />
:
<>
<Form.Control
className='qt-input'
value={filter}
onChange={(evt) => setFilter(evt.currentTarget.value)}
placeholder={t('translation:search')}
type='text'
maxLength={utils.shortHashLen}
/>
{ dataToShow.length === 0 ? <></> : <>
<hr />
<ListGroup>
{dataToShow.map((archiv, index) => (
<ListGroupItem
onClick={handleListSelect}
action
eventKey={archiv.hashedGuid}
key={archiv.hashedGuid}
>
{archiv.hashedGuid.substring(0, utils.shortHashLen)}
</ListGroupItem>
))}
{pages > 1 && (<>
<hr />
<Pagination size='sm' className='mb-0 justify-content-center' >
<Pagination.Prev disabled={curPage === 1} onClick={() => handleClick(curPage - 1)} />
{pageinationItems}
<Pagination.Next disabled={curPage === pages} onClick={() => handleClick(curPage + 1)} />
</Pagination>
</>)}
</ListGroup>
</>}
<hr />
</>
)
}