import classnames from 'classnames' import * as R from 'ramda' import React, { useState, useEffect } from 'react' import { AutoSizer, List, CellMeasurer, CellMeasurerCache, } from 'react-virtualized' import { Table, TBody, THead, Tr, Td, Th } from '../fake-table/Table' import { H4 } from '../typography' import ExpandClosedIcon from '../../styling/icons/action/expand/closed.svg?react' import ExpandOpenIcon from '../../styling/icons/action/expand/open.svg?react' import { EmptyTable } from '../table' const Row = ({ id, index, elements, data, width, Details, expanded, expandRow, expWidth, expandable, onClick, size, ...props }) => { const hasPointer = onClick || expandable const trClasses = { 'cursor-pointer': hasPointer, 'border-2 border-transparent': true, 'border-2 border-zircon shadow-md': expanded, } return (
{ expandable && expandRow(id, data) onClick && onClick(data) }} error={data.error || data.hasError || data.batchError} shouldShowError={false} errorMessage={data.errorMessage || data.hasError || data.batchError}> {elements.map(({ view = it => it?.toString(), ...props }, idx) => ( {view(data)} ))} {expandable && ( )}
{expanded && (
)}
) } const DataTable = ({ elements = [], data = [], Details, className, tableClassName, expandable, initialExpanded, onClick, loading, maxWidth = 1200, emptyText, rowSize, ...props }) => { const [expanded, setExpanded] = useState(initialExpanded) useEffect(() => setExpanded(initialExpanded), [initialExpanded]) const coreWidth = R.compose(R.sum, R.map(R.prop('width')))(elements) const expWidth = maxWidth - coreWidth const width = coreWidth + (expandable ? expWidth : 0) const expandRow = (id, data) => { if (data.id) { cache.clear(data.id) setExpanded(data.id === expanded ? null : data.id) } else { cache.clear(id) setExpanded(id === expanded ? null : id) } } const cache = new CellMeasurerCache({ defaultHeight: 58, fixedWidth: true, }) function rowRenderer({ index, key, parent, style }) { return ( {({ registerChild }) => (
)}
) } return (
{elements.map(({ width, className, textAlign, header }, idx) => ( ))} {expandable && } {loading &&

Loading...

} {!loading && R.isEmpty(data) && } {!loading && !R.isEmpty(data) && ( {({ height }) => ( )} )}
{header}
) } export default DataTable