import React, { useCallback } from 'react'; import { Table as SemanticTable } from 'semantic-ui-react'; import { map } from 'lodash-es'; import { useRelatableStateContext } from '../states'; import arrayHasItems from '../utils/array-has-items'; import { columnHasActions } from '../utils/column-actions'; import getSemanticTableProps from '../utils/get-semantic-table-props'; import isLastIndex from '../utils/is-last-index'; import getRowNumber from '../utils/get-row-number'; import ColumnActions from './column-actions'; import { BodyRow } from './renderers'; import RowActions from './renderers/row-actions'; export interface ITableProps { // used for rendering loading animation and empty rows loading?: boolean; expectedRowCount?: number; headless?: boolean; // semantic ui react props https://react.semantic-ui.com/collections/table/ attached?: boolean | string; basic?: boolean | string; className?: string; collapsing?: boolean; color?: string; compact?: boolean | string; definition?: boolean; fixed?: boolean; inverted?: boolean; padded?: boolean | string; singleLine?: boolean; size?: string; striped?: boolean; structured?: boolean; textAlign?: string; verticalAlign?: string; } export default function Table({ loading, expectedRowCount, headless, ...rest }: ITableProps): JSX.Element { const { getTableProps, headerGroups, state: { pageIndex = 0, pageSize = 1, }, _rowsToUse: rows, // @todo: handle this more gracefully inside addOns prepareRow, availableTableActions, onCustomSelectionChange, } = useRelatableStateContext(); const { className = '', ...semanticTableProps } = getSemanticTableProps(rest); const onSelectAllClick = useCallback( (select: boolean) => { onCustomSelectionChange!(rows, select); }, [onCustomSelectionChange, rows], ); return ( {!headless && {map(headerGroups, (headerGroup, index: number) => ( {isLastIndex(headerGroups, index) && } {map(headerGroup.headers, (column: any) => { const headerProps = column.getHeaderProps(); const hasActions = isLastIndex(headerGroups, index) && columnHasActions(column, availableTableActions); if (column.colSpan === 0) return null; return hasActions ? : {column.render('Header')} ; }, )} ))} } {map(rows, (row, index: number) => { prepareRow(row); return ( ); })} {/* render empty rows when passed expectedRowCount and no data */} {!arrayHasItems(rows) && loading && expectedRowCount ? map(Array(expectedRowCount), (_, index) => (
)) : null } ); }