import React from 'react'; import Pagination from 'components/common/Pagination/Pagination'; import { Table } from 'components/common/table/Table/Table.styled'; import * as S from 'components/common/table/TableHeaderCell/TableHeaderCell.styled'; import TableHeaderCell from 'components/common/table/TableHeaderCell/TableHeaderCell'; import { TableState } from 'lib/hooks/useTableState'; import { isColumnElement, SelectCell, TableHeaderCellProps, } from './TableColumn'; import { TableRow } from './TableRow'; interface SmartTableProps { tableState: TableState; allSelectable?: boolean; selectable?: boolean; className?: string; placeholder?: string; isFullwidth?: boolean; paginated?: boolean; hoverable?: boolean; } export const SmartTable = ({ children, tableState, selectable = false, allSelectable = false, placeholder = 'No Data Found', isFullwidth = false, paginated = false, hoverable = false, }: React.PropsWithChildren>) => { const handleRowSelection = React.useCallback( (row: T, checked: boolean) => { tableState.setRowsSelection([row], checked); }, [tableState] ); const headerRow = React.useMemo(() => { const headerCells = React.Children.map(children, (child) => { if (!isColumnElement(child)) { return child; } const { headerCell, title, orderValue } = child.props; const HeaderCell = headerCell as | React.FC> | undefined; return HeaderCell ? ( ) : ( // TODO types will be changed after fixing TableHeaderCell ); }); return ( {allSelectable ? ( ) : ( )} {headerCells} ); }, [children, allSelectable, tableState]); const bodyRows = React.useMemo(() => { if (tableState.data.length === 0) { const colspan = React.Children.count(children) + +selectable; return ( {placeholder} ); } return tableState.data.map((dataItem, index) => { return ( {children} ); }); }, [ children, handleRowSelection, hoverable, placeholder, selectable, tableState, ]); return ( <> {headerRow}{bodyRows}
{paginated && tableState.totalPages !== undefined && ( )} ); };