import { makeStyles } from '@mui/styles'
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 'src/components/fake-table/Table'
import { H4 } from 'src/components/typography'
import ExpandClosedIcon from 'src/styling/icons/action/expand/closed.svg?react'
import ExpandOpenIcon from 'src/styling/icons/action/expand/open.svg?react'
import styles from 'src/components/tables/DataTable.styles'
const useStyles = makeStyles(styles)
const Row = ({
id,
elements,
data,
width,
Details,
expanded,
expandRow,
expWidth,
expandable,
onClick
}) => {
const classes = useStyles()
const hasPointer = onClick || expandable
const trClasses = {
[classes.pointer]: hasPointer,
[classes.row]: true,
[classes.expanded]: expanded
}
return (
{
expandable && expandRow(id)
onClick && onClick(data)
}}
error={data.error}
errorMessage={data.errorMessage}>
{elements.map(({ view = it => it?.toString(), ...props }, idx) => (
|
{view(data)}
|
))}
{expandable && (
|
)}
{expandable && expanded && (
|
)}
)
}
const DataTable = ({
elements = [],
data = [],
Details,
className,
expandable,
initialExpanded,
onClick,
loading,
emptyText,
extraHeight,
...props
}) => {
const [expanded, setExpanded] = useState(initialExpanded)
useEffect(() => setExpanded(initialExpanded), [initialExpanded])
const coreWidth = R.compose(R.sum, R.map(R.prop('width')))(elements)
const expWidth = 850 - coreWidth
const width = coreWidth + (expandable ? expWidth : 0)
const classes = useStyles({ width })
const expandRow = id => {
setExpanded(id === expanded ? null : id)
}
const cache = new CellMeasurerCache({
defaultHeight: 62,
fixedWidth: true
})
function rowRenderer({ index, key, parent, style }) {
return (
)
}
return (
{elements.map(({ width, className, textAlign, header }, idx) => (
|
{header}
|
))}
{expandable && | }
{loading && Loading...
}
{!loading && R.isEmpty(data) && {emptyText}
}
{() => (
)}
)
}
export default DataTable