import dynamic from 'next/dynamic'; import { Flex, Icon, Text } from '@chakra-ui/react'; import { usePagination, useSortBy, useTable } from 'react-table'; import { useMobile } from '~/context'; import { CardBody, CardFooter, CardHeader, If } from '~/components'; 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 { TableSelectShow } from './pageSelect'; import type { TableOptions, PluginHook } from 'react-table'; import type { TCellRender } from '~/types'; import type { TTable } from './types'; const ChevronRight = dynamic(() => import('@meronex/icons/fa').then(i => i.FaChevronRight), ); const ChevronLeft = dynamic(() => import('@meronex/icons/fa').then(i => i.FaChevronLeft), ); const ChevronDown = dynamic(() => import('@meronex/icons/fa').then(i => i.FaChevronDown), ); const DoubleChevronRight = dynamic(() => import('@meronex/icons/fi').then(i => i.FiChevronsRight), ); const DoubleChevronLeft = dynamic(() => import('@meronex/icons/fi').then(i => i.FiChevronsLeft), ); export function Table(props: TTable) { 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, }; let 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 TCellRender; 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)} /> ); }