-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into deposit-history
- Loading branch information
Showing
32 changed files
with
484 additions
and
148 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,43 @@ | ||
import React from "react" | ||
import { PaginationContext } from "#/contexts" | ||
import { StackProps, VStack } from "@chakra-ui/react" | ||
|
||
export type PaginationProps<T> = Omit<StackProps, "as"> & { | ||
data: T[] | ||
pageSize?: number | ||
defaultPage?: number | ||
} | ||
|
||
function Pagination<T>(props: PaginationProps<T>) { | ||
const { data, children, pageSize = 10, defaultPage = 0, ...restProps } = props | ||
|
||
const [page, setPage] = React.useState(defaultPage) | ||
|
||
const pageData = React.useMemo(() => { | ||
const startIndex = page * pageSize | ||
const endIndex = startIndex + pageSize | ||
|
||
return data.slice(startIndex, endIndex) | ||
}, [data, page, pageSize]) | ||
|
||
const contextValue = React.useMemo( | ||
() => ({ | ||
pageSize, | ||
page, | ||
setPage, | ||
totalSize: data.length, | ||
pageData, | ||
}), | ||
[pageSize, page, data, pageData], | ||
) | ||
|
||
return ( | ||
<PaginationContext.Provider value={contextValue}> | ||
<VStack spacing={6} align="stretch" w="full" {...restProps}> | ||
{children} | ||
</VStack> | ||
</PaginationContext.Provider> | ||
) | ||
} | ||
|
||
export default Pagination |
43 changes: 43 additions & 0 deletions
43
dapp/src/components/shared/Pagination/PaginationButton.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,43 @@ | ||
import React from "react" | ||
import { Icon, IconButton, IconButtonProps } from "@chakra-ui/react" | ||
import { IconArrowLeft, IconArrowRight } from "@tabler/icons-react" | ||
import { usePagination } from "#/hooks" | ||
|
||
type PaginationButtonProps = Omit<IconButtonProps, "aria-label" | "icon"> & { | ||
mode: "previous" | "next" | ||
} | ||
|
||
function PaginationButton(props: PaginationButtonProps) { | ||
const { mode, ...restProps } = props | ||
const { page, setPage, totalSize, pageSize } = usePagination() | ||
|
||
const handleClick = () => setPage(mode === "next" ? page + 1 : page - 1) | ||
const isDisabled = React.useMemo( | ||
() => | ||
mode === "next" ? page * pageSize + pageSize >= totalSize : page === 0, | ||
[mode, page, pageSize, totalSize], | ||
) | ||
|
||
return ( | ||
<IconButton | ||
variant="pagination" | ||
rounded="full" | ||
p={1.5} | ||
boxSize={7} | ||
minW="unset" | ||
icon={ | ||
<Icon | ||
as={mode === "next" ? IconArrowRight : IconArrowLeft} | ||
strokeWidth={2} | ||
boxSize={4} | ||
/> | ||
} | ||
aria-label={mode === "next" ? "Next" : "Previous"} | ||
onClick={handleClick} | ||
isDisabled={isDisabled} | ||
{...restProps} | ||
/> | ||
) | ||
} | ||
|
||
export default PaginationButton |
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,75 @@ | ||
import React from "react" | ||
import { Box, Stack, StackProps, useToken } from "@chakra-ui/react" | ||
import { useSize } from "@chakra-ui/react-use-size" | ||
import { AnimatePresence, Transition, Variants, motion } from "framer-motion" | ||
import { usePagination } from "#/hooks" | ||
|
||
const transition: Transition = { | ||
type: "spring", | ||
stiffness: 120, | ||
damping: 16, | ||
mass: 0.85, | ||
} | ||
|
||
const variants: Variants = { | ||
enter: ({ direction, spacing }) => ({ | ||
x: `calc((100% + ${spacing}) * ${direction === "right" ? 1 : -1})`, | ||
transition, | ||
}), | ||
center: { | ||
x: 0, | ||
transition, | ||
}, | ||
exit: ({ direction, spacing }) => ({ | ||
x: `calc((100% + ${spacing}) * ${direction === "left" ? 1 : -1})`, | ||
transition, | ||
}), | ||
} | ||
|
||
type PaginationPageProps<T> = Omit<StackProps, "children"> & { | ||
children: (pageData: T[]) => React.ReactNode | ||
pageSpacing?: number | string | ||
} | ||
|
||
function PaginationPage<T>(props: PaginationPageProps<T>) { | ||
const { children, pageSpacing = 0, ...restProps } = props | ||
const { page, pageData } = usePagination() | ||
|
||
const ref = React.useRef<HTMLDivElement>(null) | ||
const { height } = useSize(ref) ?? { height: 0 } | ||
|
||
const previousPage = React.useRef<number>(0) | ||
React.useEffect(() => { | ||
previousPage.current = page | ||
}, [page]) | ||
|
||
const direction = page < previousPage.current ? "left" : "right" | ||
const spacing = useToken("space", pageSpacing, "20%") | ||
|
||
return ( | ||
<Box as={motion.div} layout animate={{ height, transition }}> | ||
<Box ref={ref}> | ||
<AnimatePresence | ||
mode="popLayout" | ||
custom={{ direction, spacing }} | ||
initial={false} | ||
> | ||
<Stack | ||
as={motion.div} | ||
custom={{ direction, spacing }} | ||
key={page} | ||
variants={variants} | ||
animate="center" | ||
initial="enter" | ||
exit="exit" | ||
{...restProps} | ||
> | ||
{children(pageData as T[])} | ||
</Stack> | ||
</AnimatePresence> | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
export default PaginationPage |
Oops, something went wrong.