260127_01_ENH: 커스텀 컴포넌트들 1차 완성 및 일부페이지에 SampleData 적용
This commit is contained in:
162
src/components/lge-dashboard/table/GaugeTable.tsx
Normal file
162
src/components/lge-dashboard/table/GaugeTable.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { cn } 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
|
||||
|
||||
/** 숫자 표시 여부/포맷 */
|
||||
showValue?: boolean; // 기본 true
|
||||
valueFormatter?: (v: number) => string;
|
||||
};
|
||||
|
||||
const num = (v: unknown) => {
|
||||
if (typeof v === "number" && Number.isFinite(v)) return v;
|
||||
if (typeof v === "string") {
|
||||
const n = Number(v.replaceAll(",", "").replaceAll("%", "").trim());
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const GaugeTable = <T extends Record<string, unknown>>({
|
||||
columns,
|
||||
rows,
|
||||
className,
|
||||
wrapperClassName,
|
||||
scale = 1,
|
||||
fillColor = "hsl(var(--primary) / 0.85)",
|
||||
trackColor = "hsl(var(--lg-gray-200) / 0.85)",
|
||||
cellHeight = 26,
|
||||
showValue = true,
|
||||
valueFormatter,
|
||||
}: Props<T>) => {
|
||||
const hasGroup = columns.some((c) => !!c.group);
|
||||
|
||||
const groupCells = useMemo(() => {
|
||||
if (!hasGroup) return [];
|
||||
const res: { label: string; span: number }[] = [];
|
||||
let i = 0;
|
||||
while (i < columns.length) {
|
||||
const g = columns[i].group ?? "";
|
||||
let span = 1;
|
||||
let j = i + 1;
|
||||
while (j < columns.length && (columns[j].group ?? "") === g) {
|
||||
span++;
|
||||
j++;
|
||||
}
|
||||
res.push({ label: g, span });
|
||||
i = j;
|
||||
}
|
||||
return res;
|
||||
}, [columns, hasGroup]);
|
||||
|
||||
const fmt = valueFormatter ?? ((v: number) => String(Math.round(v)));
|
||||
|
||||
return (
|
||||
<div className={cn("w-full overflow-x-auto", wrapperClassName)}>
|
||||
<table className={cn("lg-table", className)}>
|
||||
<thead>
|
||||
{hasGroup && (
|
||||
<tr className="lg-table__group">
|
||||
{groupCells.map((g, idx) => (
|
||||
<th key={`${g.label}-${idx}`} colSpan={g.span}>
|
||||
{g.label}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
)}
|
||||
<tr className={hasGroup ? "lg-table__subhead" : undefined}>
|
||||
{columns.map((c) => (
|
||||
<th key={String(c.key)} style={{ width: c.width }}>
|
||||
{c.header}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((row, rIdx) => (
|
||||
<tr key={rIdx}>
|
||||
{columns.map((c) => {
|
||||
const align = c.align ?? (c.rowHead ? "left" : "center");
|
||||
|
||||
if (c.rowHead) {
|
||||
const raw = row[c.key];
|
||||
const label = c.renderLabel
|
||||
? c.renderLabel(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 = num(raw);
|
||||
const max = c.max ?? 100;
|
||||
const ratio = v === null ? 0 : Math.max(0, Math.min(1, (v * scale) / max));
|
||||
const pct = ratio * 100;
|
||||
|
||||
return (
|
||||
<td key={String(c.key)} style={{ textAlign: align }}>
|
||||
<div
|
||||
className="relative overflow-hidden rounded-md"
|
||||
style={{
|
||||
height: cellHeight,
|
||||
background: trackColor,
|
||||
border: "1px solid hsl(var(--border))",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className="absolute inset-y-0 left-0"
|
||||
style={{
|
||||
width: `${pct}%`,
|
||||
background: fillColor,
|
||||
}}
|
||||
/>
|
||||
{showValue && (
|
||||
<div
|
||||
className="relative z-10 flex h-full items-center justify-center content-text-sm"
|
||||
style={{ color: "hsl(var(--lg-gray-900))" }}
|
||||
>
|
||||
{v === null ? "-" : fmt(v)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default GaugeTable;
|
||||
Reference in New Issue
Block a user