260326_02_ENH: 테이블 4종 통합 및 BaseTable 생성
This commit is contained in:
@@ -1,21 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import type { DataColumn, SignStyle } from "../types";
|
||||
import SectionTitle from "../SectionTitle";
|
||||
import type React from "react";
|
||||
import { TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
||||
import {isSampleRow, roundNumber} from "@/lib/lg-dashboard";
|
||||
import {SAMPLE_SIZE_NOTICE, UNIT_TEXT, VIEW_DECIMAL_POINT} from "@/const/lg-common.const";
|
||||
import { UNIT_TEXT } from "@/const/lg-common.const";
|
||||
import type { DataColumn, SignStyle } from "../types";
|
||||
import BaseDataTable from "../table/BaseDataTable";
|
||||
|
||||
type MergedChartCell<T> = {
|
||||
/** 차트를 삽입할 컬럼 key(해당 컬럼 td는 startRow에서만 렌더되고 rowSpan 적용) */
|
||||
key: keyof T;
|
||||
/** rowSpan 시작 row index (기본 0) */
|
||||
startRowIndex?: number;
|
||||
/** rowSpan 수 (기본: rows.length - startRowIndex) */
|
||||
rowSpan?: number;
|
||||
/** chart td 클래스 (예: lg-table__sep-r) */
|
||||
className?: string;
|
||||
};
|
||||
|
||||
@@ -23,42 +17,17 @@ type Props<T extends Record<string, unknown>> = {
|
||||
title?: string;
|
||||
columns: DataColumn<T>[];
|
||||
rows: T[];
|
||||
/** 머지된 셀 안에 렌더할 차트 */
|
||||
chart: React.ReactNode;
|
||||
/** 어떤 컬럼을 머지-차트 셀로 교체할지 */
|
||||
chartCell: MergedChartCell<T>;
|
||||
|
||||
/** +/-/0 표기/색상 옵션(선택) */
|
||||
signStyle?: SignStyle<T>;
|
||||
|
||||
wrapperClassName?: string;
|
||||
tableClassName?: string;
|
||||
minWidthPx?: number;
|
||||
minWidth?: number;
|
||||
starUnder30Sample?: boolean;
|
||||
unitText?: string;
|
||||
};
|
||||
|
||||
// toNumber는 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
function renderSigned(
|
||||
n: number,
|
||||
opts: {
|
||||
display: "number" | "arrow";
|
||||
showPlus: boolean;
|
||||
arrowWithNumber: boolean;
|
||||
zeroText: string;
|
||||
}
|
||||
) {
|
||||
if (n === 0) return opts.zeroText;
|
||||
if (opts.display === "arrow") {
|
||||
const arrow = n > 0 ? "▲" : "▼";
|
||||
const num = Math.abs(n);
|
||||
return opts.arrowWithNumber ? `${arrow} ${num}` : arrow;
|
||||
}
|
||||
if (n > 0) return opts.showPlus ? `+${n}` : `${n}`;
|
||||
return `${n}`;
|
||||
}
|
||||
|
||||
export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
title,
|
||||
columns,
|
||||
@@ -69,207 +38,34 @@ export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||
wrapperClassName,
|
||||
tableClassName,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.COMPACT,
|
||||
minWidth,
|
||||
starUnder30Sample = true,
|
||||
unitText = UNIT_TEXT,
|
||||
}: Props<T>) {
|
||||
const hasGroupHeader = useMemo(
|
||||
() => columns.some((c) => c.group) || columns.some((c) => c.headRowSpan === 2),
|
||||
[columns]
|
||||
);
|
||||
|
||||
const groupCells = useMemo(() => {
|
||||
if (!hasGroupHeader) return [] as Array<
|
||||
| { 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 }
|
||||
>;
|
||||
|
||||
type Cell =
|
||||
| { 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 };
|
||||
|
||||
const out: Cell[] = [];
|
||||
let i = 0;
|
||||
while (i < columns.length) {
|
||||
const c = columns[i];
|
||||
if (c.headRowSpan === 2) {
|
||||
out.push({
|
||||
kind: "rowspan",
|
||||
label: c.header,
|
||||
colSpan: 1,
|
||||
rowSpan: 2,
|
||||
className: c.headerClassName,
|
||||
});
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const g = c.group ?? "";
|
||||
if (!g) {
|
||||
out.push({ kind: "blank", label: "", colSpan: 1, rowSpan: 1, className: c.headerClassName });
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
let span = 1;
|
||||
while (
|
||||
i + span < columns.length &&
|
||||
(columns[i + span].headRowSpan ?? 1) !== 2 &&
|
||||
(columns[i + span].group ?? "") === g
|
||||
) {
|
||||
span += 1;
|
||||
}
|
||||
|
||||
out.push({ kind: "group", label: g, colSpan: span, rowSpan: 1, className: columns[i + span -1].headerClassName });
|
||||
i += span;
|
||||
}
|
||||
return out;
|
||||
}, [columns, hasGroupHeader]);
|
||||
|
||||
const display = signStyle?.display ?? "number";
|
||||
const arrowWithNumber = signStyle?.arrowWithNumber ?? true;
|
||||
const showPlus = signStyle?.showPlus ?? true;
|
||||
const zeroText = signStyle?.zeroText ?? "-";
|
||||
const posCls = signStyle?.positiveTextClassName ?? "lg-num-pos";
|
||||
const negCls = signStyle?.negativeTextClassName ?? "lg-num-neg";
|
||||
const zeroCls = signStyle?.zeroTextClassName ?? "";
|
||||
|
||||
const start = chartCell.startRowIndex ?? 0;
|
||||
const rowSpan = chartCell.rowSpan ?? Math.max(0, rows.length - start);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
{ title && <SectionTitle title={title} />}
|
||||
{
|
||||
(unitText || starUnder30Sample) &&
|
||||
<div className="ml-auto text-gray-500 content-text-xs font-semibold text-right">
|
||||
{unitText}
|
||||
{starUnder30Sample && <><br/>{SAMPLE_SIZE_NOTICE}</>}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidthPx}px` }}>
|
||||
<thead>
|
||||
{hasGroupHeader ? (
|
||||
<tr className="lg-table__group">
|
||||
{groupCells.map((g, idx) => (
|
||||
<th
|
||||
key={`${g.kind}-${idx}`}
|
||||
className={cn(g.className)}
|
||||
colSpan={g.colSpan}
|
||||
rowSpan={g.rowSpan}
|
||||
>
|
||||
{g.label}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
) : null}
|
||||
|
||||
<tr className={hasGroupHeader ? "lg-table__subhead" : undefined}>
|
||||
{columns
|
||||
.filter((c) => (c.headRowSpan ?? 1) !== 2)
|
||||
.map((c) => (
|
||||
<th key={String(c.key)} className={cn(c.headerClassName)} style={c.width ? { width: c.width } : undefined}>
|
||||
{c.header}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((row, ri) => {
|
||||
const sampleRow = isSampleRow(row.rowLabel as string ?? "");
|
||||
const isSignRow = signStyle?.rowSelector ? signStyle.rowSelector(row, ri) : false;
|
||||
|
||||
return (
|
||||
<tr key={ri}>
|
||||
{columns.map((c) => {
|
||||
// 머지-차트 셀
|
||||
if (c.key === chartCell.key && ri >= start) {
|
||||
if (ri > start) return null;
|
||||
return (
|
||||
<td
|
||||
key={String(c.key)}
|
||||
rowSpan={rowSpan}
|
||||
className={cn(c.cellClassName, chartCell.className)}
|
||||
style={{ padding: 0, verticalAlign: "middle" }}
|
||||
>
|
||||
{chart}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
// 일반 셀
|
||||
const raw = c.key in row ? row[c.key] : undefined;
|
||||
const content = c.render
|
||||
? c.render(row)
|
||||
: raw === null || raw === undefined
|
||||
? ""
|
||||
: typeof raw === "string" || typeof raw === "number"
|
||||
? raw
|
||||
: String(raw);
|
||||
|
||||
|
||||
const shouldSign = isSignRow && (signStyle?.keys ? signStyle.keys.includes(c.key) : !!c.numeric);
|
||||
|
||||
if (c.rowHead) {
|
||||
return (
|
||||
<th
|
||||
key={String(c.key)}
|
||||
scope="row"
|
||||
className={cn("lg-table__rowhead", c.cellClassName)}
|
||||
style={c.width ? { width: c.width } : undefined}
|
||||
>
|
||||
{content}
|
||||
</th>
|
||||
);
|
||||
}
|
||||
|
||||
if (!shouldSign) {
|
||||
const numContent = c.numeric ? parseNumber(content) ?? 0 : 0;
|
||||
const isStar = starUnder30Sample && sampleRow && numContent < 30;
|
||||
|
||||
return (
|
||||
<td
|
||||
key={String(c.key)}
|
||||
className={cn(c.cellClassName)}
|
||||
style={c.width ? { width: c.width } : undefined}
|
||||
>
|
||||
{isStar?`${content}*`: roundNumber(parseNumber(content))}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
const n = c.numeric ? roundNumber(parseNumber(raw)) : null;
|
||||
// sign row: 값이 없으면 "-" 유지
|
||||
if (n === null) {
|
||||
return (
|
||||
<td key={String(c.key)} className={cn(c.cellClassName)}>
|
||||
-
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const cls = n > 0 ? posCls : n < 0 ? negCls : zeroCls;
|
||||
const txt = renderSigned(n, { display, showPlus, arrowWithNumber, zeroText });
|
||||
|
||||
return (
|
||||
<td key={String(c.key)} className={cn(c.cellClassName, cls)}>
|
||||
{txt}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<BaseDataTable
|
||||
columns={columns}
|
||||
rows={rows}
|
||||
title={title}
|
||||
wrapperClassName={wrapperClassName}
|
||||
tableClassName={tableClassName}
|
||||
minWidth={minWidth ?? minWidthPx}
|
||||
signStyle={signStyle}
|
||||
signStyleFallback={{
|
||||
positiveTextClassName: "lg-num-pos",
|
||||
negativeTextClassName: "lg-num-neg",
|
||||
zeroTextClassName: "",
|
||||
arrowWithNumber: true,
|
||||
}}
|
||||
starUnder30Sample={starUnder30Sample}
|
||||
unitText={unitText}
|
||||
mergedCell={{
|
||||
key: chartCell.key,
|
||||
content: chart,
|
||||
startRowIndex: chartCell.startRowIndex,
|
||||
rowSpan: chartCell.rowSpan,
|
||||
className: chartCell.className,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,172 +3,134 @@
|
||||
import React, { useMemo } from "react";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
||||
import SectionTitle from "../SectionTitle";
|
||||
import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
||||
import {UNIT_TEXT} from "@/const/lg-common.const";
|
||||
import {isCagrRow, roundNumber} from "@/lib/lg-dashboard";
|
||||
import { UNIT_TEXT } from "@/const/lg-common.const";
|
||||
import { isCagrRow } from "@/lib/lg-dashboard";
|
||||
import BaseTableHeader from "../table/core/BaseTableHeader";
|
||||
import { formatSignedValue, getSignTextClass, resolveSignStyle, toRoundedNumber } from "../table/core/table-core.helpers";
|
||||
|
||||
export type TopChartTableProps = {
|
||||
title?: string;
|
||||
categories: string[];
|
||||
rows: TopChartTableRow[];
|
||||
/** 테이블 상단(헤더 1행) 콜스팬 셀 내부에 렌더될 차트 */
|
||||
chart?:
|
||||
| React.ReactNode
|
||||
| ((ctx: {
|
||||
/** 테이블 컬럼(=x축) 기준으로 구성된 ChartData */
|
||||
chartData: ChartData;
|
||||
}) => React.ReactNode);
|
||||
/** chart 셀 높이(px) */
|
||||
chartHeight?: number;
|
||||
/** 가로 스크롤 min-width */
|
||||
| React.ReactNode
|
||||
| ((ctx: {
|
||||
chartData: ChartData;
|
||||
}) => React.ReactNode);
|
||||
chartHeight?: number | string;
|
||||
minWidthPx?: number;
|
||||
minWidth?: number;
|
||||
tableClassName?: string;
|
||||
wrapperClassName?: string;
|
||||
/** +/-/0 색상/표기 옵션(선택) */
|
||||
signStyle?: SignStyle<TopChartTableRow>;
|
||||
fillCAGR?: boolean;
|
||||
unitText?: string;
|
||||
};
|
||||
// toNumber는 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
function formatSignedNumber(n: number, zeroText: string, showPlus: boolean) {
|
||||
if (n === 0) return zeroText;
|
||||
if (n > 0) return showPlus ? `+${n}` : `${n}`;
|
||||
return `${n}`;
|
||||
}
|
||||
export default function TopChartTable({
|
||||
title,
|
||||
rows,
|
||||
categories,
|
||||
chart,
|
||||
chartHeight = CHART_HEIGHTS.DEFAULT,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD,
|
||||
minWidth,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
fillCAGR = false,
|
||||
unitText = UNIT_TEXT,
|
||||
}: TopChartTableProps) {
|
||||
const resolvedSignStyle = resolveSignStyle(signStyle, {
|
||||
positiveTextClassName: "lg-num-pos",
|
||||
negativeTextClassName: "lg-num-neg",
|
||||
zeroTextClassName: "",
|
||||
arrowWithNumber: true,
|
||||
});
|
||||
|
||||
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,
|
||||
rows,
|
||||
categories,
|
||||
chart,
|
||||
chartHeight = CHART_HEIGHTS.DEFAULT,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
fillCAGR = false,
|
||||
unitText = UNIT_TEXT
|
||||
} = 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 ?? "-";
|
||||
|
||||
// 테이블 컬럼(=categories)을 x축으로 사용하는 chartData를 항상 제공
|
||||
const chartData: ChartData = useMemo(
|
||||
() => ({
|
||||
categories,
|
||||
series: rows.map((r) => ({
|
||||
name: r.label,
|
||||
data: r.values.map((v) => parseNumber(v) ?? null),
|
||||
})),
|
||||
}),
|
||||
[categories, rows]
|
||||
() => ({
|
||||
categories,
|
||||
series: rows.map((row) => ({
|
||||
name: row.label,
|
||||
data: row.values.map((value) => parseNumber(value) ?? null),
|
||||
})),
|
||||
}),
|
||||
[categories, rows]
|
||||
);
|
||||
|
||||
const chartNode = useMemo(() => {
|
||||
if (!chart) return null;
|
||||
if (!chart) {
|
||||
return null;
|
||||
}
|
||||
return typeof chart === "function" ? chart({ chartData }) : chart;
|
||||
}, [chart, chartData, categories, rows]);
|
||||
}, [chart, chartData]);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
{title ? <SectionTitle title={title} /> : <div />}
|
||||
{unitText ? <div className="text-gray-500 text-xs font-semibold">{unitText}</div> : null}
|
||||
</div>
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
<BaseTableHeader title={title} unitText={unitText} />
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table
|
||||
className={cn("lg-table", tableClassName)}
|
||||
style={{ minWidth: `${minWidthPx}px` }}
|
||||
>
|
||||
<thead>
|
||||
<div className="overflow-x-auto">
|
||||
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidth ?? minWidthPx}px` }}>
|
||||
<thead>
|
||||
<tr>
|
||||
{/* 좌상단: rowhead 헤더(필요 시 표시). HTML과 동일하게 2행 rowspan */}
|
||||
<th rowSpan={2} className="lg-table__sep-r w-31" style={{ whiteSpace: "nowrap" }}>
|
||||
|
||||
</th>
|
||||
<th rowSpan={2} className="lg-table__sep-r w-31" style={{ whiteSpace: "nowrap" }} />
|
||||
<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>
|
||||
{categories.map((category) => (
|
||||
<th key={category} className="content-text-xs">
|
||||
{category}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((r, ri) => {
|
||||
const isSignRow = !!(signStyle?.rowSelector && signStyle.rowSelector(r, ri));
|
||||
const cagrRow = isCagrRow(r.label as string ?? "");
|
||||
const total = parseNumber(r.total);
|
||||
<tbody>
|
||||
{rows.map((row, rowIndex) => {
|
||||
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
||||
const cagrRow = isCagrRow(row.label ?? "");
|
||||
const rowTotal = parseNumber(row.total);
|
||||
|
||||
return (
|
||||
<tr key={`${r.label}-${ri}`}>
|
||||
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
||||
{r.label}
|
||||
</th>
|
||||
<tr key={`${row.label}-${rowIndex}`}>
|
||||
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
||||
{row.label}
|
||||
</th>
|
||||
|
||||
{categories.map((_, ci) => {
|
||||
const raw = r.values?.[ci];
|
||||
const n = roundNumber(parseNumber(raw));
|
||||
{categories.map((_, categoryIndex) => {
|
||||
const rawValue = row.values?.[categoryIndex];
|
||||
const roundedValue = toRoundedNumber(rawValue);
|
||||
|
||||
// 일반 행
|
||||
if (!isSignRow) {
|
||||
if (n === null) return <td key={ci}>-</td>;
|
||||
|
||||
const isFill = fillCAGR && cagrRow && total && n > total + 5;
|
||||
const txt = n.toLocaleString();
|
||||
|
||||
return <td key={ci} className={cn(isFill? "bg-red-200" :"")}>{txt}</td>;
|
||||
if (!signRow) {
|
||||
if (roundedValue === null) {
|
||||
return <td key={categoryIndex}>-</td>;
|
||||
}
|
||||
|
||||
// sign row
|
||||
if (n === null) return <td key={ci}>-</td>;
|
||||
if (n === 0) return <td key={ci}>{zeroText}</td>;
|
||||
const shouldFillCell = fillCAGR && cagrRow && rowTotal !== null && roundedValue > rowTotal + 5;
|
||||
return <td key={categoryIndex} className={cn(shouldFillCell ? "bg-red-200" : "")}>{roundedValue.toLocaleString()}</td>;
|
||||
}
|
||||
|
||||
const cls = n > 0 ? posCls : negCls;
|
||||
if (roundedValue === null) {
|
||||
return <td key={categoryIndex}>-</td>;
|
||||
}
|
||||
|
||||
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>
|
||||
return (
|
||||
<td key={categoryIndex} className={getSignTextClass(roundedValue, resolvedSignStyle)}>
|
||||
{formatSignedValue(roundedValue, resolvedSignStyle)}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user