115 lines
3.6 KiB
TypeScript
115 lines
3.6 KiB
TypeScript
"use client";
|
|
|
|
import { cn, parseNumber } from "@/lib/utils";
|
|
import { roundNumber } from "@/lib/lg-dashboard";
|
|
import type { GaugeColumn } from "../types";
|
|
import BaseTableRenderer from "./core/BaseTableRenderer";
|
|
import { getColumnAlign, getLeafColumns, toTableCellContent } from "./core/table-core.helpers";
|
|
|
|
type Props<T extends Record<string, unknown>> = {
|
|
columns: GaugeColumn<T>[];
|
|
rows: T[];
|
|
className?: string;
|
|
tableClassName?: string;
|
|
wrapperClassName?: string;
|
|
scale?: number;
|
|
fillColor?: string;
|
|
trackColor?: string;
|
|
cellHeight?: number;
|
|
valueFormatter?: (value: number) => string;
|
|
};
|
|
|
|
const GaugeTable = <T extends Record<string, unknown>>({
|
|
columns,
|
|
rows,
|
|
className,
|
|
tableClassName,
|
|
wrapperClassName,
|
|
scale = 1,
|
|
fillColor = "hsl(var(--lg-gray-200) / 0.85)",
|
|
trackColor = "hsl(var(--lg-pink-300) / 0.2)",
|
|
cellHeight = 26,
|
|
valueFormatter,
|
|
}: Props<T>) => {
|
|
const formatValue = valueFormatter ?? ((value: number) => String(roundNumber(value)));
|
|
const clampRatio = (value: number) => Math.max(0, Math.min(1, value));
|
|
const leafColumns = getLeafColumns(columns);
|
|
|
|
return (
|
|
<BaseTableRenderer
|
|
columns={columns}
|
|
wrapperClassName={wrapperClassName}
|
|
tableClassName={tableClassName ?? className}
|
|
minWidth="100%"
|
|
colgroup={
|
|
<colgroup>
|
|
{leafColumns.map((column, index) => (
|
|
<col
|
|
key={`${String(column.key)}-${index}`}
|
|
style={{
|
|
width: column.width ?? (column.rowHead ? 50 : 40),
|
|
}}
|
|
/>
|
|
))}
|
|
</colgroup>
|
|
}
|
|
>
|
|
{rows.map((row, rowIndex) => (
|
|
<tr key={rowIndex}>
|
|
{leafColumns.map((column) => {
|
|
const align = getColumnAlign(column);
|
|
|
|
if (column.rowHead) {
|
|
const content = column.render ? column.render(row) : toTableCellContent(row[column.key]);
|
|
return (
|
|
<th
|
|
key={String(column.key)}
|
|
scope="row"
|
|
className={cn("lg-table__rowhead", column.cellClassName)}
|
|
style={{ textAlign: align }}
|
|
>
|
|
{content}
|
|
</th>
|
|
);
|
|
}
|
|
|
|
const rawValue = row[column.key];
|
|
const numericValue = parseNumber(rawValue);
|
|
const max = "max" in column ? column.max ?? 100 : 100;
|
|
const ratio = numericValue === null ? 0 : clampRatio((numericValue * scale) / max);
|
|
const percentage = ratio * 100;
|
|
const background =
|
|
numericValue === null ? undefined : `linear-gradient(to right, ${fillColor} ${percentage}%, ${trackColor} 0%)`;
|
|
|
|
if (("cellType" in column ? column.cellType : "gauge") === "number") {
|
|
return (
|
|
<td key={String(column.key)} className={cn("px-2 content-text-sm", column.cellClassName)} style={{ textAlign: align }}>
|
|
{numericValue === null ? "-" : formatValue(numericValue)}
|
|
</td>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<td
|
|
key={String(column.key)}
|
|
className={cn("px-2 content-text-sm", column.cellClassName)}
|
|
style={{
|
|
textAlign: "left",
|
|
height: cellHeight,
|
|
whiteSpace: "nowrap",
|
|
background,
|
|
backgroundClip: "padding-box",
|
|
}}
|
|
>
|
|
{numericValue === null ? "-" : formatValue(numericValue)}
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
))}
|
|
</BaseTableRenderer>
|
|
);
|
|
};
|
|
|
|
export default GaugeTable;
|