260507_01_ENH: SafetyInspectionTotal, SafetyInspectionDivDetail 페이지 구현
This commit is contained in:
99
src/components/chart/BaseEChart.tsx
Normal file
99
src/components/chart/BaseEChart.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
"use client";
|
||||
|
||||
import dynamic from "next/dynamic";
|
||||
import type { EChartsOption } from "echarts";
|
||||
|
||||
import {useResizeKey} from "@/hooks/useResize";
|
||||
import { useMemo } from "react";
|
||||
import {roundNumber} from "@/lib/utils";
|
||||
import {CHART_HEIGHTS} from "./chart.config";
|
||||
|
||||
|
||||
const ReactECharts = dynamic(() => import("echarts-for-react"), { ssr: false });
|
||||
|
||||
type BaseEChartProps = {
|
||||
option: EChartsOption;
|
||||
height?: number | string;
|
||||
className?: string; // wrapper용
|
||||
contentClassName?: string; // 안쪽 wrapper
|
||||
chartClassName?: string; // ReactECharts용
|
||||
/** echarts-for-react의 notMerge/lazyUpdate 옵션 필요 시 */
|
||||
notMerge?: boolean;
|
||||
lazyUpdate?: boolean;
|
||||
/** echarts-for-react 이벤트 바인딩(예: legendselectchanged) */
|
||||
onEvents?: Record<string, (params: unknown) => void>;
|
||||
};
|
||||
|
||||
const normalizeDataValue = (value: unknown): unknown => {
|
||||
if (typeof value === "number") {
|
||||
return roundNumber(value, 2);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(normalizeDataValue);
|
||||
}
|
||||
|
||||
if (value && typeof value === "object" && "value" in value) {
|
||||
const item = value as { value: unknown };
|
||||
return {
|
||||
...(value as Record<string, unknown>),
|
||||
value: normalizeDataValue(item.value),
|
||||
};
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
const normalizeSeries = (series?: EChartsOption["series"]): EChartsOption["series"] => {
|
||||
if (!series) return series;
|
||||
|
||||
const normalizeOne = (s: any) => {
|
||||
if (!s || !("data" in s) || !Array.isArray(s.data)) {
|
||||
return s;
|
||||
}
|
||||
|
||||
return {
|
||||
...s,
|
||||
data: s.data.map(normalizeDataValue),
|
||||
};
|
||||
};
|
||||
|
||||
return Array.isArray(series) ? series.map(normalizeOne) : normalizeOne(series);
|
||||
};
|
||||
const BaseEChart = ({
|
||||
option,
|
||||
height = CHART_HEIGHTS.DEFAULT,
|
||||
className,
|
||||
contentClassName,
|
||||
chartClassName,
|
||||
notMerge = true,
|
||||
lazyUpdate = false, //이거 켜져있으면 오류남 Cannot read properties of undefined (reading 'getRawIndex')
|
||||
onEvents,
|
||||
}: BaseEChartProps) => {
|
||||
const resizeKey = useResizeKey(150);
|
||||
|
||||
const normalizedOption = useMemo<EChartsOption>(() => {
|
||||
return {
|
||||
...option,
|
||||
series: normalizeSeries(option.series),
|
||||
};
|
||||
}, [option]);
|
||||
|
||||
return (
|
||||
<div className={className} style={{ display: "flex", flexDirection: "column", height: height }}>
|
||||
<div className={contentClassName} style={{ flex: 1, minHeight: 0 }}>
|
||||
<ReactECharts
|
||||
key={resizeKey}
|
||||
option={normalizedOption}
|
||||
className={chartClassName}
|
||||
style={{ width: "100%", height: "100%" }}
|
||||
notMerge={notMerge}
|
||||
lazyUpdate={lazyUpdate}
|
||||
onEvents={onEvents}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default BaseEChart;
|
||||
91
src/components/chart/HorizontalBar.tsx
Normal file
91
src/components/chart/HorizontalBar.tsx
Normal file
@@ -0,0 +1,91 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import type { EChartsOption } from "echarts";
|
||||
import {ChartData, ShowLabel} from "@/components/chart/types";
|
||||
import {ValueFormatter} from "@/components/table/types";
|
||||
import {buildLgPalette} from "@/components/chart/color";
|
||||
import {
|
||||
createCategoryAxis,
|
||||
createDefaultGrid,
|
||||
createDefaultLegend,
|
||||
createDefaultTooltip, createValueAxis
|
||||
} from "@/components/chart/chart-options";
|
||||
import {getIsShowLabel} from "@/components/chart/utill";
|
||||
import BaseEChart from "@/components/chart/BaseEChart";
|
||||
import {BAR_WIDTHS} from "@/components/chart/chart.config";
|
||||
|
||||
type Props = {
|
||||
data: ChartData;
|
||||
height?: number | string;
|
||||
className?: string;
|
||||
paletteVarNames?: string[];
|
||||
valueFormatter?: ValueFormatter;
|
||||
tooltip?: EChartsOption["tooltip"];
|
||||
legend?: EChartsOption["legend"];
|
||||
grid?: EChartsOption["grid"];
|
||||
xAxis?: EChartsOption["xAxis"];
|
||||
yAxis?: EChartsOption["yAxis"];
|
||||
barMaxWidth?: number;
|
||||
showFocusLabel?:ShowLabel;
|
||||
labelOptions?: {
|
||||
position?: "right" | "insideRight";
|
||||
color?: string;
|
||||
distance?: number;
|
||||
};
|
||||
};
|
||||
|
||||
const HorizontalBar = ({
|
||||
data,
|
||||
height,
|
||||
className,
|
||||
paletteVarNames,
|
||||
valueFormatter,
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
showFocusLabel = "all",
|
||||
labelOptions,
|
||||
}: Props) => {
|
||||
const palette = useMemo(() => buildLgPalette(paletteVarNames), [paletteVarNames]);
|
||||
|
||||
const option: EChartsOption = useMemo(() => {
|
||||
const fmt = valueFormatter ?? ((v: number) => String(v));
|
||||
|
||||
return {
|
||||
color: palette,
|
||||
tooltip: tooltip ?? createDefaultTooltip(fmt),
|
||||
legend: legend ?? createDefaultLegend(),
|
||||
grid: grid ?? createDefaultGrid(),
|
||||
xAxis: xAxis ?? createValueAxis(),
|
||||
yAxis: yAxis ?? createCategoryAxis(data.categories),
|
||||
series: data.series.map((s, index) => {
|
||||
const showLabel = getIsShowLabel(showFocusLabel, index, data.series.length);
|
||||
|
||||
return {
|
||||
name: s.name,
|
||||
type: "bar",
|
||||
data: s.data,
|
||||
emphasis: { disabled: true },
|
||||
barMaxWidth: barMaxWidth ?? BAR_WIDTHS.DEFAULT,
|
||||
label: showLabel
|
||||
? {
|
||||
show: true,
|
||||
position: labelOptions?.position ?? "right",
|
||||
color: labelOptions?.color,
|
||||
distance: labelOptions?.distance,
|
||||
formatter: (p: any) => (p?.value ?? "") as any,
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}, [data, palette, valueFormatter, tooltip, legend, grid, xAxis, yAxis, barMaxWidth, showFocusLabel, labelOptions]);
|
||||
|
||||
return <BaseEChart option={option} height={height} className={className} />;
|
||||
}
|
||||
|
||||
export default HorizontalBar;
|
||||
93
src/components/chart/VerticalBar.tsx
Normal file
93
src/components/chart/VerticalBar.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import type { EChartsOption } from "echarts";
|
||||
import {buildLgPalette} from "@/components/chart/color";
|
||||
import {
|
||||
createCategoryAxis,
|
||||
createDefaultGrid,
|
||||
createDefaultLegend,
|
||||
createDefaultTooltip, createValueAxis
|
||||
} from "@/components/chart/chart-options";
|
||||
import {getIsShowLabel} from "@/components/chart/utill";
|
||||
import BaseEChart from "@/components/chart/BaseEChart";
|
||||
import {BAR_WIDTHS} from "@/components/chart/chart.config";
|
||||
import {ChartData, ShowLabel} from "@/components/chart/types";
|
||||
import {ValueFormatter} from "@/components/table/types";
|
||||
|
||||
|
||||
type Props = {
|
||||
data: ChartData;
|
||||
height?: number | string;
|
||||
className?: string;
|
||||
paletteVarNames?: string[];
|
||||
valueFormatter?: ValueFormatter;
|
||||
|
||||
tooltip?: EChartsOption["tooltip"];
|
||||
legend?: EChartsOption["legend"];
|
||||
grid?: EChartsOption["grid"];
|
||||
xAxis?: EChartsOption["xAxis"];
|
||||
yAxis?: EChartsOption["yAxis"];
|
||||
barMaxWidth?: number;
|
||||
showFocusLabel?:ShowLabel;
|
||||
labelOptions?: {
|
||||
position?: "top" | "insideTop" | "right" | "insideRight";
|
||||
color?: string;
|
||||
distance?: number;
|
||||
};
|
||||
};
|
||||
|
||||
const VerticalBar = ({
|
||||
data,
|
||||
height,
|
||||
className,
|
||||
paletteVarNames,
|
||||
valueFormatter,
|
||||
tooltip,
|
||||
legend,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
barMaxWidth,
|
||||
showFocusLabel = "all",
|
||||
labelOptions,
|
||||
}: Props) => {
|
||||
const palette = useMemo(() => buildLgPalette(paletteVarNames), [paletteVarNames]);
|
||||
|
||||
const option: EChartsOption = useMemo(() => {
|
||||
const fmt = valueFormatter ?? ((v: number) => String(v));
|
||||
|
||||
return {
|
||||
color: palette,
|
||||
tooltip: tooltip ?? createDefaultTooltip(fmt),
|
||||
legend: legend ?? createDefaultLegend(),
|
||||
grid: grid ?? createDefaultGrid(),
|
||||
xAxis: xAxis ?? createCategoryAxis(data.categories, false, { axisTick: { alignWithLabel: true } }),
|
||||
yAxis: yAxis ?? createValueAxis(),
|
||||
series: data.series.map((s, index) => {
|
||||
const showLabel = getIsShowLabel(showFocusLabel, index, data.series.length);
|
||||
|
||||
return {
|
||||
name: s.name,
|
||||
type: "bar",
|
||||
data: s.data,
|
||||
emphasis: { disabled: true },
|
||||
barMaxWidth: barMaxWidth ?? BAR_WIDTHS.DEFAULT,
|
||||
label: showLabel
|
||||
? {
|
||||
show: true,
|
||||
position: labelOptions?.position ?? "right",
|
||||
color: labelOptions?.color,
|
||||
distance: labelOptions?.distance,
|
||||
formatter: (p: any) => (typeof p?.value === "number" ? fmt(p.value) : (p?.value ?? "")) as any,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
}),
|
||||
};
|
||||
}, [data, palette, valueFormatter, tooltip, legend, grid, xAxis, yAxis, barMaxWidth, showFocusLabel, labelOptions]);
|
||||
|
||||
return <BaseEChart option={option} height={height} className={className} />;
|
||||
}
|
||||
|
||||
export default VerticalBar;
|
||||
134
src/components/chart/color.ts
Normal file
134
src/components/chart/color.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
type Rgb = { r: number; g: number; b: number };
|
||||
|
||||
const MAX_VAR_DEPTH = 10;
|
||||
|
||||
const getCssVarRaw = (varName: string) =>
|
||||
getComputedStyle(document.documentElement).getPropertyValue(varName).trim();
|
||||
|
||||
const clamp = (value: number, min: number, max: number) =>
|
||||
Math.min(max, Math.max(min, value));
|
||||
|
||||
const getVarFallback = (varName: string) =>
|
||||
varName.startsWith("--") ? `var(${varName})` : varName;
|
||||
|
||||
/** Resolve var(--a) / var(--a, fallback) to its concrete CSS value. */
|
||||
export const resolveCssVarValue = (varName: string, depth = 0): string | null => {
|
||||
if (typeof window === "undefined") return null;
|
||||
if (depth > MAX_VAR_DEPTH) return null;
|
||||
|
||||
const raw = getCssVarRaw(varName);
|
||||
if (!raw) return null;
|
||||
|
||||
const m = raw.match(/^var\(\s*(--[^,\s)]+)\s*(?:,\s*([^)]+))?\s*\)$/);
|
||||
if (m) {
|
||||
const inner = m[1];
|
||||
const fallback = m[2]?.trim() ?? null;
|
||||
return resolveCssVarValue(inner, depth + 1) ?? fallback;
|
||||
}
|
||||
|
||||
return raw;
|
||||
};
|
||||
|
||||
export const resolveColorVar = (varName: string): string =>
|
||||
resolveCssVarValue(varName) ?? getVarFallback(varName);
|
||||
|
||||
const parseHexColor = (color: string): Rgb | null => {
|
||||
const hex = color.trim().replace(/^#/, "");
|
||||
|
||||
if (/^[\da-f]{3}$/i.test(hex)) {
|
||||
const [r, g, b] = hex.split("").map((v) => parseInt(v + v, 16));
|
||||
return { r, g, b };
|
||||
}
|
||||
|
||||
if (/^[\da-f]{6}$/i.test(hex)) {
|
||||
return {
|
||||
r: parseInt(hex.slice(0, 2), 16),
|
||||
g: parseInt(hex.slice(2, 4), 16),
|
||||
b: parseInt(hex.slice(4, 6), 16),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const parseRgbChannel = (token: string): number | null => {
|
||||
const t = token.trim();
|
||||
const parsed = Number.parseFloat(t);
|
||||
if (!Number.isFinite(parsed)) return null;
|
||||
|
||||
if (t.endsWith("%")) return clamp((parsed / 100) * 255, 0, 255);
|
||||
return clamp(parsed, 0, 255);
|
||||
};
|
||||
|
||||
const parseRgbColor = (color: string): Rgb | null => {
|
||||
const m = color
|
||||
.trim()
|
||||
.match(/^rgba?\(\s*([^)]+?)\s*\)$/i);
|
||||
if (!m) return null;
|
||||
|
||||
const tokens = m[1].replace(/\s*\/\s*[^, ]+$/, "").split(/[,\s]+/).filter(Boolean);
|
||||
if (tokens.length < 3) return null;
|
||||
|
||||
const [r, g, b] = tokens.slice(0, 3).map(parseRgbChannel);
|
||||
if (r == null || g == null || b == null) return null;
|
||||
|
||||
return { r, g, b };
|
||||
};
|
||||
|
||||
const parseCssColor = (color: string): Rgb | null =>
|
||||
parseHexColor(color) ?? parseRgbColor(color);
|
||||
|
||||
const rgbCss = ({ r, g, b }: Rgb, alpha = 1) =>
|
||||
`rgb(${Math.round(r)} ${Math.round(g)} ${Math.round(b)} / ${alpha})`;
|
||||
|
||||
const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
|
||||
|
||||
const buildPalette = (defaults: string[], varNames?: string[]): string[] =>
|
||||
(varNames ?? defaults).map(resolveColorVar);
|
||||
|
||||
export const buildLgPalette = (varNames?: string[]): string[] =>
|
||||
buildPalette(["--chart-1", "--chart-2", "--chart-3"], varNames);
|
||||
|
||||
export const buildLgStackPalette = (varNames?: string[]): string[] =>
|
||||
buildPalette(
|
||||
[
|
||||
"--chart-stack-1",
|
||||
"--chart-stack-2",
|
||||
"--chart-stack-3",
|
||||
"--chart-stack-4",
|
||||
"--chart-stack-5",
|
||||
"--chart-stack-6",
|
||||
"--chart-stack-7",
|
||||
"--chart-stack-8",
|
||||
"--chart-stack-9",
|
||||
"--chart-stack-10",
|
||||
"--chart-stack-11",
|
||||
"--chart-stack-12",
|
||||
"--chart-stack-13",
|
||||
],
|
||||
varNames
|
||||
);
|
||||
|
||||
/** Interpolate a row-level heatmap background between two CSS colors. */
|
||||
export const rowHeatColor = (
|
||||
value: number,
|
||||
rowMin: number,
|
||||
rowMax: number,
|
||||
low: string,
|
||||
high: string,
|
||||
alpha = 0.18
|
||||
) => {
|
||||
const lowRgb = parseCssColor(low);
|
||||
const highRgb = parseCssColor(high);
|
||||
if (!lowRgb || !highRgb) return high;
|
||||
|
||||
const t = rowMax === rowMin ? 0.5 : clamp((value - rowMin) / (rowMax - rowMin), 0, 1);
|
||||
return rgbCss(
|
||||
{
|
||||
r: lerp(lowRgb.r, highRgb.r, t),
|
||||
g: lerp(lowRgb.g, highRgb.g, t),
|
||||
b: lerp(lowRgb.b, highRgb.b, t),
|
||||
},
|
||||
alpha
|
||||
);
|
||||
};
|
||||
@@ -15,3 +15,5 @@ export type ChartData = {
|
||||
/** 시리즈(막대/스택 등) */
|
||||
series: ChartSeries[];
|
||||
};
|
||||
|
||||
export type ShowLabel = "none" | "all" | "last" | "first";
|
||||
22
src/components/chart/utill.ts
Normal file
22
src/components/chart/utill.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {ShowLabel} from "@/components/chart/types";
|
||||
|
||||
|
||||
export const roundedRectIcon = (w: number, h: number, r: number) => {
|
||||
const rr = Math.max(0, Math.min(r, Math.min(w, h) / 2));
|
||||
// ECharts legend icon은 'path://' 지원
|
||||
return `path://M${rr},0 H${w - rr} Q${w},0 ${w},${rr} V${h - rr} Q${w},${h} ${w - rr},${h} H${rr} Q0,${h} 0,${h - rr} V${rr} Q0,0 ${rr},0 Z`;
|
||||
};
|
||||
|
||||
export const getIsShowLabel = (showFocusLabel:ShowLabel, i:number, arrayLength: number ):boolean => {
|
||||
let isShowLabel = false;
|
||||
|
||||
if (showFocusLabel === "all") {
|
||||
isShowLabel = true;
|
||||
} else if (showFocusLabel === "last") {
|
||||
isShowLabel = i === arrayLength - 1;
|
||||
} else if (showFocusLabel === "first") {
|
||||
isShowLabel = i === 0;
|
||||
}
|
||||
|
||||
return isShowLabel;
|
||||
}
|
||||
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>
|
||||
);
|
||||
}
|
||||
21
src/components/combo/types.ts
Normal file
21
src/components/combo/types.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type React from "react";
|
||||
|
||||
export type TopChartTablePreAppendCellConfig = {
|
||||
content: React.ReactNode;
|
||||
className?: string;
|
||||
colSpan?: number;
|
||||
rowSpan?: number;
|
||||
width?: number | string;
|
||||
};
|
||||
|
||||
export type TopChartTablePreAppendCell =
|
||||
| React.ReactNode
|
||||
| TopChartTablePreAppendCellConfig;
|
||||
|
||||
export type TopChartTableRow = {
|
||||
label: string;
|
||||
labelClassName?: string;
|
||||
labelWidth?: number | string;
|
||||
preAppendCells?: TopChartTablePreAppendCell[];
|
||||
values: Array<number | string | null | undefined>;
|
||||
};
|
||||
@@ -9,7 +9,7 @@ export function LoadingOverlay({ force = false }: { force?: boolean }) {
|
||||
if (!force && !isLoading) return null;
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[9999] bg-black/70 flex items-center justify-center">
|
||||
<div className="fixed inset-0 z-[9999] bg-black-600/70 flex items-center justify-center">
|
||||
{/* 심플 로딩 인디케이터 */}
|
||||
<div className="h-12 w-12 rounded-full border-4 border-gray-400 border-t-white animate-spin" />
|
||||
</div>
|
||||
|
||||
10
src/components/common/SectionTitle.tsx
Normal file
10
src/components/common/SectionTitle.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
const SectionTitle = ({title}:{title:string}) => {
|
||||
return (
|
||||
<div className="font-semibold flex items-center gap-2">
|
||||
<span className="w-3 h-3 bg-primary" />
|
||||
{title}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default SectionTitle;
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import {useEffect, useRef, useState} from "react";
|
||||
import {apiError} from "@/lib/http-util";
|
||||
import {NONE_HTTP_ERROR} from "@/const/commonConst";
|
||||
import {NONE_HTTP_ERROR} from "../../const/common.const";
|
||||
import {DialogId} from "@/store/jotai/dialog";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import {useLoginMutation} from "@/query/auth";
|
||||
|
||||
@@ -55,7 +55,7 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
if(!isMounted) return null;
|
||||
|
||||
return (
|
||||
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
|
||||
<div className="flex h-dvh flex-col overflow-hidden bg-transparent">
|
||||
<LayoutCategoryProvider selectedDiv={selectedDiv}>
|
||||
<Header
|
||||
topMenus={TOP_MENUS}
|
||||
@@ -65,16 +65,18 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
onLogin={openLoginDialog}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
<div className="flex flex-1">
|
||||
<div className="flex h-[calc(100dvh-4rem)] min-h-0 overflow-hidden">
|
||||
<Sidebar
|
||||
sideMenus={SIDE_MENUS}
|
||||
isOpen={menuOpen}
|
||||
selectedDiv={selectedDiv}
|
||||
onChangeDiv={setSelectedDiv}
|
||||
/>
|
||||
<main className="flex-1 p-4">
|
||||
{children}
|
||||
</main>
|
||||
<div className="h-full min-h-0 flex-1 overflow-y-scroll overscroll-contain p-4">
|
||||
<main className="h-full min-h-0 flex-1">
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
<LoginDialog />
|
||||
|
||||
|
||||
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>
|
||||
);
|
||||
}
|
||||
43
src/components/table/DataTable.tsx
Normal file
43
src/components/table/DataTable.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
"use client";
|
||||
|
||||
import BaseDataTable from "./BaseDataTable";
|
||||
import {DataColumn} from "@/components/table/types";
|
||||
|
||||
type Props<T extends Record<string, unknown>> = {
|
||||
columns: DataColumn<T>[];
|
||||
rows: T[];
|
||||
title?: string;
|
||||
className?: string;
|
||||
tableClassName?: string;
|
||||
wrapperClassName?: string;
|
||||
subheadRowClassName?: string;
|
||||
minWidth?: number | string;
|
||||
fillGapRow?: boolean;
|
||||
};
|
||||
|
||||
const DataTable = <T extends Record<string, unknown>>({
|
||||
columns,
|
||||
rows,
|
||||
title,
|
||||
className,
|
||||
tableClassName,
|
||||
wrapperClassName,
|
||||
subheadRowClassName,
|
||||
minWidth,
|
||||
fillGapRow = false,
|
||||
}: Props<T>) => {
|
||||
return (
|
||||
<BaseDataTable
|
||||
columns={columns}
|
||||
rows={rows}
|
||||
title={title}
|
||||
tableClassName={tableClassName ?? className}
|
||||
wrapperClassName={wrapperClassName}
|
||||
subheadRowClassName={subheadRowClassName}
|
||||
minWidth={minWidth}
|
||||
fillGapRow={fillGapRow}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default DataTable;
|
||||
18
src/components/table/core/BaseTableHeader.tsx
Normal file
18
src/components/table/core/BaseTableHeader.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import SectionTitle from "@/components/common/SectionTitle";
|
||||
|
||||
|
||||
type Props = {
|
||||
title?: string;
|
||||
};
|
||||
|
||||
export default function BaseTableHeader({ title }: Readonly<Props>) {
|
||||
if (!title) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mb-2 flex items-center justify-between">
|
||||
<SectionTitle title={title} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
68
src/components/table/core/BaseTableRenderer.tsx
Normal file
68
src/components/table/core/BaseTableRenderer.tsx
Normal file
@@ -0,0 +1,68 @@
|
||||
"use client";
|
||||
|
||||
import type React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import BaseTableHeader from "./BaseTableHeader";
|
||||
import { buildHeaderRows } from "./table-core.helpers";
|
||||
import {DataColumn} from "@/components/table/types";
|
||||
|
||||
type BaseColumn<T> = DataColumn<T>;
|
||||
|
||||
type Props<T> = {
|
||||
columns: BaseColumn<T>[];
|
||||
title?: string;
|
||||
wrapperClassName?: string;
|
||||
tableClassName?: string;
|
||||
subheadRowClassName?: string;
|
||||
minWidth?: number | string;
|
||||
colgroup?: React.ReactNode;
|
||||
renderHeaderPrefix?: React.ReactNode;
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export default function BaseTableRenderer<T>({
|
||||
columns,
|
||||
title,
|
||||
wrapperClassName,
|
||||
tableClassName,
|
||||
subheadRowClassName,
|
||||
minWidth,
|
||||
colgroup,
|
||||
renderHeaderPrefix,
|
||||
children,
|
||||
}: Readonly<Props<T>>) {
|
||||
const headerRows = buildHeaderRows(columns);
|
||||
const lastHeaderRowIndex = headerRows.length - 1;
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", wrapperClassName)}>
|
||||
<BaseTableHeader title={title} />
|
||||
|
||||
<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}
|
||||
{headerRows.map((row, rowIndex) => (
|
||||
<tr
|
||||
key={`header-row-${rowIndex}`}
|
||||
className={cn(rowIndex === lastHeaderRowIndex ? "lg-table__subhead" : "lg-table__group", rowIndex === lastHeaderRowIndex ? subheadRowClassName : undefined)}
|
||||
>
|
||||
{row.map((cell) => (
|
||||
<th key={cell.id} colSpan={cell.colSpan} rowSpan={cell.rowSpan} className={cell.className}>
|
||||
{cell.label}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</thead>
|
||||
<tbody>{children}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
242
src/components/table/core/table-core.helpers.tsx
Normal file
242
src/components/table/core/table-core.helpers.tsx
Normal file
@@ -0,0 +1,242 @@
|
||||
import type React from "react";
|
||||
import { parseNumber, roundNumber } from "@/lib/utils";
|
||||
import {
|
||||
DataColumn,
|
||||
DataColumnLeaf,
|
||||
SignDisplayMode,
|
||||
SignStyle,
|
||||
ValueFormatter
|
||||
} from "@/components/table/types";
|
||||
|
||||
export type TableColumnNode<T> = DataColumn<T>;
|
||||
export type TableColumnLeaf<T> = DataColumnLeaf<T>;
|
||||
|
||||
export type GroupHeaderCell = {
|
||||
id: string;
|
||||
label: string;
|
||||
colSpan: number;
|
||||
rowSpan: number;
|
||||
className?: string;
|
||||
isLeaf: boolean;
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
function isColumnLeaf<T>(column: TableColumnNode<T>): column is TableColumnLeaf<T> {
|
||||
return !("children" in column) || !column.children?.length;
|
||||
}
|
||||
|
||||
export function getLeafColumns<T>(columns: TableColumnNode<T>[]) {
|
||||
const leaves: TableColumnLeaf<T>[] = [];
|
||||
|
||||
const visit = (nodes: TableColumnNode<T>[]) => {
|
||||
nodes.forEach((node) => {
|
||||
if (isColumnLeaf(node)) {
|
||||
leaves.push(node);
|
||||
return;
|
||||
}
|
||||
|
||||
visit(node.children);
|
||||
});
|
||||
};
|
||||
|
||||
visit(columns);
|
||||
return leaves;
|
||||
}
|
||||
|
||||
function getHeaderDepth<T>(columns: TableColumnNode<T>[]) {
|
||||
const visit = (nodes: TableColumnNode<T>[], depth: number): number =>
|
||||
nodes.reduce((maxDepth, node) => {
|
||||
if (isColumnLeaf(node)) {
|
||||
return Math.max(maxDepth, depth);
|
||||
}
|
||||
|
||||
return Math.max(maxDepth, visit(node.children, depth + 1));
|
||||
}, depth);
|
||||
|
||||
return columns.length ? visit(columns, 1) : 0;
|
||||
}
|
||||
|
||||
function getLeafCount<T>(column: TableColumnNode<T>): number {
|
||||
if (isColumnLeaf(column)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return column.children.reduce((sum, child) => sum + getLeafCount(child), 0);
|
||||
}
|
||||
|
||||
export function buildHeaderRows<T>(columns: TableColumnNode<T>[]) {
|
||||
const depth = getHeaderDepth(columns);
|
||||
|
||||
if (!depth) {
|
||||
return [] as GroupHeaderCell[][];
|
||||
}
|
||||
|
||||
const rows = Array.from({ length: depth }, () => [] as GroupHeaderCell[]);
|
||||
|
||||
const visit = (nodes: TableColumnNode<T>[], level: number, path: string[]) => {
|
||||
nodes.forEach((node, index) => {
|
||||
const nodeId = node.id ?? (isColumnLeaf(node) ? String(node.key) : `${path.join(".") || "root"}:${index}:${node.header}`);
|
||||
const nextPath = [...path, nodeId];
|
||||
const isLeaf = isColumnLeaf(node);
|
||||
|
||||
rows[level].push({
|
||||
id: nextPath.join("/"),
|
||||
label: node.header,
|
||||
colSpan: isLeaf ? 1 : getLeafCount(node),
|
||||
rowSpan: isLeaf ? depth - level : 1,
|
||||
className: node.headerClassName,
|
||||
isLeaf,
|
||||
});
|
||||
|
||||
if (!isLeaf) {
|
||||
visit(node.children, level + 1, nextPath);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
visit(columns, 0, []);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function hasGroupedHeader<T>(columns: TableColumnNode<T>[]) {
|
||||
return getHeaderDepth(columns) > 1;
|
||||
}
|
||||
|
||||
export function getColumnAlign<T>(column: TableColumnLeaf<T>, fallback: "left" | "center" | "right" = "center") {
|
||||
if (column.align) {
|
||||
return column.align;
|
||||
}
|
||||
|
||||
if ("numeric" in column && 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, decimalPoint?: number) {
|
||||
const parsed = parseNumber(raw);
|
||||
return parsed === null ? null : roundNumber(parsed, decimalPoint);
|
||||
}
|
||||
|
||||
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 resolveSignStyle<T>(signStyle?: SignStyle<T>, fallback?: Partial<ResolvedSignStyle>) {
|
||||
return {
|
||||
...defaultSignStyle,
|
||||
...fallback,
|
||||
...signStyle,
|
||||
valueFormatter: signStyle?.valueFormatter ?? fallback?.valueFormatter ?? defaultSignStyle.valueFormatter,
|
||||
};
|
||||
}
|
||||
|
||||
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 appendCellMarker(content: React.ReactNode, marker: string) {
|
||||
if (!marker) {
|
||||
return content;
|
||||
}
|
||||
|
||||
if (typeof content === "string" || typeof content === "number") {
|
||||
return `${content}${marker}`;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{content}
|
||||
{marker}
|
||||
</>
|
||||
);
|
||||
}
|
||||
12
src/components/table/table.config.ts
Normal file
12
src/components/table/table.config.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export const TABLE_DIMENSIONS = {
|
||||
CELL_HEIGHT: {
|
||||
DEFAULT: 26,
|
||||
COMPACT: 20,
|
||||
EXPANDED: 32,
|
||||
},
|
||||
MIN_WIDTH: {
|
||||
STANDARD: 1600,
|
||||
COMPACT: 1200,
|
||||
WIDE: 2000,
|
||||
},
|
||||
} as const;
|
||||
64
src/components/table/types.ts
Normal file
64
src/components/table/types.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
export type ValueFormatter = (v: number) => string;
|
||||
|
||||
export type TableColumnAlign = "left" | "center" | "right";
|
||||
|
||||
type TableColumnShared<T> = {
|
||||
id?: string;
|
||||
header: string;
|
||||
width?: number | string;
|
||||
align?: TableColumnAlign;
|
||||
headerClassName?: string;
|
||||
cellClassName?: string;
|
||||
render?: (row: T) => React.ReactNode;
|
||||
};
|
||||
|
||||
export type DataColumnLeaf<T> = TableColumnShared<T> & {
|
||||
key: keyof T;
|
||||
rowHead?: boolean; // 첫 컬럼 등 row header
|
||||
numeric?: boolean; // 숫자열(정렬 기본값/색상 타겟 기본값에 사용)
|
||||
children?: undefined;
|
||||
};
|
||||
|
||||
export type DataColumnGroup<T> = TableColumnShared<T> & {
|
||||
children: DataColumn<T>[];
|
||||
key?: undefined;
|
||||
rowHead?: undefined;
|
||||
numeric?: undefined;
|
||||
};
|
||||
|
||||
export type DataColumn<T> = DataColumnLeaf<T> | DataColumnGroup<T>;
|
||||
|
||||
export type SignDisplayMode = "number" | "arrow";
|
||||
|
||||
export type SignStyle<T> = {
|
||||
/** 어떤 row에 +/ -/ 0 색상표기를 적용할지 선택 */
|
||||
rowSelector: (row: T, rowIndex: number) => boolean;
|
||||
|
||||
/** 어떤 컬럼에 적용할지(미지정 시 numeric 컬럼 전체) */
|
||||
keys?: (keyof T)[];
|
||||
|
||||
/** 색상 적용 방식 */
|
||||
paint?: "text" | "cell"; // 기본 text
|
||||
|
||||
/** 표기 방식 */
|
||||
display?: SignDisplayMode; // 기본 number
|
||||
showPlus?: boolean; // number 모드에서 + 표시(기본 true)
|
||||
|
||||
/** 0 값 표기(기본 "-") */
|
||||
zeroText?: string;
|
||||
|
||||
/** arrow 모드 옵션 */
|
||||
arrowWithNumber?: boolean; // ▲ 2 처럼 (기본 false)
|
||||
|
||||
/** 값 포맷(기본: 정수면 그대로/그 외 toLocaleString) */
|
||||
valueFormatter?: (v: number) => string;
|
||||
|
||||
/** 색상 커스터마이즈(기본: + primary, - info, 0 gray) */
|
||||
positiveTextClassName?: string; // 기본 "text-primary font-semibold"
|
||||
negativeTextClassName?: string; // 기본 "text-info font-semibold"
|
||||
zeroTextClassName?: string; // 기본 "text-gray-600"
|
||||
|
||||
positiveCellBg?: string; // ex) "hsl(var(--primary) / 0.08)"
|
||||
negativeCellBg?: string; // ex) "hsl(var(--info) / 0.08)"
|
||||
zeroCellBg?: string; // ex) "hsl(var(--lg-gray-200) / 0.6)"
|
||||
};
|
||||
@@ -39,7 +39,7 @@ function DialogOverlay({
|
||||
<DialogPrimitive.Overlay
|
||||
data-slot="dialog-overlay"
|
||||
className={cn(
|
||||
"fixed inset-0 isolate z-50 bg-black/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
|
||||
"fixed inset-0 isolate z-50 bg-black-600/10 duration-100 supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
|
||||
Reference in New Issue
Block a user