import React, { FC } from 'react'; import { Cell, Column, ColumnInstance, HeaderGroup, Row, useFlexLayout, useResizeColumns, useRowSelect, useSortBy, useTable } from 'react-table'; import { TableFooter } from './components/Footer/table-footer.component'; import { TableFooterRow } from './components/Footer/table-footer-row.component'; import { TableHead } from './components/Header/table-header.component'; import { TableHeaderRow } from './components/Header/table-header-row.component'; import { TableBody } from './components/table-body.component'; import { TableCell } from './components/table-cell.component'; import { TableRow } from './components/table-row.component'; import { tableClassName } from './utils'; import './table.components.scss'; const className = tableClassName; export interface DataType { [key: string]: any; } type TablePropsType = { columns: Column[]; data: DataType[]; className?: string; minWidth?: number; width?: number; maxWidth?: number; usePagination?: boolean; useSelection?: boolean; useSorting?: boolean; noHeader?: boolean; noFooter?: boolean; stickyHeader?: boolean; loadNext?: () => void; getTableHeight?: () => void; onHeaderClick?: (column: Column) => void; }; export const Table: FC = React.memo( ({ columns, data, // useSelection, // usePagination, // width, // minWidth, // maxWidth, useSorting, noHeader, noFooter, stickyHeader }) => { const getTableHooks = () => { const hooks = [ useFlexLayout, useResizeColumns, useRowSelect ]; if (useSorting) { return [ useSortBy, ...hooks ]; } return hooks; }; const { getTableProps, getTableBodyProps, headerGroups, footerGroups, rows, prepareRow } = useTable( { columns, data }, ...getTableHooks() // (hooks: Hooks) => { // if (useSelection) { // hooks.flatColumns.push((_columns: Column[]) => [ // { // id: 'selection', // minWidth: 48, // width: 0, // Header: ({ getToggleAllRowsSelectedProps }: { // getToggleAllRowsSelectedProps: Function; // }) => ( //
// getToggleAllRowsSelectedProps().onChange({ target: { value: val } }) } // checked={ getToggleAllRowsSelectedProps().checked } // /> //
// ), // // @ts-ignore // Cell: ({ row }): { [key: string]: any } => ( //
// row // .getToggleRowSelectedProps() // .onChange({ target: { value: val } }) } // checked={ row.getToggleRowSelectedProps().checked } // /> //
// ) // }, // ..._columns // ]); // } // } ); return (
{ !noHeader && ( { headerGroups.map((headerGroup: HeaderGroup) => ( { (headerGroup.headers as ColumnInstance[]).map((column) => ( { column.render('Header') } )) } )) } ) } { /* { usePagination && !!tableHeight && ( { RenderRow } ) } */ } { /* { !usePagination */ } { rows.map((row: Row) => { prepareRow(row); return ( { row.cells.map((cell: Cell) => ( { cell.render('Cell') } )) } ); }) } { !noFooter && ( { footerGroups.map((footerGroup) => ( { (footerGroup.headers as ColumnInstance[]).map((column) => ( { column.render('Footer') } )) } )) } ) }
); } );