feat(nui): Table pagination

This commit is contained in:
Luke committed 2022-01-03 17:56:09 +01:00
1 parent 1ecb93801a
commit 0e2d2b7551
4 files changed
+71 -8

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@ const App: React.FC = () => {
<MotionBox <MotionBox
animate={{ y: [100, 0] }} animate={{ y: [100, 0] }}
width="60%" width="60%"
height="60%" height="50%"
backgroundColor="#191E26" backgroundColor="#191E26"
borderRadius="1vh" borderRadius="1vh"
color="white" color="white"
+1 -1
View File
@@ -5,7 +5,7 @@ const MainContent: React.FC = ({ children }) => {
return ( return (
<> <>
<Center h="95%" alignItems="stretch"> <Center h="95%" alignItems="stretch">
<Box h="100%" w="50%" backgroundColor="#1B2129" flex="1"> <Box h="100%" w="50%" backgroundColor="#1B2129" flex="1" overflowY="scroll">
{children} {children}
</Box> </Box>
</Center> </Center>
+1 -1
View File
@@ -24,7 +24,7 @@ const LeftBar: React.FC = () => {
}); });
return ( return (
<Box p="1.2vh" fontSize="1.5vh" float="left" w="12%" height="55vh" overflowY="scroll"> <Box p="1.2vh" fontSize="1.5vh" float="left" w="13%" height="55vh" overflowY="scroll">
<VStack align="left"> <VStack align="left">
{initData.resources.map((resource, index) => ( {initData.resources.map((resource, index) => (
<Link to={resource} key={`${resource}-${index}`}> <Link to={resource} key={`${resource}-${index}`}>
+68 -5
View File
@@ -3,9 +3,16 @@ import { useEffect, useState, useMemo } from 'react';
import { fetchNui } from '../utils/fetchNui'; import { fetchNui } from '../utils/fetchNui';
import { useNuiEvent } from '../hooks/useNuiEvent'; import { useNuiEvent } from '../hooks/useNuiEvent';
import { debugData } from '../utils/debugData'; import { debugData } from '../utils/debugData';
import { Table, Thead, Tbody, Tr, Th, Td, chakra } from '@chakra-ui/react'; import { Table, Thead, Tbody, Tr, Th, Td, chakra, Flex, IconButton, Text } from '@chakra-ui/react';
import { TriangleDownIcon, TriangleUpIcon } from '@chakra-ui/icons'; import {
import { useTable, useSortBy, Column } from 'react-table'; TriangleDownIcon,
TriangleUpIcon,
ArrowLeftIcon,
ChevronLeftIcon,
ArrowRightIcon,
ChevronRightIcon,
} from '@chakra-ui/icons';
import { useTable, useSortBy, Column, usePagination } from 'react-table';
interface QueryData { interface QueryData {
date: number; date: number;
@@ -63,10 +70,25 @@ 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 // Todo: pagination
return ( return (
<>
<Table {...getTableProps} size="sm"> <Table {...getTableProps} size="sm">
<Thead> <Thead>
{headerGroups.map((header): any => ( {headerGroups.map((header): any => (
@@ -94,7 +116,7 @@ const Resource: React.FC = () => {
))} ))}
</Thead> </Thead>
<Tbody {...getTableBodyProps()}> <Tbody {...getTableBodyProps()}>
{rows.map((row) => { {page.map((row) => {
prepareRow(row); prepareRow(row);
return ( return (
<Tr {...row.getRowProps()}> <Tr {...row.getRowProps()}>
@@ -115,6 +137,47 @@ const Resource: React.FC = () => {
})} })}
</Tbody> </Tbody>
</Table> </Table>
<Flex mt={3} justifyContent="center " alignItems="center" p="1.2vh">
<Flex>
<IconButton
icon={<ArrowLeftIcon h={3} w={3} color="black" />}
aria-label="Last page"
onClick={() => gotoPage(0)}
size="sm"
isDisabled={!canPreviousPage}
/>
<IconButton
aria-label="Previous page"
onClick={previousPage}
isDisabled={!canPreviousPage}
icon={<ChevronLeftIcon h={6} w={6} color="black" />}
size="sm"
ml={4}
/>
</Flex>
<Text align="center" pl={4} pr={4}>
Page {pageIndex + 1} of {pageOptions.length}
</Text>
<Flex>
<IconButton
aria-label="Next page"
onClick={nextPage}
size="sm"
isDisabled={!canNextPage}
icon={<ChevronRightIcon h={6} w={6} color="black" />}
/>
<IconButton
aria-label="Last page"
onClick={() => gotoPage(pageCount - 1)}
isDisabled={!canNextPage}
icon={<ArrowRightIcon h={3} w={3} color="black" />}
size="sm"
ml={4}
/>
</Flex>
</Flex>
</>
); );
}; };