Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 18x 18x 18x 18x 18x 18x 18x 4x 18x 18x 18x 18x 18x 18x 4x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 18x 18x 18x 18x 18x 18x 18x 18x 6x 6x 2x 2x 2x 2x 2x 1x | import React, { type ReactNode } from 'react';
import classNames from 'classnames';
import './Table.css';
export interface TableColumn<T = Record<string, unknown>> {
key: string;
header: string;
render?: (value: unknown, row: T, index: number) => React.ReactNode;
sortable?: boolean;
width?: string;
align?: 'left' | 'center' | 'right';
}
export interface TableProps<T = Record<string, unknown>> {
columns: TableColumn<T>[];
data: T[];
loading?: boolean;
emptyMessage?: string;
onRowClick?: (row: T, index: number) => void;
className?: string;
striped?: boolean;
hoverable?: boolean;
bordered?: boolean;
compact?: boolean;
}
function Table<T = Record<string, unknown>>({
columns,
data,
loading = false,
emptyMessage = 'No data available',
onRowClick,
className,
striped = true,
hoverable = true,
bordered = false,
compact = false,
}: TableProps<T>) {
const tableClasses = classNames(
'table',
{
'table-striped': striped,
'table-hover': hoverable,
'table-bordered': bordered,
'table-sm': compact,
},
className
);
const getCellValue = (row: T, column: TableColumn<T>): unknown => {
const keys = column.key.split('.');
let value: unknown = row;
for (const key of keys) {
value = (value as Record<string, unknown>)?.[key];
}
return value;
};
const renderCell = (row: T, column: TableColumn<T>, rowIndex: number): ReactNode => {
const value = getCellValue(row, column);
if (column.render) {
return column.render(value, row, rowIndex);
}
if (value === null || value === undefined) {
return <span className="text-muted">—</span>;
}
if (typeof value === 'boolean') {
return value ? '✓' : '✗';
}
return <>{value}</>;
};
if (loading) {
return (
<div className="table-loading">
<div className="spinner-border text-primary" role="status">
<span className="visually-hidden">Loading...</span>
</div>
</div>
);
}
if (!data || data.length === 0) {
return (
<div className="table-empty">
<p className="text-muted mb-0">{emptyMessage}</p>
</div>
);
}
return (
<div className="table-responsive">
<table className={tableClasses}>
<thead>
<tr>
{columns.map((column) => (
<th
key={column.key}
style={{ width: column.width }}
className={classNames({
'text-center': column.align === 'center',
'text-end': column.align === 'right',
})}
>
{column.header}
</th>
))}
</tr>
</thead>
<tbody>
{data.map((row, rowIndex) => (
<tr
key={rowIndex}
onClick={() => onRowClick?.(row, rowIndex)}
className={classNames({
'cursor-pointer': !!onRowClick,
})}
>
{columns.map((column) => (
<td
key={`${rowIndex}-${column.key}`}
className={classNames({
'text-center': column.align === 'center',
'text-end': column.align === 'right',
})}
>
{renderCell(row, column, rowIndex)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
}
export default Table;
|