260127_01_ENH: 커스텀 컴포넌트들 1차 완성 및 일부페이지에 SampleData 적용

This commit is contained in:
2026-01-27 16:59:24 +09:00
parent 76c2626507
commit 8bfa8fa7c4
33 changed files with 3035 additions and 35 deletions

View File

@@ -0,0 +1,219 @@
"use client";
import React, { useMemo } from "react";
import { cn } from "@/lib/utils";
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
type BaseProps = {
title?: string;
/** 테이블 상단(헤더 1행) 콜스팬 셀 내부에 렌더될 차트 */
chart?:
| React.ReactNode
| ((ctx: {
/** 테이블 컬럼(=x축) 기준으로 구성된 ChartData */
chartData: ChartData;
tableCategories: string[];
tableRows: TopChartTableRow[];
}) => React.ReactNode);
/** chart 셀 높이(px) */
chartHeight?: number;
/** 가로 스크롤 min-width */
minWidthPx?: number;
tableClassName?: string;
wrapperClassName?: string;
/** +/-/0 색상/표기 옵션(선택) */
signStyle?: SignStyle<TopChartTableRow>;
};
/**
* 1) (신규) HTML 형태: columns=categories, rows=TopChartTableRow
*/
type HtmlModeProps = BaseProps & {
categories: string[];
rows: TopChartTableRow[];
};
/**
* 2) (호환) 기존 형태: ChartData 기반으로 rows/columns 자동 생성
* - rows = data.categories
* - cols = data.series.name
*/
type ChartDataModeProps = BaseProps & {
data: ChartData;
/** 좌측 rowhead 컬럼 헤더 */
categoryHeader?: string;
/** 값 표기 포맷터 */
valueFormatter?: (v: number) => string;
};
export type TopChartTableProps = HtmlModeProps | ChartDataModeProps;
function toNumber(v: unknown): number | null {
if (v === null || v === undefined) return null;
if (typeof v === "number") return Number.isFinite(v) ? v : null;
if (typeof v === "string") {
const s = v.trim();
if (s === "" || s === "-") return null;
const n = Number(s.replaceAll(",", ""));
return Number.isFinite(n) ? n : null;
}
return null;
}
function formatSignedNumber(n: number, zeroText: string, showPlus: boolean) {
if (n === 0) return zeroText;
if (n > 0) return showPlus ? `+${n}` : `${n}`;
return `${n}`;
}
type CellValue = TopChartTableRow["values"][number];
function toCellValue(v: unknown): CellValue {
if (v === null || v === undefined) return v;
if (typeof v === "number" || typeof v === "string") return v;
return String(v);
}
export default function TopChartTable(props: TopChartTableProps) {
const {
title,
chart,
chartHeight = 260,
minWidthPx = 1600,
tableClassName,
wrapperClassName,
signStyle,
} = props;
const display = signStyle?.display ?? "number";
const arrowWithNumber = signStyle?.arrowWithNumber ?? true;
const showPlus = signStyle?.showPlus ?? true;
const posCls = signStyle?.positiveTextClassName ?? "lg-num-pos";
const negCls = signStyle?.negativeTextClassName ?? "lg-num-neg";
const zeroText = signStyle?.zeroText ?? "-";
const { categories, rows, rowHeadHeader, valueFormatter } = useMemo(() => {
if ("data" in props) {
const d = props.data;
const tableRows: TopChartTableRow[] = d.categories.map((cat, i) => ({
label: String(cat ?? ""),
values: d.series.map((s) => toCellValue(s.data[i] ?? null)),
}));
return {
categories: d.series.map((s) => s.name),
rows: tableRows,
rowHeadHeader: props.categoryHeader ?? "",
valueFormatter: props.valueFormatter,
};
}
return {
categories: props.categories,
rows: props.rows,
rowHeadHeader: "",
valueFormatter: undefined,
};
}, [props]);
// 테이블 컬럼(=categories)을 x축으로 사용하는 chartData를 항상 제공
const chartData: ChartData = useMemo(
() => ({
categories,
series: rows.map((r) => ({
name: r.label,
data: r.values.map((v) => toNumber(v) ?? null),
})),
}),
[categories, rows]
);
const chartNode = useMemo(() => {
if (!chart) return null;
return typeof chart === "function" ? chart({ chartData, tableCategories: categories, tableRows: rows }) : chart;
}, [chart, chartData, categories, rows]);
return (
<div className={cn("w-full", wrapperClassName)}>
{title && (
<div className="mb-2 font-semibold flex items-center gap-2">
<span className="w-3 h-3 bg-primary"></span>
{title}
</div>
)}
<div className="overflow-x-auto">
<table
className={cn("lg-table", tableClassName)}
style={{ minWidth: `${minWidthPx}px` }}
>
<thead>
<tr>
{/* 좌상단: rowhead 헤더(필요 시 표시). HTML과 동일하게 2행 rowspan */}
<th rowSpan={2} className="lg-table__sep-r w-15" style={{ whiteSpace: "nowrap" }}>
{rowHeadHeader}
</th>
<td colSpan={categories.length} style={{ padding: 0, height: chartNode ? chartHeight : 0 }}>
{chartNode ? <div style={{ width: "100%", height: "100%" }}>{chartNode}</div> : null}
</td>
</tr>
<tr className="lg-table__subhead">
{categories.map((c) => (
<th key={c} className={"content-text-xs"}>{c}</th>
))}
</tr>
</thead>
<tbody>
{rows.map((r, ri) => {
const isSignRow = !!(signStyle?.rowSelector && signStyle.rowSelector(r, ri));
return (
<tr key={`${r.label}-${ri}`}>
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
{r.label}
</th>
{categories.map((_, ci) => {
const raw = r.values?.[ci];
const n = toNumber(raw);
// 일반 행
if (!isSignRow) {
if (n === null) return <td key={ci}>-</td>;
const txt = valueFormatter ? valueFormatter(n) : n.toLocaleString();
return <td key={ci}>{txt}</td>;
}
// sign row
if (n === null) return <td key={ci}>-</td>;
if (n === 0) return <td key={ci}>{zeroText}</td>;
const cls = n > 0 ? posCls : negCls;
if (display === "arrow") {
const arrow = n > 0 ? "▲" : "▼";
const num = Math.abs(n);
return (
<td key={ci} className={cls}>
{arrow}
{arrowWithNumber ? ` ${num}` : ""}
</td>
);
}
return (
<td key={ci} className={cls}>
{formatSignedNumber(n, zeroText, showPlus)}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
);
}