diff --git a/packages/chakra-components/src/components/Pagination/EllipsisButton.tsx b/packages/chakra-components/src/components/Pagination/EllipsisButton.tsx new file mode 100644 index 00000000..56aa8914 --- /dev/null +++ b/packages/chakra-components/src/components/Pagination/EllipsisButton.tsx @@ -0,0 +1,48 @@ +import { useState } from 'react' +import { useStyleConfig } from '@chakra-ui/system' +import { Button, ButtonProps, Input, InputProps } from '@chakra-ui/react' + +type EllipsisButtonProps = ButtonProps & { + gotoPage: (page: number) => void + inputProps?: InputProps +} + +export const EllipsisButton = ({ gotoPage, inputProps, ...rest }: EllipsisButtonProps) => { + const [ellipsisInput, setEllipsisInput] = useState(false) + const styles = useStyleConfig('EllipsisButton', rest) + + if (ellipsisInput) { + return ( + { + if (e.target instanceof HTMLInputElement && e.key === 'Enter') { + const pageNumber = Number(e.target.value) + gotoPage(pageNumber) + setEllipsisInput(false) + } + }} + onBlur={() => setEllipsisInput(false)} + autoFocus + /> + ) + } + + return ( + + ) +} diff --git a/packages/chakra-components/src/components/Pagination.tsx b/packages/chakra-components/src/components/Pagination/Pagination.tsx similarity index 72% rename from packages/chakra-components/src/components/Pagination.tsx rename to packages/chakra-components/src/components/Pagination/Pagination.tsx index 21cc7b2e..15bde823 100644 --- a/packages/chakra-components/src/components/Pagination.tsx +++ b/packages/chakra-components/src/components/Pagination/Pagination.tsx @@ -1,9 +1,11 @@ -import { Button, ButtonGroup, ButtonGroupProps, ButtonProps, Input, InputProps, Text } from '@chakra-ui/react' -import { ReactElement, useMemo, useState } from 'react' +import { ButtonGroup, ButtonGroupProps, ButtonProps, InputProps, Text } from '@chakra-ui/react' +import { ReactElement, useMemo } from 'react' import { generatePath, Link as RouterLink, useLocation, useNavigate, useParams } from 'react-router-dom' -import { usePagination, useRoutedPagination } from '@vocdoni/react-providers' +import { useLocalize, usePagination, useRoutedPagination } from '@vocdoni/react-providers' import { PaginationResponse } from '@vocdoni/sdk' -import { Trans } from 'react-i18next' +import { useMultiStyleConfig, chakra } from '@chakra-ui/system' +import { EllipsisButton } from './EllipsisButton' +import { PaginatorButton } from './PaginatorButton' export type PaginationProps = ButtonGroupProps & { maxButtons?: number | false @@ -16,57 +18,19 @@ type PaginatorButtonProps = { currentPage: number } & ButtonProps -const PageButton = ({ page, currentPage, ...rest }: PaginatorButtonProps) => ( - -) - -const RoutedPageButton = ({ page, currentPage, to, ...rest }: PaginatorButtonProps & { to: string }) => ( - -) - -type EllipsisButtonProps = ButtonProps & { - gotoPage: (page: number) => void - inputProps?: InputProps +const PageButton = ({ page, currentPage, ...rest }: PaginatorButtonProps) => { + return ( + + {page + 1} + + ) } -const EllipsisButton = ({ gotoPage, inputProps, ...rest }: EllipsisButtonProps) => { - const [ellipsisInput, setEllipsisInput] = useState(false) - - if (ellipsisInput) { - return ( - { - if (e.target instanceof HTMLInputElement && e.key === 'Enter') { - const pageNumber = Number(e.target.value) - gotoPage(pageNumber) - setEllipsisInput(false) - } - }} - onBlur={() => setEllipsisInput(false)} - autoFocus - /> - ) - } - +const RoutedPageButton = ({ page, currentPage, to, ...rest }: PaginatorButtonProps & { to: string }) => { return ( - + + {page + 1} + ) } @@ -139,6 +103,9 @@ const PaginationButtons = ({ goToPage: GotoPageType } & ButtonGroupProps & Pick) => { + const styles = useMultiStyleConfig('Pagination') + const t = useLocalize() + const pages = usePaginationPages( currentPage, totalPages, @@ -152,34 +119,34 @@ const PaginationButtons = ({ ) return ( - <> - + + {totalPages === undefined ? ( <> - - + ) : ( pages )} {totalItems && ( - - - Showing a total of {{ count: totalItems }} results - + + {t('pagination.total_results', { + count: totalItems, + })} )} - + ) } diff --git a/packages/chakra-components/src/components/Pagination/PaginatorButton.tsx b/packages/chakra-components/src/components/Pagination/PaginatorButton.tsx new file mode 100644 index 00000000..17412fc9 --- /dev/null +++ b/packages/chakra-components/src/components/Pagination/PaginatorButton.tsx @@ -0,0 +1,7 @@ +import { useStyleConfig } from '@chakra-ui/system' +import { Button, ButtonProps, forwardRef } from '@chakra-ui/react' + +export const PaginatorButton = forwardRef((props, ref) => { + const styles = useStyleConfig('PageButton', props) + return