2020-08-07 08:02:24 +02:00
|
|
|
import React from 'react';
|
2020-08-07 09:24:01 +02:00
|
|
|
import classNames from 'classnames';
|
2020-09-11 07:21:38 +02:00
|
|
|
import NoData from 'components/common/NoData';
|
2020-08-07 08:02:24 +02:00
|
|
|
import styles from './Table.module.css';
|
|
|
|
|
2020-10-09 13:21:59 +02:00
|
|
|
export default function Table({
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
empty,
|
|
|
|
className,
|
|
|
|
bodyClassName,
|
|
|
|
rowKey,
|
|
|
|
children,
|
|
|
|
}) {
|
2020-08-11 04:54:03 +02:00
|
|
|
if (empty && rows.length === 0) {
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:02:24 +02:00
|
|
|
return (
|
2020-10-09 11:56:15 +02:00
|
|
|
<div className={classNames(styles.table, className)}>
|
2020-08-18 07:47:58 +02:00
|
|
|
<div className={classNames(styles.header, 'row')}>
|
2020-08-09 08:48:43 +02:00
|
|
|
{columns.map(({ key, label, className, style, header }) => (
|
2020-08-07 11:27:12 +02:00
|
|
|
<div
|
|
|
|
key={key}
|
2020-08-09 08:48:43 +02:00
|
|
|
className={classNames(styles.head, className, header?.className)}
|
|
|
|
style={{ ...style, ...header?.style }}
|
2020-08-07 11:27:12 +02:00
|
|
|
>
|
2020-08-07 09:24:01 +02:00
|
|
|
{label}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2020-10-09 13:21:59 +02:00
|
|
|
<div className={classNames(styles.body, bodyClassName)}>
|
2020-09-11 07:21:38 +02:00
|
|
|
{rows.length === 0 && <NoData />}
|
2020-10-09 13:21:59 +02:00
|
|
|
{!children &&
|
|
|
|
rows.map((row, index) => {
|
|
|
|
const id = rowKey ? rowKey(row) : index;
|
|
|
|
return <TableRow key={id} columns={columns} row={row} />;
|
|
|
|
})}
|
|
|
|
{children}
|
2020-08-07 09:24:01 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-08-07 08:02:24 +02:00
|
|
|
);
|
|
|
|
}
|
2020-10-09 13:21:59 +02:00
|
|
|
|
|
|
|
export const TableRow = ({ columns, row }) => (
|
|
|
|
<div className={classNames(styles.row, 'row')}>
|
|
|
|
{columns.map(({ key, render, className, style, cell }, index) => (
|
|
|
|
<div
|
|
|
|
key={`${key}-${index}`}
|
|
|
|
className={classNames(styles.cell, className, cell?.className)}
|
|
|
|
style={{ ...style, ...cell?.style }}
|
|
|
|
>
|
|
|
|
{render ? render(row) : row[key]}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|