From 7c11dafd4c41a71c38df78d6286e27c8ffa36bc3 Mon Sep 17 00:00:00 2001 From: Mason Hu Date: Wed, 10 Jan 2024 13:36:20 +0200 Subject: [PATCH] feat: generalise Pagination component for tables Signed-off-by: Mason Hu --- src/components/ConfigurationTable.tsx | 6 +- src/components/Pagination.tsx | 147 ------------------ src/components/ScrollableTable.tsx | 4 +- src/components/SelectableMainTable.tsx | 14 +- .../forms/ScrollableConfigurationTable.tsx | 7 +- src/pages/cluster/ClusterList.tsx | 43 +++-- src/pages/images/ImageList.tsx | 43 +++-- src/pages/images/ImageSelector.tsx | 6 +- src/pages/instances/InstanceList.tsx | 113 +++++++------- src/pages/instances/InstanceSnapshots.tsx | 83 +++++----- src/pages/profiles/ProfileList.tsx | 51 +++--- src/pages/settings/Settings.tsx | 6 +- src/pages/storage/CustomIsoList.tsx | 43 +++-- src/pages/storage/CustomVolumeSelectModal.tsx | 2 + src/pages/storage/StoragePools.tsx | 3 +- src/pages/storage/StorageVolumeSnapshots.tsx | 87 ++++++----- src/pages/storage/StorageVolumes.tsx | 45 +++--- src/sass/_pagination.scss | 37 ----- src/sass/styles.scss | 1 - src/util/pagination.tsx | 80 ---------- src/util/useSortTableData.tsx | 45 ++++++ 21 files changed, 330 insertions(+), 536 deletions(-) delete mode 100644 src/components/Pagination.tsx delete mode 100644 src/sass/_pagination.scss delete mode 100644 src/util/pagination.tsx create mode 100644 src/util/useSortTableData.tsx diff --git a/src/components/ConfigurationTable.tsx b/src/components/ConfigurationTable.tsx index a1216e2821..3a7b53e63e 100644 --- a/src/components/ConfigurationTable.tsx +++ b/src/components/ConfigurationTable.tsx @@ -1,5 +1,5 @@ import React, { FC, ReactNode } from "react"; -import { MainTable } from "@canonical/react-components"; +import { MainTable, MainTableProps } from "@canonical/react-components"; import { MainTableRow } from "@canonical/react-components/dist/components/MainTable/MainTable"; interface Props { @@ -8,10 +8,11 @@ interface Props { emptyStateMsg?: string; } -const ConfigurationTable: FC = ({ +const ConfigurationTable: FC = ({ rows, configurationExtra, emptyStateMsg, + ...props }) => { const headers = [ { @@ -28,6 +29,7 @@ const ConfigurationTable: FC = ({ emptyStateMsg={emptyStateMsg} headers={headers} rows={rows} + {...props} /> ); }; diff --git a/src/components/Pagination.tsx b/src/components/Pagination.tsx deleted file mode 100644 index 40c25990bc..0000000000 --- a/src/components/Pagination.tsx +++ /dev/null @@ -1,147 +0,0 @@ -import { Button, Icon, Input, Select } from "@canonical/react-components"; -import React, { - FC, - HTMLAttributes, - ReactNode, - useEffect, - useState, -} from "react"; -import { paginationOptions } from "util/pagination"; -import useEventListener from "@use-it/event-listener"; -import { MainTableRow } from "@canonical/react-components/dist/components/MainTable/MainTable"; -import classnames from "classnames"; - -const figureSmallScreen = () => { - const descriptionElement = document.getElementById("pagination-description"); - if (!descriptionElement) { - return true; - } - return descriptionElement.getBoundingClientRect().width < 230; -}; - -type Props = { - className?: string; - pageSize: number; - setPageSize: (val: number) => void; - currentPage: number; - paginate: (val: number) => void; - visibleCount: number; - totalCount: number; - totalPages: number; - keyword: string; - pageData?: MainTableRow[]; - itemsPerPage?: number; - totalItems?: number; - updateSort?: (sort?: string | null) => void; - selectedNotification?: ReactNode; -} & HTMLAttributes; - -const Pagination: FC = ({ - className, - pageSize, - setPageSize, - currentPage, - paginate, - visibleCount, - totalCount, - totalPages, - keyword, - pageData: _pageData, - itemsPerPage: _itemsPerPage, - totalItems: _totalItems, - updateSort: _updateSort, - selectedNotification, - ...divProps -}) => { - const [isSmallScreen, setSmallScreen] = useState(figureSmallScreen()); - - const resize = () => { - setSmallScreen(figureSmallScreen()); - }; - useEventListener("resize", resize); - useEffect(resize, []); - - const getVisibleCount = () => { - if (selectedNotification) { - return selectedNotification; - } - - if (isSmallScreen) { - return `${visibleCount} out of ${totalCount}`; - } - - if (visibleCount === totalCount && visibleCount > 1) { - return `Showing all ${totalCount} ${keyword}s`; - } - - return `Showing ${visibleCount} out of ${totalCount} ${keyword}${ - totalCount !== 1 ? "s" : "" - }`; - }; - - return ( -
-
- {getVisibleCount()} -
- - { - const newPage = Math.min( - totalPages, - Math.max(1, parseInt(e.target.value)), - ); - - paginate(newPage); - }} - value={currentPage} - type="number" - />{" "} - of {totalPages} - -