Fix table head render
This commit is contained in:
parent
1d333d7d23
commit
04f98339c2
1 changed files with 16 additions and 35 deletions
|
@ -1,9 +1,6 @@
|
|||
import React, { FC, useMemo } from 'react';
|
||||
import { keys, map } from 'ramda';
|
||||
import clsx from 'clsx';
|
||||
|
||||
import { isNilOrEmpty } from '@procyon/utils';
|
||||
|
||||
import type { TableRow as TableRowType } from '../types';
|
||||
import { TableCol } from '../types';
|
||||
import { TableCell } from './TableCell';
|
||||
|
@ -18,46 +15,30 @@ export type TableProps = {
|
|||
};
|
||||
|
||||
export const Table: FC<TableProps> = ({ cols, rows, nthStyle = true, className }) => {
|
||||
const headCells = useMemo(() => {
|
||||
if (!isNilOrEmpty(cols?.data)) {
|
||||
Object.keys(cols.data).map((c) => (
|
||||
<TableHead key={c} className={cols.className}>
|
||||
{cols.data[c].value}
|
||||
</TableHead>
|
||||
))
|
||||
}
|
||||
return null;
|
||||
}, [cols.data]);
|
||||
|
||||
const body = useMemo(() => {
|
||||
if (!rows) {
|
||||
return null;
|
||||
}
|
||||
let rowIndex = 0;
|
||||
return map((row) => {
|
||||
rowIndex++;
|
||||
return (
|
||||
<TableRow nthStyle={nthStyle} key={rowIndex} className={row.className}>
|
||||
{map((cell) => {
|
||||
const head = cols.data[cell];
|
||||
return (
|
||||
<TableCell
|
||||
key={cell}
|
||||
title={head.value}
|
||||
className={row.data[cell].className}
|
||||
>
|
||||
return rows.map((row, i) => (
|
||||
<TableRow nthStyle={nthStyle} key={i} className={row.className}>
|
||||
{Object.keys(cols.data).map((cell) => (
|
||||
<TableCell key={cell} title={cols.data[cell].value} className={row.data[cell].className}>
|
||||
{row.data[cell].value}
|
||||
</TableCell>
|
||||
);
|
||||
}, keys(cols.data))}
|
||||
))}
|
||||
</TableRow>
|
||||
);
|
||||
}, rows);
|
||||
));
|
||||
}, [cols.data, rows, nthStyle]);
|
||||
|
||||
return (
|
||||
<div className={clsx('tablek', className)}>
|
||||
<div className="tablek-head-row">{headCells}</div>
|
||||
<div className="tablek-head-row">
|
||||
{Object.keys(cols.data).map((c) => (
|
||||
<TableHead key={c} className={cols.className}>
|
||||
{cols.data[c].value}
|
||||
</TableHead>
|
||||
))}
|
||||
</div>
|
||||
{body}
|
||||
</div>
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue