From fd8ea43f0b5bf980974b959957e77ade6f2a3f03 Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Mon, 28 Mar 2022 15:01:50 +0800 Subject: [PATCH] feat(console): pagination (#453) * feat(console): pagination * fix: large number * fix: new icon --- packages/console/package.json | 1 + .../console/src/components/Button/index.tsx | 13 +++--- .../src/components/DangerousRaw/index.tsx | 9 ++++ .../src/components/Pagination/Next.tsx | 14 ++++++ .../src/components/Pagination/Previous.tsx | 14 ++++++ .../components/Pagination/index.module.scss | 37 ++++++++++++++++ .../src/components/Pagination/index.tsx | 43 +++++++++++++++++++ pnpm-lock.yaml | 11 +++++ 8 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 packages/console/src/components/DangerousRaw/index.tsx create mode 100644 packages/console/src/components/Pagination/Next.tsx create mode 100644 packages/console/src/components/Pagination/Previous.tsx create mode 100644 packages/console/src/components/Pagination/index.module.scss create mode 100644 packages/console/src/components/Pagination/index.tsx diff --git a/packages/console/package.json b/packages/console/package.json index c8e1e53ed..56726e018 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -36,6 +36,7 @@ "react-i18next": "^11.15.4", "react-markdown": "^8.0.0", "react-modal": "^3.14.4", + "react-paginate": "^8.1.2", "react-router-dom": "^6.2.2", "remark-gfm": "^3.0.1", "swr": "^1.2.2" diff --git a/packages/console/src/components/Button/index.tsx b/packages/console/src/components/Button/index.tsx index 8809b1b06..ddb73855e 100644 --- a/packages/console/src/components/Button/index.tsx +++ b/packages/console/src/components/Button/index.tsx @@ -1,9 +1,10 @@ import { I18nKey } from '@logto/phrases'; import { conditionalString } from '@silverhand/essentials'; import classNames from 'classnames'; -import React, { HTMLProps, ReactNode } from 'react'; +import React, { HTMLProps, ReactElement, ReactNode } from 'react'; import { useTranslation } from 'react-i18next'; +import DangerousRaw from '../DangerousRaw'; import * as styles from './index.module.scss'; type BaseProps = Omit, 'type' | 'size' | 'title'> & { @@ -13,12 +14,12 @@ type BaseProps = Omit, 'type' | 'size' | 'title'> & }; type TitleButtonProps = BaseProps & { - title: I18nKey; + title: I18nKey | ReactElement; icon?: ReactNode; }; type IconButtonProps = BaseProps & { - title?: I18nKey; + title?: I18nKey | ReactElement; icon: ReactNode; }; @@ -30,6 +31,7 @@ const Button = ({ size = 'medium', title, icon, + className, ...rest }: Props) => { const { t } = useTranslation(); @@ -40,13 +42,14 @@ const Button = ({ styles.button, styles[type], styles[size], - conditionalString(icon && styles.withIcon) + conditionalString(icon && styles.withIcon), + className )} type={htmlType} {...rest} > {icon && {icon}} - {title && {t(title)}} + {title && (typeof title === 'string' ? {t(title)} : title)} ); }; diff --git a/packages/console/src/components/DangerousRaw/index.tsx b/packages/console/src/components/DangerousRaw/index.tsx new file mode 100644 index 000000000..b1a37ccfb --- /dev/null +++ b/packages/console/src/components/DangerousRaw/index.tsx @@ -0,0 +1,9 @@ +import React, { ReactNode } from 'react'; + +type Props = { + children: ReactNode; +}; + +const DangerousRaw = ({ children }: Props) => {children}; + +export default DangerousRaw; diff --git a/packages/console/src/components/Pagination/Next.tsx b/packages/console/src/components/Pagination/Next.tsx new file mode 100644 index 000000000..992625b63 --- /dev/null +++ b/packages/console/src/components/Pagination/Next.tsx @@ -0,0 +1,14 @@ +import React from 'react'; + +const Next = () => { + return ( + + + + ); +}; + +export default Next; diff --git a/packages/console/src/components/Pagination/Previous.tsx b/packages/console/src/components/Pagination/Previous.tsx new file mode 100644 index 000000000..75bfa7428 --- /dev/null +++ b/packages/console/src/components/Pagination/Previous.tsx @@ -0,0 +1,14 @@ +import React from 'react'; + +const Previous = () => { + return ( + + + + ); +}; + +export default Previous; diff --git a/packages/console/src/components/Pagination/index.module.scss b/packages/console/src/components/Pagination/index.module.scss new file mode 100644 index 000000000..bd399f4fe --- /dev/null +++ b/packages/console/src/components/Pagination/index.module.scss @@ -0,0 +1,37 @@ +@use '@/scss/underscore' as _; + +.pagination { + display: flex; + justify-content: right; + + li { + list-style: none; + + &:not(:first-child) { + margin-left: _.unit(2); + } + + .button { + border-radius: 6px; + min-width: 32px; + padding: 0 6px; + height: 32px; + text-overflow: unset; + + > span { + margin: 0 auto; + } + } + + &.disabled { + .button { + cursor: not-allowed; + background: var(--color-neutral-95); + + &:hover { + background: var(--color-neutral-95); + } + } + } + } +} diff --git a/packages/console/src/components/Pagination/index.tsx b/packages/console/src/components/Pagination/index.tsx new file mode 100644 index 000000000..7451696ff --- /dev/null +++ b/packages/console/src/components/Pagination/index.tsx @@ -0,0 +1,43 @@ +import React from 'react'; +import ReactPaginate from 'react-paginate'; + +import Button from '../Button'; +import DangerousRaw from '../DangerousRaw'; +import Next from './Next'; +import Previous from './Previous'; +import * as styles from './index.module.scss'; + +type Props = { + pageIndex: number; + pageCount: number; + onChange?: (pageIndex: number) => void; +}; + +const Pagination = ({ pageIndex, pageCount, onChange }: Props) => { + return ( + ( +