import * as React from 'react'; import { ColumnDef, flexRender, getCoreRowModel, useReactTable, } from '@tanstack/react-table'; import TableHeader from './table-header/TableHeader'; import { IconProp } from '@fortawesome/fontawesome-svg-core'; import styled from '@emotion/styled'; import { SortType } from './table-header/SortAndFilterBar'; import { faCalendar } from '@fortawesome/pro-regular-svg-icons'; type OwnProps = { data: Array; columns: Array>; viewName: string; viewIcon?: IconProp; onSortsUpdate?: (sorts: Array) => void; }; const StyledTable = styled.table` min-width: calc(100% - ${(props) => props.theme.spacing(4)}); border-radius: 4px; border-spacing: 0; border-collapse: collapse; margin-left: ${(props) => props.theme.spacing(2)}; margin-right: ${(props) => props.theme.spacing(2)}; th { border-collapse: collapse; color: ${(props) => props.theme.text40}; padding: 0; border: 1px solid ${(props) => props.theme.tertiaryBackground}; text-align: left; :last-child { border-right-color: transparent; } :first-of-type { border-left-color: transparent; } } td { border-collapse: collapse; color: ${(props) => props.theme.text80}; padding: 0; border: 1px solid ${(props) => props.theme.tertiaryBackground}; text-align: left; :last-child { border-right-color: transparent; } :first-of-type { border-left-color: transparent; } } `; const StyledTableWithHeader = styled.div` display: flex; flex-direction: column; flex: 1; `; function Table({ data, columns, viewName, viewIcon, onSortsUpdate, }: OwnProps) { const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), }); const sortsAvailable: Array = [ { id: 'created_at', label: 'Created at', order: 'asc', icon: faCalendar, }, ]; return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ))} ))} {table.getRowModel().rows.map((row, index) => ( {row.getVisibleCells().map((cell) => { return ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ); })} ))} {table .getFooterGroups() .flatMap((group) => group.headers) .filter((header) => !!header.column.columnDef.footer).length > 0 && ( {table.getFooterGroups().map((footerGroup) => ( {footerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.footer, header.getContext(), )} ))} ))} )} ); } export default Table;