160 lines
5.4 KiB
TypeScript
160 lines
5.4 KiB
TypeScript
"use client";
|
|
|
|
import type React from "react";
|
|
import { cn, parseNumber } from "@/lib/utils";
|
|
import { isSampleRow } from "@/lib/lg-dashboard";
|
|
import type { DataColumn, SignStyle } from "../types";
|
|
import BaseTableRenderer from "./core/BaseTableRenderer";
|
|
import {
|
|
appendCellMarker,
|
|
formatSignedValue,
|
|
getColumnAlign,
|
|
getSignCellBackground,
|
|
getSignTextClass,
|
|
resolveSignStyle,
|
|
shouldApplySignToColumn,
|
|
toRoundedNumber,
|
|
toTableCellContent,
|
|
type ResolvedSignStyle,
|
|
} from "./core/table-core.helpers";
|
|
|
|
export type MergedTableCell<T> = {
|
|
key: keyof T;
|
|
content: React.ReactNode;
|
|
startRowIndex?: number;
|
|
rowSpan?: number;
|
|
className?: string;
|
|
};
|
|
|
|
type Props<T extends Record<string, unknown>> = {
|
|
columns: DataColumn<T>[];
|
|
rows: T[];
|
|
title?: string;
|
|
wrapperClassName?: string;
|
|
tableClassName?: string;
|
|
subheadRowClassName?: string;
|
|
minWidth?: number;
|
|
signStyle?: SignStyle<T>;
|
|
signStyleFallback?: Partial<ResolvedSignStyle>;
|
|
fillGreaterThan5?: boolean;
|
|
starUnder30Sample?: boolean;
|
|
unitText?: string;
|
|
mergedCell?: MergedTableCell<T>;
|
|
getCellClassName?: (row: T, column: DataColumn<T>, rowIndex: number) => string | undefined;
|
|
};
|
|
|
|
export default function BaseDataTable<T extends Record<string, unknown>>({
|
|
columns,
|
|
rows,
|
|
title,
|
|
wrapperClassName,
|
|
tableClassName,
|
|
subheadRowClassName,
|
|
minWidth,
|
|
signStyle,
|
|
signStyleFallback,
|
|
fillGreaterThan5 = false,
|
|
starUnder30Sample = false,
|
|
unitText,
|
|
mergedCell,
|
|
getCellClassName,
|
|
}: Readonly<Props<T>>) {
|
|
const resolvedSignStyle = resolveSignStyle(signStyle, signStyleFallback);
|
|
const mergedCellStart = mergedCell?.startRowIndex ?? 0;
|
|
const mergedCellSpan = mergedCell?.rowSpan ?? Math.max(0, rows.length - mergedCellStart);
|
|
|
|
return (
|
|
<BaseTableRenderer
|
|
columns={columns}
|
|
title={title}
|
|
unitText={unitText}
|
|
showSampleNotice={starUnder30Sample}
|
|
wrapperClassName={wrapperClassName}
|
|
tableClassName={tableClassName}
|
|
subheadRowClassName={subheadRowClassName}
|
|
minWidth={minWidth}
|
|
>
|
|
{rows.map((row, rowIndex) => {
|
|
const sampleRow = isSampleRow((row.rowLabel as string) ?? "");
|
|
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
|
const rowTotal = parseNumber(row.total);
|
|
|
|
return (
|
|
<tr key={rowIndex}>
|
|
{columns.map((column) => {
|
|
const dynamicCellClassName = getCellClassName?.(row, column, rowIndex);
|
|
|
|
if (mergedCell && column.key === mergedCell.key && rowIndex >= mergedCellStart) {
|
|
if (rowIndex > mergedCellStart) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<td
|
|
key={String(column.key)}
|
|
rowSpan={mergedCellSpan}
|
|
className={cn(column.cellClassName, mergedCell.className)}
|
|
style={{ padding: 0, verticalAlign: "middle" }}
|
|
>
|
|
{mergedCell.content}
|
|
</td>
|
|
);
|
|
}
|
|
|
|
const rawValue = row[column.key];
|
|
const align = getColumnAlign(column);
|
|
const defaultContent = column.render ? column.render(row) : toTableCellContent(rawValue);
|
|
const numericValue = column.numeric ? parseNumber(rawValue ?? defaultContent) ?? 0 : 0;
|
|
const applySign = signRow && shouldApplySignToColumn(column, signStyle);
|
|
|
|
if (column.rowHead) {
|
|
return (
|
|
<th
|
|
key={String(column.key)}
|
|
scope="row"
|
|
className={cn("lg-table__rowhead", column.cellClassName, dynamicCellClassName)}
|
|
style={{ textAlign: align }}
|
|
>
|
|
{defaultContent}
|
|
</th>
|
|
);
|
|
}
|
|
|
|
if (applySign) {
|
|
const roundedValue = toRoundedNumber(rawValue);
|
|
const displayText = roundedValue === null ? "-" : formatSignedValue(roundedValue, resolvedSignStyle);
|
|
const cellStyle: React.CSSProperties =
|
|
resolvedSignStyle.paint === "cell"
|
|
? { textAlign: align, background: getSignCellBackground(roundedValue ?? 0, resolvedSignStyle) }
|
|
: { textAlign: align };
|
|
|
|
return (
|
|
<td key={String(column.key)} className={cn(column.cellClassName, dynamicCellClassName)} style={cellStyle}>
|
|
<span className={cn(roundedValue === null ? "text-gray-500" : getSignTextClass(roundedValue, resolvedSignStyle))}>
|
|
{displayText}
|
|
</span>
|
|
</td>
|
|
);
|
|
}
|
|
|
|
const shouldFillCell = fillGreaterThan5 && rowTotal !== null && numericValue > rowTotal + 5;
|
|
const shouldMarkSample = starUnder30Sample && sampleRow && numericValue < 30;
|
|
const displayContent = shouldMarkSample ? appendCellMarker(defaultContent, "*") : defaultContent;
|
|
|
|
return (
|
|
<td
|
|
key={String(column.key)}
|
|
className={cn(column.cellClassName, dynamicCellClassName, shouldFillCell ? "bg-red-200" : "")}
|
|
style={{ textAlign: align }}
|
|
>
|
|
{displayContent}
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
);
|
|
})}
|
|
</BaseTableRenderer>
|
|
);
|
|
}
|