import { Flex, Icon, Text } from '@chakra-ui/core'; import { usePagination, useSortBy, useTable } from 'react-table'; import { useMedia } from '~/context'; import { CardBody, CardFooter, CardHeader, If } from '~/components'; import { TableMain } from './TableMain'; import { TableCell } from './TableCell'; import { TableHead } from './TableHead'; import { TableRow } from './TableRow'; import { TableBody } from './TableBody'; import { TableIconButton } from './TableIconButton'; import { TableSelectShow } from './TableSelectShow'; import type { ITable } from './types'; export const Table = (props: ITable) => { const { columns, data, heading, onRowClick, striped = false, bordersVertical = false, bordersHorizontal = false, cellRender, rowHighlightProp, rowHighlightBg, rowHighlightColor, } = props; const { isSm, isMd } = useMedia(); const defaultColumn = { minWidth: 100, width: 150, maxWidth: 300, }; let hiddenColumns = [] as string[]; for (const col of columns) { if (col.hidden) { hiddenColumns.push(col.accessor); } } const table = useTable( { columns, defaultColumn, data, initialState: { hiddenColumns }, }, useSortBy, usePagination, ); const { getTableProps, headerGroups, prepareRow, page, canPreviousPage, canNextPage, pageOptions, pageCount, gotoPage, nextPage, previousPage, setPageSize, state: { pageIndex, pageSize }, } = table; return ( {heading && {heading}} {headerGroups.map((headerGroup, i) => ( {headerGroup.headers.map(column => ( {column.render('Header')} {''} ))} ))} {page.map( (row, key) => prepareRow(row) || ( onRowClick && onRowClick(row)} key={key} highlight={row.values[rowHighlightProp ?? ''] ?? false} highlightBg={rowHighlightBg} highlightColor={rowHighlightColor} {...row.getRowProps()}> {row.cells.map((cell, i) => { return ( {cell.render(cellRender ?? 'Cell')} ); })} ), )} gotoPage(0)} isDisabled={!canPreviousPage} icon={() => } /> previousPage()} isDisabled={!canPreviousPage} icon={() => } /> Page{' '} {pageIndex + 1} of {pageOptions.length} {' '} {!(isSm || isMd) && ( { setPageSize(Number(e.target.value)); }} /> )} nextPage()} icon={() => } /> gotoPage(pageCount ? pageCount - 1 : 1)} isDisabled={!canNextPage} icon={() => } /> ); };