// This rule isn't needed because react-table does this for us, for better or worse. /* eslint react/jsx-key: 0 */ import { Flex, Text } from '@chakra-ui/react'; import { usePagination, useSortBy, useTable } from 'react-table'; import { If, Then, Else } from 'react-if'; import { CardBody, CardFooter, CardHeader, DynamicIcon } from '~/elements'; import { useMobile } from '~/hooks'; import { TableMain } from './table'; import { TableCell } from './cell'; import { TableHead } from './head'; import { TableRow } from './row'; import { TableBody } from './body'; import { TableIconButton } from './button'; import { PageSelect } from './page-select'; import type { TableOptions, PluginHook } from 'react-table'; import type { Theme, TableColumn, CellRenderProps } from '~/types'; interface TableProps { data: Route[]; striped?: boolean; columns: TableColumn[]; heading?: React.ReactNode; bordersVertical?: boolean; bordersHorizontal?: boolean; Cell?: React.FC; rowHighlightProp?: keyof Route; rowHighlightBg?: Theme.ColorNames; } export const Table = (props: TableProps): JSX.Element => { const { data, columns, heading, Cell, rowHighlightBg, striped = false, rowHighlightProp, bordersVertical = false, bordersHorizontal = false, } = props; const isMobile = useMobile(); const defaultColumn = { minWidth: 100, width: 150, maxWidth: 300, }; const hiddenColumns = [] as string[]; for (const col of columns) { if (col.hidden) { hiddenColumns.push(col.accessor); } } const options = { columns, defaultColumn, data, initialState: { hiddenColumns }, } as TableOptions; const plugins = [useSortBy, usePagination] as PluginHook[]; const instance = useTable(options, ...plugins); const { page, gotoPage, nextPage, pageCount, prepareRow, canNextPage, pageOptions, setPageSize, headerGroups, previousPage, getTableProps, canPreviousPage, state: { pageIndex, pageSize }, } = instance; return ( {heading && {heading}} {headerGroups.map((headerGroup, i) => ( {headerGroup.headers.map(column => ( {column.render('Header')} {''} ))} ))} {page.map((row, key) => { prepareRow(row); return ( {row.cells.map((cell, i) => { const { column, row, value } = cell as CellRenderProps; return ( {typeof Cell !== 'undefined' ? ( ) : ( cell.render('Cell') )} ); })} ); })} gotoPage(0)} isDisabled={!canPreviousPage} icon={} /> previousPage()} isDisabled={!canPreviousPage} icon={} /> Page{' '} {pageIndex + 1} of {pageOptions.length} {' '} {!isMobile && ( { setPageSize(Number(e.target.value)); }} /> )} } /> } onClick={() => gotoPage(pageCount ? pageCount - 1 : 1)} /> ); };