260507_01_ENH: SafetyInspectionTotal, SafetyInspectionDivDetail 페이지 구현
This commit is contained in:
162
src/components/table/BaseDataTable.tsx
Normal file
162
src/components/table/BaseDataTable.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
"use client";
|
||||
|
||||
import type React from "react";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
|
||||
import BaseTableRenderer from "./core/BaseTableRenderer";
|
||||
import {
|
||||
appendCellMarker,
|
||||
getColumnAlign,
|
||||
getLeafColumns,
|
||||
toRoundedNumber,
|
||||
toTableCellContent,
|
||||
} from "./core/table-core.helpers";
|
||||
import {DataColumn} from "@/components/table/types";
|
||||
import {isGapRow} from "@/lib/kfpa-utill";
|
||||
|
||||
export type MergedTableCell<T> = {
|
||||
key: keyof T;
|
||||
content: React.ReactNode;
|
||||
startRowIndex?: number;
|
||||
rowSpan?: number;
|
||||
className?: string;
|
||||
};
|
||||
|
||||
export type PreAppendTableCell =
|
||||
| React.ReactNode
|
||||
| {
|
||||
content: React.ReactNode;
|
||||
className?: string;
|
||||
colSpan?: number;
|
||||
rowSpan?: number;
|
||||
};
|
||||
|
||||
type Props<T extends Record<string, unknown>> = {
|
||||
columns: DataColumn<T>[];
|
||||
rows: T[];
|
||||
title?: string;
|
||||
wrapperClassName?: string;
|
||||
tableClassName?: string;
|
||||
subheadRowClassName?: string;
|
||||
minWidth?: number | string;
|
||||
fillGapRow?: boolean;
|
||||
mergedCell?: MergedTableCell<T>;
|
||||
getCellClassName?: (row: T, column: DataColumn<T>, rowIndex: number) => string | undefined;
|
||||
};
|
||||
|
||||
const getPreAppendCells = (row: Record<string, unknown>): PreAppendTableCell[] => {
|
||||
const cells = row.preAppendCells;
|
||||
return Array.isArray(cells) ? (cells as PreAppendTableCell[]) : [];
|
||||
};
|
||||
|
||||
const renderPreAppendCell = (cell: PreAppendTableCell, index: number) => {
|
||||
const isCellConfig =
|
||||
typeof cell === "object" &&
|
||||
cell !== null &&
|
||||
!Array.isArray(cell) &&
|
||||
"content" in cell;
|
||||
|
||||
if (!isCellConfig) {
|
||||
return <td key={`pre-append-${index}`}>{cell}</td>;
|
||||
}
|
||||
|
||||
return (
|
||||
<td
|
||||
key={`pre-append-${index}`}
|
||||
className={cell.className}
|
||||
colSpan={cell.colSpan}
|
||||
rowSpan={cell.rowSpan}
|
||||
>
|
||||
{cell.content}
|
||||
</td>
|
||||
);
|
||||
};
|
||||
|
||||
export default function BaseDataTable<T extends Record<string, unknown>>({
|
||||
columns,
|
||||
rows,
|
||||
title,
|
||||
wrapperClassName,
|
||||
tableClassName,
|
||||
subheadRowClassName,
|
||||
minWidth,
|
||||
fillGapRow = false,
|
||||
mergedCell,
|
||||
getCellClassName,
|
||||
}: Readonly<Props<T>>) {
|
||||
const mergedCellStart = mergedCell?.startRowIndex ?? 0;
|
||||
const mergedCellSpan = mergedCell?.rowSpan ?? Math.max(0, rows.length - mergedCellStart);
|
||||
const leafColumns = getLeafColumns(columns);
|
||||
|
||||
return (
|
||||
<BaseTableRenderer
|
||||
columns={columns}
|
||||
title={title}
|
||||
wrapperClassName={wrapperClassName}
|
||||
tableClassName={tableClassName}
|
||||
subheadRowClassName={subheadRowClassName}
|
||||
minWidth={minWidth}
|
||||
>
|
||||
{rows.map((row, rowIndex) => {
|
||||
const rowTotal = parseNumber(row.total);
|
||||
const isFillGapRow = fillGapRow && isGapRow((row.rowLabel as string) ?? "")
|
||||
const fillGapClass = isFillGapRow ? "bg-gray-100" : "";
|
||||
const preAppendCells = getPreAppendCells(row);
|
||||
|
||||
return (
|
||||
<tr key={rowIndex}>
|
||||
{preAppendCells.map(renderPreAppendCell)}
|
||||
{leafColumns.map((column) => {
|
||||
const dynamicCellClassName = getCellClassName?.(row, column, rowIndex);
|
||||
|
||||
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 = "numeric" in column && column.numeric ? parseNumber(rawValue ?? defaultContent) ?? 0 : 0;
|
||||
|
||||
if (column.rowHead) {
|
||||
return (
|
||||
<th
|
||||
key={String(column.key)}
|
||||
scope="row"
|
||||
className={cn("lg-table__rowhead", column.cellClassName, dynamicCellClassName, fillGapClass)}
|
||||
style={{ textAlign: align }}
|
||||
>
|
||||
{defaultContent}
|
||||
</th>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<td
|
||||
key={String(column.key)}
|
||||
className={cn(column.cellClassName, dynamicCellClassName, fillGapClass ? "bg-red-200" : "")}
|
||||
style={{ textAlign: align }}
|
||||
>
|
||||
{defaultContent}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</BaseTableRenderer>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user