"use client"; import React, { useMemo } from "react"; import { cn, parseNumber } from "@/lib/utils"; import type { DataColumn, SignStyle } from "../types"; import SectionTitle from "../SectionTitle"; 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"; type MergedChartCell = { /** 차트를 삽입할 컬럼 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; }; type Props> = { title?: string; columns: DataColumn[]; rows: T[]; /** 머지된 셀 안에 렌더할 차트 */ chart: React.ReactNode; /** 어떤 컬럼을 머지-차트 셀로 교체할지 */ chartCell: MergedChartCell; /** +/-/0 표기/색상 옵션(선택) */ signStyle?: SignStyle; wrapperClassName?: string; tableClassName?: string; minWidthPx?: 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>({ title, columns, rows, chart, chartCell, signStyle, wrapperClassName, tableClassName, minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.COMPACT, starUnder30Sample = true, unitText = UNIT_TEXT, }: Props) { 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 (
{ title && } { (unitText || starUnder30Sample) &&
{unitText} {starUnder30Sample && <>
{SAMPLE_SIZE_NOTICE}}
}
{hasGroupHeader ? ( {groupCells.map((g, idx) => ( ))} ) : null} {columns .filter((c) => (c.headRowSpan ?? 1) !== 2) .map((c) => ( ))} {rows.map((row, ri) => { const sampleRow = isSampleRow(row.rowLabel as string ?? ""); const isSignRow = signStyle?.rowSelector ? signStyle.rowSelector(row, ri) : false; return ( {columns.map((c) => { // 머지-차트 셀 if (c.key === chartCell.key && ri >= start) { if (ri > start) return null; return ( ); } // 일반 셀 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 ( ); } if (!shouldSign) { const numContent = c.numeric ? parseNumber(content) ?? 0 : 0; const isStar = starUnder30Sample && sampleRow && numContent < 30; return ( ); } const n = c.numeric ? roundNumber(parseNumber(raw)) : null; // sign row: 값이 없으면 "-" 유지 if (n === null) { return ( ); } const cls = n > 0 ? posCls : n < 0 ? negCls : zeroCls; const txt = renderSigned(n, { display, showPlus, arrowWithNumber, zeroText }); return ( ); })} ); })}
{g.label}
{c.header}
{chart} {content} {isStar?`${content}*`: roundNumber(parseNumber(content))} - {txt}
); }