260326_02_ENH: 테이블 4종 통합 및 BaseTable 생성

This commit is contained in:
2026-03-26 10:52:31 +09:00
parent 8133ab1ee4
commit ee8bb13209
9 changed files with 760 additions and 797 deletions

View File

@@ -1,205 +1,113 @@
"use client";
import React, { useMemo } from "react";
import { cn, parseNumber } from "@/lib/utils";
import {GaugeColumn} from "../types";
import {roundNumber} from "@/lib/lg-dashboard";
import { roundNumber } from "@/lib/lg-dashboard";
import type { GaugeColumn } from "../types";
import BaseTableRenderer from "./core/BaseTableRenderer";
import { getColumnAlign, toTableCellContent } from "./core/table-core.helpers";
type Props<T extends Record<string, unknown>> = {
columns: GaugeColumn<T>[];
rows: T[];
className?: string;
tableClassName?: 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;
scale?: number;
fillColor?: string;
trackColor?: string;
cellHeight?: number;
valueFormatter?: (value: 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(roundNumber(v)));
const clamp01 = (n: number) => Math.max(0, Math.min(1, n));
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));
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>
<BaseTableRenderer
columns={columns}
wrapperClassName={wrapperClassName}
tableClassName={tableClassName ?? className}
minWidth="100%"
colgroup={
<colgroup>
{columns.map((column, index) => (
<col
key={`${String(column.key)}-${index}`}
style={{
width: column.width ?? (column.rowHead ? 50 : 40),
}}
/>
))}
</tbody>
</table>
</div>
);
}
</colgroup>
}
>
{rows.map((row, rowIndex) => (
<tr key={rowIndex}>
{columns.map((column) => {
const align = getColumnAlign(column);
export default GaugeTable;
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 = column.max ?? 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 ((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;