This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created LoadMoreTable and Button components
- Loading branch information
Showing
9 changed files
with
3,272 additions
and
137 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { Text, StyleSheet, Pressable } from 'react-native'; | ||
|
||
export default function Button(props) { | ||
const { onPress, title = 'Load More' } = props; | ||
return ( | ||
<Pressable style={styles.button} onPress={onPress}> | ||
<Text style={styles.text}>{title}</Text> | ||
</Pressable> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
button: { | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
paddingVertical: 12, | ||
paddingHorizontal: 32, | ||
borderRadius: 4, | ||
elevation: 3, | ||
backgroundColor: 'black', | ||
}, | ||
text: { | ||
fontSize: 16, | ||
lineHeight: 21, | ||
fontWeight: 'bold', | ||
letterSpacing: 0.25, | ||
color: 'white', | ||
}, | ||
}); |
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,123 @@ | ||
import { | ||
Box, | ||
HStack, | ||
Button, | ||
Divider, | ||
Stack, | ||
BoxProps, | ||
} from "@chakra-ui/react"; | ||
import React from "react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
type PageChangeCallback = (page: number) => void; | ||
type ItemChangeCallback = (items: number) => void; | ||
|
||
export interface PaginationProps extends BoxProps { | ||
/** Total number of items to paginate */ | ||
total: number; | ||
/** Array with all available "items per page" amounts */ | ||
possibleItemsPerPage?: Array<number>; | ||
/** External bind for current page */ | ||
page?: number; | ||
/** Number of items to display per page */ | ||
limit?: number; | ||
/** Callback for page change events */ | ||
onPageChange?: PageChangeCallback; | ||
/** Callback for item count change event */ | ||
onItemCountChange?: ItemChangeCallback; | ||
} | ||
|
||
const CursorPaginator = ({ | ||
total, | ||
possibleItemsPerPage = [5, 10, 15, 20, 30, 50, 100], | ||
limit = 5, | ||
page, | ||
onPageChange, | ||
onItemCountChange, | ||
...props | ||
}: PaginationProps) => { | ||
const [internalPage, setInternalPage] = useState(page || 1); | ||
// Use limit set in instance, unless it does not exist in the list of possible items per page. | ||
// Default to middle. | ||
const [itemsPerPage] = useState( | ||
possibleItemsPerPage.includes(limit) | ||
? limit | ||
: possibleItemsPerPage[Math.floor(possibleItemsPerPage.length / 2)], | ||
); | ||
const [pageAmount, setPageAmount] = useState(1); | ||
|
||
useEffect(() => { | ||
if (page) { | ||
setInternalPage(page); | ||
} | ||
}, [page]); | ||
|
||
useEffect(() => { | ||
if (onPageChange !== undefined) { | ||
onPageChange(internalPage); | ||
} | ||
}, [internalPage, onPageChange]); | ||
|
||
useEffect(() => { | ||
if (onItemCountChange !== undefined) { | ||
onItemCountChange(itemsPerPage); | ||
} | ||
}, [itemsPerPage, onItemCountChange]); | ||
|
||
|
||
useEffect(() => { | ||
const newPageAmount = Math.ceil(total / itemsPerPage); | ||
setInternalPage((prevPage) => (prevPage > newPageAmount ? 1 : prevPage)); | ||
setPageAmount(newPageAmount); | ||
}, [total, itemsPerPage, setInternalPage]); | ||
|
||
return ( | ||
<Box py={2} {...props}> | ||
<Stack w='100%' direction={{ base: "column", md: "row" }}> | ||
<HStack> | ||
<Button | ||
aria-label='First Page' | ||
size='sm' | ||
variant='pgNotSelected' | ||
onClick={() => setInternalPage(1)} | ||
isDisabled={internalPage <= 1} | ||
> | ||
<< | ||
</Button> | ||
<Button | ||
aria-label='Previous Page' | ||
size='sm' | ||
variant='pgNotSelected' | ||
isDisabled={internalPage <= 1} | ||
onClick={() => setInternalPage(internalPage - 1)} | ||
> | ||
< | ||
</Button> | ||
<Button | ||
aria-label='Next Page' | ||
size='sm' | ||
variant='pgNotSelected' | ||
isDisabled={internalPage >= pageAmount} | ||
onClick={() => setInternalPage(internalPage + 1)} | ||
> | ||
> | ||
</Button> | ||
<Button | ||
aria-label='Last Page' | ||
size='sm' | ||
variant='pgNotSelected' | ||
isDisabled={internalPage >= pageAmount} | ||
onClick={() => setInternalPage(pageAmount)} | ||
> | ||
>> | ||
</Button> | ||
</HStack> | ||
<Divider display={{ base: "none", md: "initial" }} orientation='vertical' h='30px' /> | ||
<HStack flexGrow='1'> | ||
</HStack> | ||
</Stack> | ||
</Box> | ||
); | ||
}; | ||
|
||
export { CursorPaginator }; |
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,85 @@ | ||
import { | ||
Box, | ||
HStack, | ||
Button, | ||
Divider, | ||
Stack, | ||
BoxProps, | ||
} from "@chakra-ui/react"; | ||
import React from "react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
type PageChangeCallback = (page: number) => void; | ||
type ItemChangeCallback = (items: number) => void; | ||
|
||
export interface PaginationProps extends BoxProps { | ||
/** Total number of items to paginate */ | ||
total: number; | ||
/** Array with all available "items per page" amounts */ | ||
possibleItemsPerPage?: Array<number>; | ||
/** External bind for current page */ | ||
page?: number; | ||
/** Number of items to display per page */ | ||
limit?: number; | ||
/** Callback for page change events */ | ||
onPageChange?: PageChangeCallback; | ||
/** Callback for item count change event */ | ||
onItemCountChange?: ItemChangeCallback; | ||
} | ||
|
||
const LoadMorePaginator = ({ | ||
total, | ||
possibleItemsPerPage = [5, 10, 15, 20, 30, 50, 100], | ||
limit = 5, | ||
page, | ||
onPageChange, | ||
onItemCountChange, | ||
...props | ||
}: PaginationProps) => { | ||
const [internalPage, setInternalPage] = useState(page || 1); | ||
// Use limit set in instance, unless it does not exist in the list of possible items per page. | ||
// Default to middle. | ||
const [itemsPerPage] = useState( | ||
possibleItemsPerPage.includes(limit) | ||
? limit | ||
: possibleItemsPerPage[Math.floor(possibleItemsPerPage.length / 2)], | ||
); | ||
const [pageAmount, setPageAmount] = useState(1); | ||
const [totalPages, setTotalPages] = useState(1); | ||
const [page2, setPage] = useState(1); | ||
const [loading, setLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
if (page) { | ||
setInternalPage(page); | ||
} | ||
}, [page]); | ||
|
||
useEffect(() => { | ||
if (onPageChange !== undefined) { | ||
onPageChange(internalPage); | ||
} | ||
}, [internalPage, onPageChange]); | ||
|
||
useEffect(() => { | ||
if (onItemCountChange !== undefined) { | ||
onItemCountChange(itemsPerPage); | ||
} | ||
}, [itemsPerPage, onItemCountChange]); | ||
|
||
|
||
useEffect(() => { | ||
const newPageAmount = Math.ceil(total / itemsPerPage); | ||
setInternalPage((prevPage) => (prevPage > newPageAmount ? 1 : prevPage)); | ||
setPageAmount(newPageAmount); | ||
}, [total, itemsPerPage, setInternalPage]); | ||
|
||
return ( | ||
<div className="LoadMorePaginator"> | ||
<div className="clearfix"></div> | ||
{totalPages !== page && <button className="btn-load-more" onClick={() => setPage(page + 1)}>{loading ? 'Loading...' : 'Load More'}</button>} | ||
</div> | ||
); | ||
}; | ||
|
||
export { LoadMorePaginator }; |
Oops, something went wrong.