import * as React from "react"; import { useMemo } from "react"; import { Flex, Icon, Text } from "@chakra-ui/core"; import { usePagination, useSortBy, useTable } from "react-table"; import useMedia from "~/components/MediaProvider"; import { CardBody, CardFooter, CardHeader } from "~/components/Card"; 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"; const Table = ({ columns, data, tableHeading, initialPageSize = 10, onRowClick, striped = false, bordersVertical = false, bordersHorizontal = false, cellRender = null, rowHighlightProp, rowHighlightBg, rowHighlightColor }) => { const tableColumns = useMemo(() => columns, [columns]); const { isSm, isMd } = useMedia(); const defaultColumn = useMemo( () => ({ minWidth: 100, width: 150, maxWidth: 300 }), [] ); let hiddenColumns = []; tableColumns.map(col => { if (col.hidden === true) { hiddenColumns.push(col.accessor); } }); const { getTableProps, headerGroups, prepareRow, page, canPreviousPage, canNextPage, pageOptions, pageCount, gotoPage, nextPage, previousPage, setPageSize, state: { pageIndex, pageSize } } = useTable( { columns: tableColumns, defaultColumn, data, initialState: { pageIndex: 0, pageSize: initialPageSize, hiddenColumns: hiddenColumns } }, useSortBy, usePagination ); return ( {!!tableHeading && {tableHeading}} {headerGroups.map(headerGroup => ( {headerGroup.headers.map(column => ( {column.render("Header")} {column.isSorted ? ( column.isSortedDesc ? ( ) : ( ) ) : ( "" )} ))} ))} {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={() => } /> ); }; Table.displayName = "Table"; export default Table;