260326_02_ENH: 테이블 4종 통합 및 BaseTable 생성
This commit is contained in:
@@ -1,21 +1,15 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useMemo } from "react";
|
import type React 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 { TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
||||||
import {isSampleRow, roundNumber} from "@/lib/lg-dashboard";
|
import { UNIT_TEXT } from "@/const/lg-common.const";
|
||||||
import {SAMPLE_SIZE_NOTICE, UNIT_TEXT, VIEW_DECIMAL_POINT} from "@/const/lg-common.const";
|
import type { DataColumn, SignStyle } from "../types";
|
||||||
|
import BaseDataTable from "../table/BaseDataTable";
|
||||||
|
|
||||||
type MergedChartCell<T> = {
|
type MergedChartCell<T> = {
|
||||||
/** 차트를 삽입할 컬럼 key(해당 컬럼 td는 startRow에서만 렌더되고 rowSpan 적용) */
|
|
||||||
key: keyof T;
|
key: keyof T;
|
||||||
/** rowSpan 시작 row index (기본 0) */
|
|
||||||
startRowIndex?: number;
|
startRowIndex?: number;
|
||||||
/** rowSpan 수 (기본: rows.length - startRowIndex) */
|
|
||||||
rowSpan?: number;
|
rowSpan?: number;
|
||||||
/** chart td 클래스 (예: lg-table__sep-r) */
|
|
||||||
className?: string;
|
className?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -23,42 +17,17 @@ type Props<T extends Record<string, unknown>> = {
|
|||||||
title?: string;
|
title?: string;
|
||||||
columns: DataColumn<T>[];
|
columns: DataColumn<T>[];
|
||||||
rows: T[];
|
rows: T[];
|
||||||
/** 머지된 셀 안에 렌더할 차트 */
|
|
||||||
chart: React.ReactNode;
|
chart: React.ReactNode;
|
||||||
/** 어떤 컬럼을 머지-차트 셀로 교체할지 */
|
|
||||||
chartCell: MergedChartCell<T>;
|
chartCell: MergedChartCell<T>;
|
||||||
|
|
||||||
/** +/-/0 표기/색상 옵션(선택) */
|
|
||||||
signStyle?: SignStyle<T>;
|
signStyle?: SignStyle<T>;
|
||||||
|
|
||||||
wrapperClassName?: string;
|
wrapperClassName?: string;
|
||||||
tableClassName?: string;
|
tableClassName?: string;
|
||||||
minWidthPx?: number;
|
minWidthPx?: number;
|
||||||
|
minWidth?: number;
|
||||||
starUnder30Sample?: boolean;
|
starUnder30Sample?: boolean;
|
||||||
unitText?: string;
|
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>>({
|
export default function LeftChartTable<T extends Record<string, unknown>>({
|
||||||
title,
|
title,
|
||||||
columns,
|
columns,
|
||||||
@@ -69,207 +38,34 @@ export default function LeftChartTable<T extends Record<string, unknown>>({
|
|||||||
wrapperClassName,
|
wrapperClassName,
|
||||||
tableClassName,
|
tableClassName,
|
||||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.COMPACT,
|
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.COMPACT,
|
||||||
|
minWidth,
|
||||||
starUnder30Sample = true,
|
starUnder30Sample = true,
|
||||||
unitText = UNIT_TEXT,
|
unitText = UNIT_TEXT,
|
||||||
}: Props<T>) {
|
}: 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 (
|
return (
|
||||||
<div className={cn("w-full", wrapperClassName)}>
|
<BaseDataTable
|
||||||
<div className="mb-2 flex items-center justify-between">
|
columns={columns}
|
||||||
{ title && <SectionTitle title={title} />}
|
rows={rows}
|
||||||
{
|
title={title}
|
||||||
(unitText || starUnder30Sample) &&
|
wrapperClassName={wrapperClassName}
|
||||||
<div className="ml-auto text-gray-500 content-text-xs font-semibold text-right">
|
tableClassName={tableClassName}
|
||||||
{unitText}
|
minWidth={minWidth ?? minWidthPx}
|
||||||
{starUnder30Sample && <><br/>{SAMPLE_SIZE_NOTICE}</>}
|
signStyle={signStyle}
|
||||||
</div>
|
signStyleFallback={{
|
||||||
}
|
positiveTextClassName: "lg-num-pos",
|
||||||
</div>
|
negativeTextClassName: "lg-num-neg",
|
||||||
|
zeroTextClassName: "",
|
||||||
<div className="overflow-x-auto">
|
arrowWithNumber: true,
|
||||||
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidthPx}px` }}>
|
}}
|
||||||
<thead>
|
starUnder30Sample={starUnder30Sample}
|
||||||
{hasGroupHeader ? (
|
unitText={unitText}
|
||||||
<tr className="lg-table__group">
|
mergedCell={{
|
||||||
{groupCells.map((g, idx) => (
|
key: chartCell.key,
|
||||||
<th
|
content: chart,
|
||||||
key={`${g.kind}-${idx}`}
|
startRowIndex: chartCell.startRowIndex,
|
||||||
className={cn(g.className)}
|
rowSpan: chartCell.rowSpan,
|
||||||
colSpan={g.colSpan}
|
className: chartCell.className,
|
||||||
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>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,172 +3,134 @@
|
|||||||
import React, { useMemo } from "react";
|
import React, { useMemo } from "react";
|
||||||
import { cn, parseNumber } from "@/lib/utils";
|
import { cn, parseNumber } from "@/lib/utils";
|
||||||
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
||||||
import SectionTitle from "../SectionTitle";
|
|
||||||
import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
||||||
import {UNIT_TEXT} from "@/const/lg-common.const";
|
import { UNIT_TEXT } from "@/const/lg-common.const";
|
||||||
import {isCagrRow, roundNumber} from "@/lib/lg-dashboard";
|
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 = {
|
export type TopChartTableProps = {
|
||||||
title?: string;
|
title?: string;
|
||||||
categories: string[];
|
categories: string[];
|
||||||
rows: TopChartTableRow[];
|
rows: TopChartTableRow[];
|
||||||
/** 테이블 상단(헤더 1행) 콜스팬 셀 내부에 렌더될 차트 */
|
|
||||||
chart?:
|
chart?:
|
||||||
| React.ReactNode
|
| React.ReactNode
|
||||||
| ((ctx: {
|
| ((ctx: {
|
||||||
/** 테이블 컬럼(=x축) 기준으로 구성된 ChartData */
|
chartData: ChartData;
|
||||||
chartData: ChartData;
|
}) => React.ReactNode);
|
||||||
}) => React.ReactNode);
|
chartHeight?: number | string;
|
||||||
/** chart 셀 높이(px) */
|
|
||||||
chartHeight?: number;
|
|
||||||
/** 가로 스크롤 min-width */
|
|
||||||
minWidthPx?: number;
|
minWidthPx?: number;
|
||||||
|
minWidth?: number;
|
||||||
tableClassName?: string;
|
tableClassName?: string;
|
||||||
wrapperClassName?: string;
|
wrapperClassName?: string;
|
||||||
/** +/-/0 색상/표기 옵션(선택) */
|
|
||||||
signStyle?: SignStyle<TopChartTableRow>;
|
signStyle?: SignStyle<TopChartTableRow>;
|
||||||
fillCAGR?: boolean;
|
fillCAGR?: boolean;
|
||||||
unitText?: string;
|
unitText?: string;
|
||||||
};
|
};
|
||||||
// toNumber는 utils.ts의 parseNumber로 통합됨
|
|
||||||
|
|
||||||
function formatSignedNumber(n: number, zeroText: string, showPlus: boolean) {
|
export default function TopChartTable({
|
||||||
if (n === 0) return zeroText;
|
title,
|
||||||
if (n > 0) return showPlus ? `+${n}` : `${n}`;
|
rows,
|
||||||
return `${n}`;
|
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(
|
const chartData: ChartData = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
categories,
|
categories,
|
||||||
series: rows.map((r) => ({
|
series: rows.map((row) => ({
|
||||||
name: r.label,
|
name: row.label,
|
||||||
data: r.values.map((v) => parseNumber(v) ?? null),
|
data: row.values.map((value) => parseNumber(value) ?? null),
|
||||||
})),
|
})),
|
||||||
}),
|
}),
|
||||||
[categories, rows]
|
[categories, rows]
|
||||||
);
|
);
|
||||||
|
|
||||||
const chartNode = useMemo(() => {
|
const chartNode = useMemo(() => {
|
||||||
if (!chart) return null;
|
if (!chart) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return typeof chart === "function" ? chart({ chartData }) : chart;
|
return typeof chart === "function" ? chart({ chartData }) : chart;
|
||||||
}, [chart, chartData, categories, rows]);
|
}, [chart, chartData]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn("w-full", wrapperClassName)}>
|
<div className={cn("w-full", wrapperClassName)}>
|
||||||
<div className="mb-2 flex items-center justify-between">
|
<BaseTableHeader title={title} unitText={unitText} />
|
||||||
{title ? <SectionTitle title={title} /> : <div />}
|
|
||||||
{unitText ? <div className="text-gray-500 text-xs font-semibold">{unitText}</div> : null}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="overflow-x-auto">
|
<div className="overflow-x-auto">
|
||||||
<table
|
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidth ?? minWidthPx}px` }}>
|
||||||
className={cn("lg-table", tableClassName)}
|
<thead>
|
||||||
style={{ minWidth: `${minWidthPx}px` }}
|
|
||||||
>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
<tr>
|
||||||
{/* 좌상단: rowhead 헤더(필요 시 표시). HTML과 동일하게 2행 rowspan */}
|
<th rowSpan={2} className="lg-table__sep-r w-31" style={{ whiteSpace: "nowrap" }} />
|
||||||
<th rowSpan={2} className="lg-table__sep-r w-31" style={{ whiteSpace: "nowrap" }}>
|
|
||||||
|
|
||||||
</th>
|
|
||||||
<td colSpan={categories.length} style={{ padding: 0, height: chartNode ? chartHeight : 0 }}>
|
<td colSpan={categories.length} style={{ padding: 0, height: chartNode ? chartHeight : 0 }}>
|
||||||
{chartNode ? <div style={{ width: "100%", height: "100%" }}>{chartNode}</div> : null}
|
{chartNode ? <div style={{ width: "100%", height: "100%" }}>{chartNode}</div> : null}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr className="lg-table__subhead">
|
<tr className="lg-table__subhead">
|
||||||
{categories.map((c) => (
|
{categories.map((category) => (
|
||||||
<th key={c} className={"content-text-xs"}>{c}</th>
|
<th key={category} className="content-text-xs">
|
||||||
|
{category}
|
||||||
|
</th>
|
||||||
))}
|
))}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tbody>
|
<tbody>
|
||||||
{rows.map((r, ri) => {
|
{rows.map((row, rowIndex) => {
|
||||||
const isSignRow = !!(signStyle?.rowSelector && signStyle.rowSelector(r, ri));
|
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
||||||
const cagrRow = isCagrRow(r.label as string ?? "");
|
const cagrRow = isCagrRow(row.label ?? "");
|
||||||
const total = parseNumber(r.total);
|
const rowTotal = parseNumber(row.total);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<tr key={`${r.label}-${ri}`}>
|
<tr key={`${row.label}-${rowIndex}`}>
|
||||||
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
||||||
{r.label}
|
{row.label}
|
||||||
</th>
|
</th>
|
||||||
|
|
||||||
{categories.map((_, ci) => {
|
{categories.map((_, categoryIndex) => {
|
||||||
const raw = r.values?.[ci];
|
const rawValue = row.values?.[categoryIndex];
|
||||||
const n = roundNumber(parseNumber(raw));
|
const roundedValue = toRoundedNumber(rawValue);
|
||||||
|
|
||||||
// 일반 행
|
if (!signRow) {
|
||||||
if (!isSignRow) {
|
if (roundedValue === null) {
|
||||||
if (n === null) return <td key={ci}>-</td>;
|
return <td key={categoryIndex}>-</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>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign row
|
const shouldFillCell = fillCAGR && cagrRow && rowTotal !== null && roundedValue > rowTotal + 5;
|
||||||
if (n === null) return <td key={ci}>-</td>;
|
return <td key={categoryIndex} className={cn(shouldFillCell ? "bg-red-200" : "")}>{roundedValue.toLocaleString()}</td>;
|
||||||
if (n === 0) return <td key={ci}>{zeroText}</td>;
|
}
|
||||||
|
|
||||||
const cls = n > 0 ? posCls : negCls;
|
if (roundedValue === null) {
|
||||||
|
return <td key={categoryIndex}>-</td>;
|
||||||
|
}
|
||||||
|
|
||||||
if (display === "arrow") {
|
return (
|
||||||
const arrow = n > 0 ? "▲" : "▼";
|
<td key={categoryIndex} className={getSignTextClass(roundedValue, resolvedSignStyle)}>
|
||||||
const num = Math.abs(n);
|
{formatSignedValue(roundedValue, resolvedSignStyle)}
|
||||||
return (
|
</td>
|
||||||
<td key={ci} className={cls}>
|
);
|
||||||
{arrow}
|
})}
|
||||||
{arrowWithNumber ? ` ${num}` : ""}
|
</tr>
|
||||||
</td>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<td key={ci} className={cls}>
|
|
||||||
{formatSignedNumber(n, zeroText, showPlus)}
|
|
||||||
</td>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</tr>
|
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
152
src/components/lge-dashboard/table/BaseDataTable.tsx
Normal file
152
src/components/lge-dashboard/table/BaseDataTable.tsx
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import type React from "react";
|
||||||
|
import { cn, parseNumber } from "@/lib/utils";
|
||||||
|
import { isSampleRow } from "@/lib/lg-dashboard";
|
||||||
|
import type { DataColumn, SignStyle } from "../types";
|
||||||
|
import BaseTableRenderer from "./core/BaseTableRenderer";
|
||||||
|
import {
|
||||||
|
appendCellMarker,
|
||||||
|
formatSignedValue,
|
||||||
|
getColumnAlign,
|
||||||
|
getSignCellBackground,
|
||||||
|
getSignTextClass,
|
||||||
|
resolveSignStyle,
|
||||||
|
shouldApplySignToColumn,
|
||||||
|
toRoundedNumber,
|
||||||
|
toTableCellContent,
|
||||||
|
type ResolvedSignStyle,
|
||||||
|
} from "./core/table-core.helpers";
|
||||||
|
|
||||||
|
export type MergedTableCell<T> = {
|
||||||
|
key: keyof T;
|
||||||
|
content: React.ReactNode;
|
||||||
|
startRowIndex?: number;
|
||||||
|
rowSpan?: number;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Props<T extends Record<string, unknown>> = {
|
||||||
|
columns: DataColumn<T>[];
|
||||||
|
rows: T[];
|
||||||
|
title?: string;
|
||||||
|
wrapperClassName?: string;
|
||||||
|
tableClassName?: string;
|
||||||
|
minWidth?: number;
|
||||||
|
signStyle?: SignStyle<T>;
|
||||||
|
signStyleFallback?: Partial<ResolvedSignStyle>;
|
||||||
|
fillGreaterThan5?: boolean;
|
||||||
|
starUnder30Sample?: boolean;
|
||||||
|
unitText?: string;
|
||||||
|
mergedCell?: MergedTableCell<T>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function BaseDataTable<T extends Record<string, unknown>>({
|
||||||
|
columns,
|
||||||
|
rows,
|
||||||
|
title,
|
||||||
|
wrapperClassName,
|
||||||
|
tableClassName,
|
||||||
|
minWidth,
|
||||||
|
signStyle,
|
||||||
|
signStyleFallback,
|
||||||
|
fillGreaterThan5 = false,
|
||||||
|
starUnder30Sample = false,
|
||||||
|
unitText,
|
||||||
|
mergedCell,
|
||||||
|
}: Props<T>) {
|
||||||
|
const resolvedSignStyle = resolveSignStyle(signStyle, signStyleFallback);
|
||||||
|
const mergedCellStart = mergedCell?.startRowIndex ?? 0;
|
||||||
|
const mergedCellSpan = mergedCell?.rowSpan ?? Math.max(0, rows.length - mergedCellStart);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<BaseTableRenderer
|
||||||
|
columns={columns}
|
||||||
|
title={title}
|
||||||
|
unitText={unitText}
|
||||||
|
showSampleNotice={starUnder30Sample}
|
||||||
|
wrapperClassName={wrapperClassName}
|
||||||
|
tableClassName={tableClassName}
|
||||||
|
minWidth={minWidth}
|
||||||
|
>
|
||||||
|
{rows.map((row, rowIndex) => {
|
||||||
|
const sampleRow = isSampleRow((row.rowLabel as string) ?? "");
|
||||||
|
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
||||||
|
const rowTotal = parseNumber(row.total);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<tr key={rowIndex}>
|
||||||
|
{columns.map((column) => {
|
||||||
|
if (mergedCell && column.key === mergedCell.key && rowIndex >= mergedCellStart) {
|
||||||
|
if (rowIndex > mergedCellStart) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
key={String(column.key)}
|
||||||
|
rowSpan={mergedCellSpan}
|
||||||
|
className={cn(column.cellClassName, mergedCell.className)}
|
||||||
|
style={{ padding: 0, verticalAlign: "middle" }}
|
||||||
|
>
|
||||||
|
{mergedCell.content}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawValue = row[column.key];
|
||||||
|
const align = getColumnAlign(column);
|
||||||
|
const defaultContent = column.render ? column.render(row) : toTableCellContent(rawValue);
|
||||||
|
const numericValue = column.numeric ? parseNumber(rawValue ?? defaultContent) ?? 0 : 0;
|
||||||
|
const applySign = signRow && shouldApplySignToColumn(column, signStyle);
|
||||||
|
|
||||||
|
if (column.rowHead) {
|
||||||
|
return (
|
||||||
|
<th
|
||||||
|
key={String(column.key)}
|
||||||
|
scope="row"
|
||||||
|
className={cn("lg-table__rowhead", column.cellClassName)}
|
||||||
|
style={{ textAlign: align }}
|
||||||
|
>
|
||||||
|
{defaultContent}
|
||||||
|
</th>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (applySign) {
|
||||||
|
const roundedValue = toRoundedNumber(rawValue);
|
||||||
|
const displayText = roundedValue === null ? "-" : formatSignedValue(roundedValue, resolvedSignStyle);
|
||||||
|
const cellStyle: React.CSSProperties =
|
||||||
|
resolvedSignStyle.paint === "cell"
|
||||||
|
? { textAlign: align, background: getSignCellBackground(roundedValue ?? 0, resolvedSignStyle) }
|
||||||
|
: { textAlign: align };
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td key={String(column.key)} className={column.cellClassName} style={cellStyle}>
|
||||||
|
<span className={cn(roundedValue === null ? "text-gray-500" : getSignTextClass(roundedValue, resolvedSignStyle))}>
|
||||||
|
{displayText}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldFillCell = fillGreaterThan5 && rowTotal !== null && numericValue > rowTotal + 5;
|
||||||
|
const shouldMarkSample = starUnder30Sample && sampleRow && numericValue < 30;
|
||||||
|
const displayContent = shouldMarkSample ? appendCellMarker(defaultContent, "*") : defaultContent;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
key={String(column.key)}
|
||||||
|
className={cn(column.cellClassName, shouldFillCell ? "bg-red-200" : "")}
|
||||||
|
style={{ textAlign: align }}
|
||||||
|
>
|
||||||
|
{displayContent}
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</BaseTableRenderer>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useMemo } from "react";
|
import type { DataColumn, SignStyle } from "../types";
|
||||||
import type React from "react";
|
import { UNIT_TEXT } from "@/const/lg-common.const";
|
||||||
import { cn, parseNumber } from "@/lib/utils";
|
import BaseDataTable from "./BaseDataTable";
|
||||||
import type { DataColumn, SignDisplayMode, SignStyle } from "../types";
|
|
||||||
import SectionTitle from "../SectionTitle";
|
|
||||||
import {isCagrRow, isSampleRow, roundNumber} from "@/lib/lg-dashboard";
|
|
||||||
import {SAMPLE_SIZE_NOTICE, UNIT_TEXT} from "@/const/lg-common.const";
|
|
||||||
|
|
||||||
type Props<T extends Record<string, unknown>> = {
|
type Props<T extends Record<string, unknown>> = {
|
||||||
columns: DataColumn<T>[];
|
columns: DataColumn<T>[];
|
||||||
rows: T[];
|
rows: T[];
|
||||||
title?: string;
|
title?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
|
tableClassName?: string;
|
||||||
wrapperClassName?: string;
|
wrapperClassName?: string;
|
||||||
signStyle?: SignStyle<T>;
|
signStyle?: SignStyle<T>;
|
||||||
fillGreaterThan5?: boolean;
|
fillGreaterThan5?: boolean;
|
||||||
@@ -20,253 +17,31 @@ type Props<T extends Record<string, unknown>> = {
|
|||||||
unitText?: string;
|
unitText?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// parseSignedNumber는 utils.ts의 parseNumber로 통합됨
|
|
||||||
|
|
||||||
const defaultFmt = (v: number) =>
|
|
||||||
Number.isInteger(v) ? String(roundNumber(v)) : v.toLocaleString();
|
|
||||||
|
|
||||||
const signText = (
|
|
||||||
v: number,
|
|
||||||
display: SignDisplayMode,
|
|
||||||
showPlus: boolean,
|
|
||||||
arrowWithNumber: boolean,
|
|
||||||
fmt: (n: number) => string,
|
|
||||||
zeroText: string
|
|
||||||
) => {
|
|
||||||
if (display === "arrow") {
|
|
||||||
if (v > 0) return arrowWithNumber ? `▲ ${fmt(Math.abs(v))}` : "▲";
|
|
||||||
if (v < 0) return arrowWithNumber ? `▼ ${fmt(Math.abs(v))}` : "▼";
|
|
||||||
return zeroText;
|
|
||||||
}
|
|
||||||
|
|
||||||
// number
|
|
||||||
if (v > 0) return showPlus ? `+${fmt(v)}` : fmt(v);
|
|
||||||
if (v < 0) return fmt(v); // 음수는 기본적으로 - 포함
|
|
||||||
return zeroText;
|
|
||||||
};
|
|
||||||
|
|
||||||
const toCellContent = (raw: unknown): React.ReactNode => {
|
|
||||||
if (raw == null) return "";
|
|
||||||
if (typeof raw === "string"){
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
if (trimmed === "") return raw;
|
|
||||||
|
|
||||||
const num = Number(trimmed);
|
|
||||||
return Number.isNaN(num) ? raw : roundNumber(num);
|
|
||||||
}
|
|
||||||
if (typeof raw === "number") return String(roundNumber(raw));
|
|
||||||
return String(raw);
|
|
||||||
};
|
|
||||||
|
|
||||||
const DataTable = <T extends Record<string, unknown>>({
|
const DataTable = <T extends Record<string, unknown>>({
|
||||||
columns,
|
columns,
|
||||||
rows,
|
rows,
|
||||||
title,
|
title,
|
||||||
className,
|
className,
|
||||||
wrapperClassName,
|
tableClassName,
|
||||||
signStyle,
|
wrapperClassName,
|
||||||
fillGreaterThan5 = false,
|
signStyle,
|
||||||
starUnder30Sample = false,
|
fillGreaterThan5 = false,
|
||||||
unitText = UNIT_TEXT,
|
starUnder30Sample = false,
|
||||||
}: Props<T>) => {
|
unitText = UNIT_TEXT,
|
||||||
const hasGroup = columns.some((c) => !!c.group) || columns.some((c) => c.headRowSpan === 2);
|
}: Props<T>) => {
|
||||||
|
|
||||||
// group header(1행): rowspan=2 컬럼(th) + group label(colspan)
|
|
||||||
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, className: c.headerClassName });
|
|
||||||
i++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const g = c.group;
|
|
||||||
if (!g) {
|
|
||||||
// group 미지정: 1칸짜리 blank group 셀
|
|
||||||
res.push({ kind: "blank", label: "", colSpan: 1, rowSpan: 1, className: c.headerClassName });
|
|
||||||
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, className: c.headerClassName });
|
|
||||||
i = j;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}, [columns, hasGroup]);
|
|
||||||
|
|
||||||
const {
|
|
||||||
rowSelector,
|
|
||||||
keys,
|
|
||||||
paint = "text",
|
|
||||||
display = "number",
|
|
||||||
showPlus = true,
|
|
||||||
arrowWithNumber = false,
|
|
||||||
zeroText = "-",
|
|
||||||
valueFormatter,
|
|
||||||
positiveTextClassName = "text-primary font-semibold",
|
|
||||||
negativeTextClassName = "text-info font-semibold",
|
|
||||||
zeroTextClassName = "text-gray-600",
|
|
||||||
positiveCellBg = "hsl(var(--primary) / 0.08)",
|
|
||||||
negativeCellBg = "hsl(var(--info) / 0.08)",
|
|
||||||
zeroCellBg = "hsl(var(--lg-gray-200) / 0.6)",
|
|
||||||
} = signStyle ?? ({} as SignStyle<T>);
|
|
||||||
|
|
||||||
const fmt = valueFormatter ?? defaultFmt;
|
|
||||||
|
|
||||||
const isSignRow = (row: T, rowIndex: number) =>
|
|
||||||
!!signStyle && !!rowSelector?.(row, rowIndex);
|
|
||||||
|
|
||||||
const isTargetKey = (k: keyof T) => {
|
|
||||||
if (!signStyle) return false;
|
|
||||||
if (keys && keys.length) return keys.includes(k);
|
|
||||||
// keys.ts 미지정이면 numeric 컬럼 전체
|
|
||||||
const col = columns.find((c) => c.key === k);
|
|
||||||
return !!col?.numeric;
|
|
||||||
};
|
|
||||||
|
|
||||||
const signClass = (v: number) => {
|
|
||||||
if (v > 0) return positiveTextClassName;
|
|
||||||
if (v < 0) return negativeTextClassName;
|
|
||||||
return zeroTextClassName;
|
|
||||||
};
|
|
||||||
|
|
||||||
const signBg = (v: number) => {
|
|
||||||
if (v > 0) return positiveCellBg;
|
|
||||||
if (v < 0) return negativeCellBg;
|
|
||||||
return zeroCellBg;
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn("w-full overflow-x-auto", wrapperClassName)}>
|
<BaseDataTable
|
||||||
<div className="mb-2 flex items-center justify-between">
|
columns={columns}
|
||||||
{ title && <SectionTitle title={title} />}
|
rows={rows}
|
||||||
{
|
title={title}
|
||||||
(unitText || starUnder30Sample) &&
|
tableClassName={tableClassName ?? className}
|
||||||
<div className="ml-auto text-gray-500 content-text-xs font-semibold text-right">
|
wrapperClassName={wrapperClassName}
|
||||||
{unitText}
|
signStyle={signStyle}
|
||||||
{starUnder30Sample && <><br/>{SAMPLE_SIZE_NOTICE}</>}
|
fillGreaterThan5={fillGreaterThan5}
|
||||||
</div>
|
starUnder30Sample={starUnder30Sample}
|
||||||
}
|
unitText={unitText}
|
||||||
</div>
|
/>
|
||||||
<table className={cn("lg-table", className)}>
|
|
||||||
<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)} style={{ width: c.width }} className={c.headerClassName}>
|
|
||||||
{c.header}
|
|
||||||
</th>
|
|
||||||
))}
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
|
|
||||||
<tbody>
|
|
||||||
{rows.map((row, rIdx) => {
|
|
||||||
const sampleRow = isSampleRow(row.rowLabel as string ?? "");
|
|
||||||
const signRow = isSignRow(row, rIdx);
|
|
||||||
const total = parseNumber(row.total);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<tr key={rIdx}>
|
|
||||||
{columns.map((c) => {
|
|
||||||
const raw: unknown = c.key ? (row[c.key] as unknown) : undefined;
|
|
||||||
const align = c.align ?? (c.numeric ? "right" : "center");
|
|
||||||
|
|
||||||
// headRowSpan=2 컬럼은 thead 2행에서 제외되므로, tbody는 항상 렌더
|
|
||||||
|
|
||||||
// sign row + target column이면 색상/표기 적용
|
|
||||||
const applySign = signRow && isTargetKey(c.key) && !c.rowHead;
|
|
||||||
|
|
||||||
if (applySign) {
|
|
||||||
const n = roundNumber(parseNumber(raw));
|
|
||||||
const displayText =
|
|
||||||
n === null ? "-" : signText(n, display, showPlus, arrowWithNumber, fmt, zeroText);
|
|
||||||
|
|
||||||
const style: React.CSSProperties =
|
|
||||||
paint === "cell"
|
|
||||||
? { textAlign: align, background: signBg(n ?? 0) }
|
|
||||||
: { textAlign: align };
|
|
||||||
|
|
||||||
return (
|
|
||||||
<td key={String(c.key)} style={style} className={c.cellClassName}>
|
|
||||||
<span className={cn(n === null ? "text-gray-500" : signClass(n))}>
|
|
||||||
{displayText}
|
|
||||||
</span>
|
|
||||||
</td>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 일반 셀
|
|
||||||
const content: React.ReactNode = c.render
|
|
||||||
? c.render(row)
|
|
||||||
: toCellContent(raw);
|
|
||||||
|
|
||||||
if (c.rowHead) {
|
|
||||||
return (
|
|
||||||
<th
|
|
||||||
key={String(c.key)}
|
|
||||||
className={cn("lg-table__rowhead", c.cellClassName)}
|
|
||||||
style={{ textAlign: align }}
|
|
||||||
>
|
|
||||||
{content}
|
|
||||||
</th>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const numContent = c.numeric ? parseNumber(content) ?? 0 : 0;
|
|
||||||
const isFill = fillGreaterThan5 && total && numContent > total + 5;
|
|
||||||
const isStar = starUnder30Sample && sampleRow && numContent < 30;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<td key={String(c.key)} style={{ textAlign: align }} className={
|
|
||||||
cn(c.cellClassName, isFill? "bg-red-200" :"")
|
|
||||||
}>
|
|
||||||
{isStar?`${content}*`:content}
|
|
||||||
</td>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default DataTable;
|
export default DataTable;
|
||||||
|
|||||||
@@ -1,205 +1,113 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useMemo } from "react";
|
|
||||||
import { cn, parseNumber } from "@/lib/utils";
|
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>> = {
|
type Props<T extends Record<string, unknown>> = {
|
||||||
columns: GaugeColumn<T>[];
|
columns: GaugeColumn<T>[];
|
||||||
rows: T[];
|
rows: T[];
|
||||||
className?: string;
|
className?: string;
|
||||||
|
tableClassName?: string;
|
||||||
wrapperClassName?: string;
|
wrapperClassName?: string;
|
||||||
|
scale?: number;
|
||||||
/** value가 0~100이 아니라 0~1이면 scale=100 */
|
fillColor?: string;
|
||||||
scale?: number; // 기본 1
|
trackColor?: string;
|
||||||
|
cellHeight?: number;
|
||||||
/** 색상(기본: primary 채움 + gray 트랙) */
|
valueFormatter?: (value: number) => string;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// num은 utils.ts의 parseNumber로 통합됨
|
|
||||||
|
|
||||||
const GaugeTable = <T extends Record<string, unknown>>({
|
const GaugeTable = <T extends Record<string, unknown>>({
|
||||||
columns,
|
columns,
|
||||||
rows,
|
rows,
|
||||||
className,
|
className,
|
||||||
wrapperClassName,
|
tableClassName,
|
||||||
scale = 1,
|
wrapperClassName,
|
||||||
fillColor = "hsl(var(--lg-gray-200) / 0.85)",
|
scale = 1,
|
||||||
trackColor = "hsl(var(--lg-pink-300) / 0.2)",
|
fillColor = "hsl(var(--lg-gray-200) / 0.85)",
|
||||||
cellHeight = 26,
|
trackColor = "hsl(var(--lg-pink-300) / 0.2)",
|
||||||
valueFormatter,
|
cellHeight = 26,
|
||||||
}: Props<T>) => {
|
valueFormatter,
|
||||||
const hasGroup = columns.some((c) => !!c.group) || columns.some((c) => c.headRowSpan === 2);
|
}: Props<T>) => {
|
||||||
|
const formatValue = valueFormatter ?? ((value: number) => String(roundNumber(value)));
|
||||||
const groupCells = useMemo(() => {
|
const clampRatio = (value: number) => Math.max(0, Math.min(1, value));
|
||||||
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));
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn("w-full overflow-x-auto", wrapperClassName)}>
|
<BaseTableRenderer
|
||||||
<table className={cn("lg-table", className)} style={{minWidth:"100%"}}>
|
columns={columns}
|
||||||
<colgroup>
|
wrapperClassName={wrapperClassName}
|
||||||
{columns.map((c, i) => (
|
tableClassName={tableClassName ?? className}
|
||||||
<col
|
minWidth="100%"
|
||||||
key={`${String(c.key)}-${i}`}
|
colgroup={
|
||||||
style={{
|
<colgroup>
|
||||||
width:
|
{columns.map((column, index) => (
|
||||||
c.width ??
|
<col
|
||||||
(c.rowHead ? 50 : 40), // 기본 폭(필요시 숫자 조절)
|
key={`${String(column.key)}-${index}`}
|
||||||
}}
|
style={{
|
||||||
/>
|
width: column.width ?? (column.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>
|
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</colgroup>
|
||||||
</table>
|
}
|
||||||
</div>
|
>
|
||||||
);
|
{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;
|
||||||
|
|||||||
31
src/components/lge-dashboard/table/core/BaseTableHeader.tsx
Normal file
31
src/components/lge-dashboard/table/core/BaseTableHeader.tsx
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import SectionTitle from "../../SectionTitle";
|
||||||
|
import { SAMPLE_SIZE_NOTICE } from "@/const/lg-common.const";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
title?: string;
|
||||||
|
unitText?: string;
|
||||||
|
showSampleNotice?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function BaseTableHeader({ title, unitText, showSampleNotice = false }: Props) {
|
||||||
|
if (!title && !unitText && !showSampleNotice) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mb-2 flex items-center justify-between">
|
||||||
|
{title ? <SectionTitle title={title} /> : <div />}
|
||||||
|
{unitText || showSampleNotice ? (
|
||||||
|
<div className="ml-auto text-right text-gray-500 content-text-xs font-semibold">
|
||||||
|
{unitText}
|
||||||
|
{showSampleNotice ? (
|
||||||
|
<>
|
||||||
|
<br />
|
||||||
|
{SAMPLE_SIZE_NOTICE}
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import type React from "react";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import BaseTableHeader from "./BaseTableHeader";
|
||||||
|
import { buildGroupHeaderCells, hasGroupedHeader } from "./table-core.helpers";
|
||||||
|
|
||||||
|
type BaseColumn<T> = {
|
||||||
|
key: keyof T;
|
||||||
|
header: string;
|
||||||
|
width?: number | string;
|
||||||
|
group?: string;
|
||||||
|
headRowSpan?: 1 | 2;
|
||||||
|
headerClassName?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Props<T> = {
|
||||||
|
columns: BaseColumn<T>[];
|
||||||
|
title?: string;
|
||||||
|
unitText?: string;
|
||||||
|
showSampleNotice?: boolean;
|
||||||
|
wrapperClassName?: string;
|
||||||
|
tableClassName?: string;
|
||||||
|
minWidth?: number | string;
|
||||||
|
colgroup?: React.ReactNode;
|
||||||
|
renderHeaderPrefix?: React.ReactNode;
|
||||||
|
children: React.ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function BaseTableRenderer<T>({
|
||||||
|
columns,
|
||||||
|
title,
|
||||||
|
unitText,
|
||||||
|
showSampleNotice = false,
|
||||||
|
wrapperClassName,
|
||||||
|
tableClassName,
|
||||||
|
minWidth,
|
||||||
|
colgroup,
|
||||||
|
renderHeaderPrefix,
|
||||||
|
children,
|
||||||
|
}: Props<T>) {
|
||||||
|
const groupedHeader = hasGroupedHeader(columns);
|
||||||
|
const groupHeaderCells = buildGroupHeaderCells(columns);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("w-full", wrapperClassName)}>
|
||||||
|
<BaseTableHeader title={title} unitText={unitText} showSampleNotice={showSampleNotice} />
|
||||||
|
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<table
|
||||||
|
className={cn("lg-table", tableClassName)}
|
||||||
|
style={minWidth === undefined ? undefined : { minWidth: typeof minWidth === "number" ? `${minWidth}px` : minWidth }}
|
||||||
|
>
|
||||||
|
{colgroup}
|
||||||
|
<thead>
|
||||||
|
{renderHeaderPrefix}
|
||||||
|
{groupedHeader ? (
|
||||||
|
<tr className="lg-table__group">
|
||||||
|
{groupHeaderCells.map((cell, index) => (
|
||||||
|
<th
|
||||||
|
key={`${cell.kind}-${cell.label}-${index}`}
|
||||||
|
colSpan={cell.colSpan}
|
||||||
|
rowSpan={cell.rowSpan}
|
||||||
|
className={cell.className}
|
||||||
|
>
|
||||||
|
{cell.label}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
) : null}
|
||||||
|
<tr className={groupedHeader ? "lg-table__subhead" : undefined}>
|
||||||
|
{columns
|
||||||
|
.filter((column) => column.headRowSpan !== 2)
|
||||||
|
.map((column) => (
|
||||||
|
<th
|
||||||
|
key={String(column.key)}
|
||||||
|
className={column.headerClassName}
|
||||||
|
style={column.width ? { width: column.width } : undefined}
|
||||||
|
>
|
||||||
|
{column.header}
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>{children}</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
247
src/components/lge-dashboard/table/core/table-core.helpers.tsx
Normal file
247
src/components/lge-dashboard/table/core/table-core.helpers.tsx
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
import type React from "react";
|
||||||
|
import { parseNumber } from "@/lib/utils";
|
||||||
|
import { roundNumber } from "@/lib/lg-dashboard";
|
||||||
|
import type { SignDisplayMode, SignStyle, ValueFormatter } from "../../types";
|
||||||
|
|
||||||
|
type BaseTableColumn<T> = {
|
||||||
|
key: keyof T;
|
||||||
|
header: string;
|
||||||
|
width?: number | string;
|
||||||
|
align?: "left" | "center" | "right";
|
||||||
|
group?: string;
|
||||||
|
headRowSpan?: 1 | 2;
|
||||||
|
rowHead?: boolean;
|
||||||
|
numeric?: boolean;
|
||||||
|
headerClassName?: string;
|
||||||
|
cellClassName?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GroupHeaderCell = {
|
||||||
|
kind: "rowspan" | "group" | "blank";
|
||||||
|
label: string;
|
||||||
|
colSpan: number;
|
||||||
|
rowSpan: number;
|
||||||
|
className?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolvedSignStyle = {
|
||||||
|
paint: "text" | "cell";
|
||||||
|
display: SignDisplayMode;
|
||||||
|
showPlus: boolean;
|
||||||
|
arrowWithNumber: boolean;
|
||||||
|
zeroText: string;
|
||||||
|
valueFormatter: ValueFormatter;
|
||||||
|
positiveTextClassName: string;
|
||||||
|
negativeTextClassName: string;
|
||||||
|
zeroTextClassName: string;
|
||||||
|
positiveCellBg: string;
|
||||||
|
negativeCellBg: string;
|
||||||
|
zeroCellBg: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultValueFormatter: ValueFormatter = (value) =>
|
||||||
|
Number.isInteger(value) ? String(roundNumber(value)) : value.toLocaleString();
|
||||||
|
|
||||||
|
const defaultSignStyle: ResolvedSignStyle = {
|
||||||
|
paint: "text",
|
||||||
|
display: "number",
|
||||||
|
showPlus: true,
|
||||||
|
arrowWithNumber: false,
|
||||||
|
zeroText: "-",
|
||||||
|
valueFormatter: defaultValueFormatter,
|
||||||
|
positiveTextClassName: "text-primary font-semibold",
|
||||||
|
negativeTextClassName: "text-info font-semibold",
|
||||||
|
zeroTextClassName: "text-gray-600",
|
||||||
|
positiveCellBg: "hsl(var(--primary) / 0.08)",
|
||||||
|
negativeCellBg: "hsl(var(--info) / 0.08)",
|
||||||
|
zeroCellBg: "hsl(var(--lg-gray-200) / 0.6)",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function hasGroupedHeader<T>(columns: BaseTableColumn<T>[]) {
|
||||||
|
return columns.some((column) => column.group) || columns.some((column) => column.headRowSpan === 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildGroupHeaderCells<T>(columns: BaseTableColumn<T>[]) {
|
||||||
|
if (!hasGroupedHeader(columns)) {
|
||||||
|
return [] as GroupHeaderCell[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const cells: GroupHeaderCell[] = [];
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
while (index < columns.length) {
|
||||||
|
const column = columns[index];
|
||||||
|
|
||||||
|
if (column.headRowSpan === 2) {
|
||||||
|
cells.push({
|
||||||
|
kind: "rowspan",
|
||||||
|
label: column.header,
|
||||||
|
colSpan: 1,
|
||||||
|
rowSpan: 2,
|
||||||
|
className: column.headerClassName,
|
||||||
|
});
|
||||||
|
index += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!column.group) {
|
||||||
|
cells.push({
|
||||||
|
kind: "blank",
|
||||||
|
label: "",
|
||||||
|
colSpan: 1,
|
||||||
|
rowSpan: 1,
|
||||||
|
className: column.headerClassName,
|
||||||
|
});
|
||||||
|
index += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let span = 1;
|
||||||
|
let cursor = index + 1;
|
||||||
|
while (cursor < columns.length && columns[cursor].headRowSpan !== 2 && columns[cursor].group === column.group) {
|
||||||
|
span += 1;
|
||||||
|
cursor += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cells.push({
|
||||||
|
kind: "group",
|
||||||
|
label: column.group,
|
||||||
|
colSpan: span,
|
||||||
|
rowSpan: 1,
|
||||||
|
className: column.headerClassName,
|
||||||
|
});
|
||||||
|
index = cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cells;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getColumnAlign<T>(column: BaseTableColumn<T>, fallback: "left" | "center" | "right" = "center") {
|
||||||
|
if (column.align) {
|
||||||
|
return column.align;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (column.numeric) {
|
||||||
|
return "right";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (column.rowHead) {
|
||||||
|
return "left";
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toTableCellContent(raw: unknown): React.ReactNode {
|
||||||
|
if (raw == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof raw === "string") {
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = Number(trimmed);
|
||||||
|
return Number.isNaN(parsed) ? raw : roundNumber(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof raw === "number") {
|
||||||
|
return String(roundNumber(raw));
|
||||||
|
}
|
||||||
|
|
||||||
|
return String(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toRoundedNumber(raw: unknown) {
|
||||||
|
const parsed = parseNumber(raw);
|
||||||
|
return parsed === null ? null : roundNumber(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function appendCellMarker(content: React.ReactNode, marker: string) {
|
||||||
|
if (!marker) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof content === "string" || typeof content === "number") {
|
||||||
|
return `${content}${marker}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{content}
|
||||||
|
{marker}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveSignStyle<T>(signStyle?: SignStyle<T>, fallback?: Partial<ResolvedSignStyle>) {
|
||||||
|
return {
|
||||||
|
...defaultSignStyle,
|
||||||
|
...fallback,
|
||||||
|
...signStyle,
|
||||||
|
valueFormatter: signStyle?.valueFormatter ?? fallback?.valueFormatter ?? defaultSignStyle.valueFormatter,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function shouldApplySignToColumn<T>(column: BaseTableColumn<T>, signStyle?: SignStyle<T>) {
|
||||||
|
if (!signStyle || column.rowHead) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (signStyle.keys?.length) {
|
||||||
|
return signStyle.keys.includes(column.key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!column.numeric;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatSignedValue(
|
||||||
|
value: number,
|
||||||
|
options: Pick<ResolvedSignStyle, "arrowWithNumber" | "display" | "showPlus" | "valueFormatter" | "zeroText">
|
||||||
|
) {
|
||||||
|
if (options.display === "arrow") {
|
||||||
|
if (value > 0) {
|
||||||
|
return options.arrowWithNumber ? `▲ ${options.valueFormatter(Math.abs(value))}` : "▲";
|
||||||
|
}
|
||||||
|
if (value < 0) {
|
||||||
|
return options.arrowWithNumber ? `▼ ${options.valueFormatter(Math.abs(value))}` : "▼";
|
||||||
|
}
|
||||||
|
return options.zeroText;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value > 0) {
|
||||||
|
return options.showPlus ? `+${options.valueFormatter(value)}` : options.valueFormatter(value);
|
||||||
|
}
|
||||||
|
if (value < 0) {
|
||||||
|
return options.valueFormatter(value);
|
||||||
|
}
|
||||||
|
return options.zeroText;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSignTextClass(
|
||||||
|
value: number,
|
||||||
|
options: Pick<ResolvedSignStyle, "negativeTextClassName" | "positiveTextClassName" | "zeroTextClassName">
|
||||||
|
) {
|
||||||
|
if (value > 0) {
|
||||||
|
return options.positiveTextClassName;
|
||||||
|
}
|
||||||
|
if (value < 0) {
|
||||||
|
return options.negativeTextClassName;
|
||||||
|
}
|
||||||
|
return options.zeroTextClassName;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSignCellBackground(
|
||||||
|
value: number,
|
||||||
|
options: Pick<ResolvedSignStyle, "negativeCellBg" | "positiveCellBg" | "zeroCellBg">
|
||||||
|
) {
|
||||||
|
if (value > 0) {
|
||||||
|
return options.positiveCellBg;
|
||||||
|
}
|
||||||
|
if (value < 0) {
|
||||||
|
return options.negativeCellBg;
|
||||||
|
}
|
||||||
|
return options.zeroCellBg;
|
||||||
|
}
|
||||||
@@ -86,6 +86,8 @@ export type GaugeColumn<T> = {
|
|||||||
max?: number;
|
max?: number;
|
||||||
render?: (row: T) => React.ReactNode; // rowHead에 쓰기 좋음
|
render?: (row: T) => React.ReactNode; // rowHead에 쓰기 좋음
|
||||||
cellType?: "gauge" | "number"; // default: "gauge"
|
cellType?: "gauge" | "number"; // default: "gauge"
|
||||||
|
headerClassName?: string;
|
||||||
|
cellClassName?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SignDisplayMode = "number" | "arrow";
|
export type SignDisplayMode = "number" | "arrow";
|
||||||
|
|||||||
Reference in New Issue
Block a user