-
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 bitcoin-native-experience
- Loading branch information
Showing
27 changed files
with
447 additions
and
75 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
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 |
28 changes: 28 additions & 0 deletions
28
dapp/src/components/shared/Pagination/PaginationStatus.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,28 @@ | ||
import React from "react" | ||
import { TextProps } from "@chakra-ui/react" | ||
import { getPaginationState } from "#/components/TransactionHistory/Table/utils" | ||
import { usePagination } from "#/hooks" | ||
import { TextSm } from "../Typography" | ||
|
||
type PaginationStatusProps = TextProps & { | ||
dataLabel?: string | ||
} | ||
|
||
function PaginationStatus(props: PaginationStatusProps) { | ||
const { dataLabel = "items", ...restProps } = props | ||
const { page, pageSize, totalSize } = usePagination() | ||
|
||
const { rowMin: rangeStart, rowMax: rangeEnd } = getPaginationState( | ||
page, | ||
pageSize, | ||
totalSize, | ||
) | ||
|
||
return ( | ||
<TextSm {...restProps}> | ||
Showing {rangeStart}-{rangeEnd} out of {totalSize} {dataLabel} | ||
</TextSm> | ||
) | ||
} | ||
|
||
export default PaginationStatus |
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,4 @@ | ||
export { default as Pagination } from "./Pagination" | ||
export { default as PaginationStatus } from "./PaginationStatus" | ||
export { default as PaginationPage } from "./PaginationPage" | ||
export { default as PaginationButton } from "./PaginationButton" |
Oops, something went wrong.