260507_01_ENH: SafetyInspectionTotal, SafetyInspectionDivDetail 페이지 구현
This commit is contained in:
236
src/components/combo/TopChartTable.tsx
Normal file
236
src/components/combo/TopChartTable.tsx
Normal file
@@ -0,0 +1,236 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo } from "react";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import BaseTableHeader from "../table/core/BaseTableHeader";
|
||||
import {
|
||||
formatSignedValue, getSignTextClass,
|
||||
resolveSignStyle,
|
||||
toRoundedNumber
|
||||
} from "../table/core/table-core.helpers";
|
||||
import {TopChartTablePreAppendCell, TopChartTablePreAppendCellConfig, TopChartTableRow} from "./types";
|
||||
import {ChartData} from "../chart/types";
|
||||
import {CHART_HEIGHTS, TABLE_DIMENSIONS} from "../chart/chart.config";
|
||||
import {SignStyle} from "@/components/table/types";
|
||||
|
||||
export type TopChartTableProps = {
|
||||
title?: string;
|
||||
categories: string[];
|
||||
rows: TopChartTableRow[];
|
||||
chart?:
|
||||
| React.ReactNode
|
||||
| ((ctx: {
|
||||
chartData: ChartData;
|
||||
}) => React.ReactNode);
|
||||
chartHeight?: number | string;
|
||||
minWidthPx?: number;
|
||||
minWidth?: number;
|
||||
subLabelHeader?: string;
|
||||
rowHeaderWidth?: number | string;
|
||||
tableClassName?: string;
|
||||
wrapperClassName?: string;
|
||||
signStyle?: SignStyle<TopChartTableRow>;
|
||||
};
|
||||
|
||||
const toCssSize = (value?: number | string) =>
|
||||
typeof value === "number" ? `${value}px` : value;
|
||||
|
||||
const isPreAppendCellConfig = (
|
||||
cell: TopChartTablePreAppendCell
|
||||
): cell is TopChartTablePreAppendCellConfig =>
|
||||
typeof cell === "object" &&
|
||||
cell !== null &&
|
||||
!Array.isArray(cell) &&
|
||||
"content" in cell;
|
||||
|
||||
const renderPreAppendCell = (cell: TopChartTablePreAppendCell, index: number) => {
|
||||
if (!isPreAppendCellConfig(cell)) {
|
||||
return <td key={`pre-append-${index}`}>{cell}</td>;
|
||||
}
|
||||
|
||||
return (
|
||||
<td
|
||||
key={`pre-append-${index}`}
|
||||
className={cell.className}
|
||||
colSpan={cell.colSpan}
|
||||
rowSpan={cell.rowSpan}
|
||||
style={{ width: toCssSize(cell.width), minWidth: toCssSize(cell.width) }}
|
||||
>
|
||||
{cell.content}
|
||||
</td>
|
||||
);
|
||||
};
|
||||
|
||||
const getLeadingColumnWidths = (rows: TopChartTableRow[], preAppendColumnCount: number) => {
|
||||
const widths = Array<string | undefined>(preAppendColumnCount).fill(undefined);
|
||||
|
||||
rows.forEach((row) => {
|
||||
let columnIndex = 0;
|
||||
|
||||
row.preAppendCells?.forEach((cell) => {
|
||||
const colSpan = isPreAppendCellConfig(cell) ? cell.colSpan ?? 1 : 1;
|
||||
const width = isPreAppendCellConfig(cell) ? toCssSize(cell.width) : undefined;
|
||||
|
||||
if (width && colSpan === 1 && !widths[columnIndex]) {
|
||||
widths[columnIndex] = width;
|
||||
}
|
||||
|
||||
columnIndex += colSpan;
|
||||
});
|
||||
});
|
||||
|
||||
return widths;
|
||||
};
|
||||
|
||||
const getLabelColumnWidth = (rows: TopChartTableRow[]) =>
|
||||
rows.map((row) => toCssSize(row.labelWidth)).find(Boolean);
|
||||
|
||||
export default function TopChartTable({
|
||||
title,
|
||||
rows,
|
||||
categories,
|
||||
chart,
|
||||
chartHeight = CHART_HEIGHTS.DEFAULT,
|
||||
minWidthPx = TABLE_DIMENSIONS.MIN_WIDTH.STANDARD,
|
||||
minWidth,
|
||||
subLabelHeader = "",
|
||||
rowHeaderWidth,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
}: Readonly<TopChartTableProps>) {
|
||||
const resolvedSignStyle = resolveSignStyle(signStyle, {
|
||||
positiveTextClassName: "kfpa-num-pos",
|
||||
negativeTextClassName: "kfpa-num-neg",
|
||||
zeroTextClassName: "",
|
||||
arrowWithNumber: true,
|
||||
});
|
||||
|
||||
const maxPreAppendCellCount = useMemo(
|
||||
() => Math.max(0, ...rows.map((row) => row.preAppendCells?.length ?? 0)),
|
||||
[rows]
|
||||
);
|
||||
const leadingColumnCount = maxPreAppendCellCount + 1;
|
||||
const leadingColumnWidths = useMemo(
|
||||
() => [...getLeadingColumnWidths(rows, maxPreAppendCellCount), getLabelColumnWidth(rows)],
|
||||
[maxPreAppendCellCount, rows]
|
||||
);
|
||||
|
||||
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]);
|
||||
const rowHeaderStyle = useMemo(() => {
|
||||
const width = toCssSize(rowHeaderWidth);
|
||||
return width ? { width, minWidth: width } : undefined;
|
||||
}, [rowHeaderWidth]);
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
<BaseTableHeader title={title} />
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className={cn("lg-table kfpa-table", tableClassName)} style={{ minWidth: `${minWidth ?? minWidthPx}px` }}>
|
||||
<colgroup>
|
||||
{leadingColumnWidths.map((width, index) => (
|
||||
<col key={`leading-col-${index}`} style={width ? { width, minWidth: width } : undefined} />
|
||||
))}
|
||||
{categories.map((category) => (
|
||||
<col key={`category-col-${category}`} />
|
||||
))}
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr>
|
||||
<th
|
||||
colSpan={leadingColumnCount}
|
||||
className={rowHeaderWidth ? undefined : "w-44"}
|
||||
style={{ whiteSpace: "nowrap", ...rowHeaderStyle }}
|
||||
/>
|
||||
<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 kfpa-table__subhead">
|
||||
<th
|
||||
colSpan={leadingColumnCount}
|
||||
className="lg-table__sep-r kfpa-table__sep-r content-text-base"
|
||||
style={rowHeaderStyle}
|
||||
>
|
||||
{subLabelHeader}
|
||||
</th>
|
||||
{categories.map((category) => (
|
||||
<th key={category} className="content-text-base">
|
||||
{category}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{rows.map((row, rowIndex) => {
|
||||
const signRow = !!signStyle?.rowSelector?.(row, rowIndex);
|
||||
const preAppendCells = row.preAppendCells ?? [];
|
||||
|
||||
return (
|
||||
<tr key={`${row.label}-${rowIndex}`}>
|
||||
{preAppendCells.map(renderPreAppendCell)}
|
||||
<th
|
||||
scope="row"
|
||||
className={cn(
|
||||
"lg-table__rowhead kfpa-table__rowhead lg-table__sep-r kfpa-table__sep-r content-text-base",
|
||||
row.labelClassName
|
||||
)}
|
||||
style={{
|
||||
width: toCssSize(row.labelWidth),
|
||||
minWidth: toCssSize(row.labelWidth),
|
||||
}}
|
||||
>
|
||||
{row.label}
|
||||
</th>
|
||||
|
||||
{categories.map((_, categoryIndex) => {
|
||||
const rawValue = row.values?.[categoryIndex];
|
||||
const roundedValue = toRoundedNumber(rawValue, 2);
|
||||
|
||||
if (roundedValue === null) {
|
||||
return <td key={categoryIndex}>-</td>;
|
||||
}
|
||||
|
||||
if(!signRow) {
|
||||
return (
|
||||
<td key={categoryIndex}>
|
||||
{roundedValue}
|
||||
</td>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<td key={categoryIndex} className={getSignTextClass(roundedValue, resolvedSignStyle)}>
|
||||
{formatSignedValue(roundedValue, resolvedSignStyle)}
|
||||
</td>
|
||||
);
|
||||
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user