260326_02_ENH: 테이블 4종 통합 및 BaseTable 생성
This commit is contained in:
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>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user