From 0e2d2b7551946a3218671978a0d957740134f761 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 3 Jan 2022 17:56:09 +0100 Subject: [PATCH] feat(nui): Table pagination --- ui/src/components/App.tsx | 2 +- ui/src/components/MainContent.tsx | 2 +- ui/src/components/NavBars/LeftBar.tsx | 2 +- ui/src/components/Resource.tsx | 157 ++++++++++++++++++-------- 4 files changed, 113 insertions(+), 50 deletions(-) diff --git a/ui/src/components/App.tsx b/ui/src/components/App.tsx index d26eb1b..19ea523 100644 --- a/ui/src/components/App.tsx +++ b/ui/src/components/App.tsx @@ -27,7 +27,7 @@ const App: React.FC = () => { { return ( <>
- + {children}
diff --git a/ui/src/components/NavBars/LeftBar.tsx b/ui/src/components/NavBars/LeftBar.tsx index 596fed9..79f0f29 100644 --- a/ui/src/components/NavBars/LeftBar.tsx +++ b/ui/src/components/NavBars/LeftBar.tsx @@ -24,7 +24,7 @@ const LeftBar: React.FC = () => { }); return ( - + {initData.resources.map((resource, index) => ( diff --git a/ui/src/components/Resource.tsx b/ui/src/components/Resource.tsx index 8402b29..3a23877 100644 --- a/ui/src/components/Resource.tsx +++ b/ui/src/components/Resource.tsx @@ -3,9 +3,16 @@ import { useEffect, useState, useMemo } from 'react'; import { fetchNui } from '../utils/fetchNui'; import { useNuiEvent } from '../hooks/useNuiEvent'; import { debugData } from '../utils/debugData'; -import { Table, Thead, Tbody, Tr, Th, Td, chakra } from '@chakra-ui/react'; -import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons'; -import { useTable, useSortBy, Column } from 'react-table'; +import { Table, Thead, Tbody, Tr, Th, Td, chakra, Flex, IconButton, Text } from '@chakra-ui/react'; +import { + TriangleDownIcon, + TriangleUpIcon, + ArrowLeftIcon, + ChevronLeftIcon, + ArrowRightIcon, + ChevronRightIcon, +} from '@chakra-ui/icons'; +import { useTable, useSortBy, Column, usePagination } from 'react-table'; interface QueryData { date: number; @@ -63,58 +70,114 @@ const Resource: React.FC = () => { [] ); - const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data }, useSortBy); + const { + getTableProps, + getTableBodyProps, + headerGroups, + page, + canPreviousPage, + canNextPage, + pageOptions, + pageCount, + gotoPage, + nextPage, + previousPage, + state: { pageIndex }, + prepareRow, + } = useTable({ columns, data, initialState: { pageSize: 12 } }, useSortBy, usePagination); // Todo: pagination return ( - - - {headerGroups.map((header): any => ( - - {header.headers.map((column) => ( - - ))} - - ))} - - - {rows.map((row) => { - prepareRow(row); - return ( - - {row.cells.map((cell) => ( - + {page.map((row) => { + prepareRow(row); + return ( + + {row.cells.map((cell) => ( + + ))} + + ); + })} + +
- {column.render('Header')} - - {column.isSorted ? ( - column.isSortedDesc ? ( - - ) : ( - - ) - ) : null} - -
+ + + {headerGroups.map((header): any => ( + + {header.headers.map((column) => ( + ))} - ); - })} - -
- {cell.render('Cell')} - + {column.render('Header')} + + {column.isSorted ? ( + column.isSortedDesc ? ( + + ) : ( + + ) + ) : null} + +
+ ))} + +
+ {cell.render('Cell')} +
+ + + + } + aria-label="Last page" + onClick={() => gotoPage(0)} + size="sm" + isDisabled={!canPreviousPage} + /> + } + size="sm" + ml={4} + /> + + + Page {pageIndex + 1} of {pageOptions.length} + + + } + /> + gotoPage(pageCount - 1)} + isDisabled={!canNextPage} + icon={} + size="sm" + ml={4} + /> + + + ); };