148 lines
4.8 KiB
TypeScript
148 lines
4.8 KiB
TypeScript
"use client";
|
|
|
|
import React, { useMemo } from "react";
|
|
import { cn, parseNumber } from "@/lib/utils";
|
|
import type { ChartData, SignStyle, TopChartTableRow } from "../types";
|
|
import { CHART_HEIGHTS, TABLE_DIMENSIONS } from "@/components/lge-dashboard/chart/chart.config";
|
|
import { UNIT_TEXT } from "@/const/lg-common.const";
|
|
import {isCagrRow, isSampleRow} 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[];
|
|
chart?:
|
|
| React.ReactNode
|
|
| ((ctx: {
|
|
chartData: ChartData;
|
|
}) => React.ReactNode);
|
|
chartHeight?: number | string;
|
|
minWidthPx?: number;
|
|
minWidth?: number;
|
|
tableClassName?: string;
|
|
wrapperClassName?: string;
|
|
signStyle?: SignStyle<TopChartTableRow>;
|
|
fillCAGR?: boolean;
|
|
unitText?: string;
|
|
};
|
|
|
|
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,
|
|
}: Readonly<TopChartTableProps>) {
|
|
const resolvedSignStyle = resolveSignStyle(signStyle, {
|
|
positiveTextClassName: "lg-num-pos",
|
|
negativeTextClassName: "lg-num-neg",
|
|
zeroTextClassName: "",
|
|
arrowWithNumber: true,
|
|
});
|
|
|
|
const chartData: ChartData = useMemo(
|
|
() => ({
|
|
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;
|
|
}
|
|
return typeof chart === "function" ? chart({ chartData }) : chart;
|
|
}, [chart, chartData]);
|
|
|
|
return (
|
|
<div className={cn("w-full", wrapperClassName)}>
|
|
<BaseTableHeader title={title} unitText={unitText} />
|
|
|
|
<div className="overflow-x-auto">
|
|
<table className={cn("lg-table", tableClassName)} style={{ minWidth: `${minWidth ?? minWidthPx}px` }}>
|
|
<thead>
|
|
<tr>
|
|
<th colSpan={2} className="w-40" 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">
|
|
<th className="lg-table__sep-r"></th>
|
|
<th className="content-text-xs">사례수</th>
|
|
{categories.map((category) => (
|
|
<th key={category} className="content-text-xs">
|
|
{category}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
{rows.map((row, rowIndex) => {
|
|
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
|
const sampleRow = isSampleRow(row.label ?? "");
|
|
const cagrRow = isCagrRow(row.label ?? "");
|
|
const rowTotal = parseNumber(row.total);
|
|
|
|
return (
|
|
<tr key={`${row.label}-${rowIndex}`}>
|
|
<th scope="row" className="lg-table__rowhead lg-table__sep-r content-text-sm">
|
|
{row.label}
|
|
</th>
|
|
<td>
|
|
{row.sample}
|
|
</td>
|
|
|
|
{categories.map((_, categoryIndex) => {
|
|
const rawValue = row.values?.[categoryIndex];
|
|
|
|
if(sampleRow){
|
|
return <td key={categoryIndex}>{rawValue}</td>;
|
|
}
|
|
|
|
const roundedValue = toRoundedNumber(rawValue, cagrRow? 1 : 0);
|
|
|
|
if (!signRow) {
|
|
if (roundedValue === null) {
|
|
return <td key={categoryIndex}>-</td>;
|
|
}
|
|
|
|
const shouldFillCell = fillCAGR && cagrRow && rowTotal !== null && roundedValue > rowTotal + 5;
|
|
return <td key={categoryIndex} className={cn(shouldFillCell ? "bg-red-200" : "")}>{roundedValue.toLocaleString()}</td>;
|
|
}
|
|
|
|
if (roundedValue === null) {
|
|
return <td key={categoryIndex}>-</td>;
|
|
}
|
|
|
|
return (
|
|
<td key={categoryIndex} className={getSignTextClass(roundedValue, resolvedSignStyle)}>
|
|
{formatSignedValue(roundedValue, resolvedSignStyle)}
|
|
</td>
|
|
);
|
|
})}
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|