Files
lg-cxdash-front/src/components/lge-dashboard/table/GaugeTable.tsx

204 lines
6.9 KiB
TypeScript

"use client";
import React, { useMemo } from "react";
import { cn, parseNumber } from "@/lib/utils";
import {GaugeColumn} from "../types";
type Props<T extends Record<string, unknown>> = {
columns: GaugeColumn<T>[];
rows: T[];
className?: string;
wrapperClassName?: string;
/** value가 0~100이 아니라 0~1이면 scale=100 */
scale?: number; // 기본 1
/** 색상(기본: primary 채움 + gray 트랙) */
fillColor?: string; // ex) "hsl(var(--primary) / 0.9)"
trackColor?: string; // ex) "hsl(var(--lg-gray-200) / 0.9)"
/** 셀 높이 */
cellHeight?: number; // 기본 26
/** 숫자 표시 여부/포맷 */
valueFormatter?: (v: number) => string;
};
// num은 utils.ts의 parseNumber로 통합됨
const GaugeTable = <T extends Record<string, unknown>>({
columns,
rows,
className,
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 hasGroup = columns.some((c) => !!c.group) || columns.some((c) => c.headRowSpan === 2);
const groupCells = useMemo(() => {
type GroupCell =
| { kind: "rowspan"; label: string; colSpan: 1; rowSpan: 2; className?: string }
| { kind: "group"; label: string; colSpan: number; rowSpan: 1; className?: string }
| { kind: "blank"; label: string; colSpan: 1; rowSpan: 1; className?: string };
if (!hasGroup) return [] as GroupCell[];
const res: GroupCell[] = [];
let i = 0;
while (i < columns.length) {
const c = columns[i];
if (c.headRowSpan === 2) {
res.push({ kind: "rowspan", label: c.header, colSpan: 1, rowSpan: 2 });
i++;
continue;
}
const g = c.group;
if (!g) {
// group 미지정: 1칸짜리 blank group 셀
res.push({ kind: "blank", label: "", colSpan: 1, rowSpan: 1 });
i++;
continue;
}
// 같은 group 연속 구간 colSpan
let span = 1;
let j = i + 1;
while (j < columns.length && columns[j].headRowSpan !== 2 && columns[j].group === g) {
span++;
j++;
}
res.push({ kind: "group", label: g, colSpan: span, rowSpan: 1 });
i = j;
}
return res;
}, [columns, hasGroup]);
const fmt = valueFormatter ?? ((v: number) => String(Math.round(v)));
const clamp01 = (n: number) => Math.max(0, Math.min(1, n));
return (
<div className={cn("w-full overflow-x-auto", wrapperClassName)}>
<table className={cn("lg-table", className)} style={{minWidth:"100%"}}>
<colgroup>
{columns.map((c, i) => (
<col
key={`${String(c.key)}-${i}`}
style={{
width:
c.width ??
(c.rowHead ? 50 : 40), // 기본 폭(필요시 숫자 조절)
}}
/>
))}
</colgroup>
<thead>
{hasGroup && (
<tr className="lg-table__group">
{groupCells.map((g, idx) => (
<th
key={`${g.kind}-${g.label}-${idx}`}
colSpan={g.colSpan}
rowSpan={g.rowSpan}
className={g.className}
>
{g.label}
</th>
))}
</tr>
)}
<tr className={hasGroup ? "lg-table__subhead" : undefined}>
{columns.filter((c) => c.headRowSpan !== 2).map((c) => (
<th key={String(c.key)}>
{c.header}
</th>
))}
</tr>
</thead>
<tbody>
{rows.map((row, rIdx) => (
<tr key={rIdx}>
{columns.map((c) => {
const align = c.align ?? (c.rowHead ? "left" : "center");
const cellType = c.cellType ?? "gauge";
if (c.rowHead) {
const raw = row[c.key];
const label = c.render
? c.render(row)
: raw === null || raw === undefined
? ""
: typeof raw === "string" || typeof raw === "number"
? raw
: String(raw);
return (
<th
key={String(c.key)}
className="lg-table__rowhead"
style={{ textAlign: align }}
>
{label}
</th>
);
}
const raw = row[c.key];
const v = parseNumber(raw);
const max = c.max ?? 100;
const ratio = v === null ? 0 : clamp01((v * scale) / max);
const pct = ratio * 100;
const fill = fillColor;
const track = trackColor;
const cellBg =
v === null
? undefined
: `linear-gradient(to right, ${fill} ${pct}%, ${track} 0%)`;
if (cellType === "number") {
return (
<td
key={String(c.key)}
className="px-2 content-text-sm"
style={{ textAlign: align }}
>
{v === null ? "-" : fmt(v)}
</td>
);
}
return (
<td
key={String(c.key)}
className="px-2 content-text-sm"
style={{
textAlign: "left",
height: cellHeight,
whiteSpace: "nowrap",
background: cellBg,
backgroundClip: "padding-box",
}}
>
{v === null ? "-" : fmt(v)}
</td>
);
})}
</tr>
))}
</tbody>
</table>
</div>
);
}
export default GaugeTable;