260202_01_ENH: route 구조 초창기 기획으로 다시 변경 및 Category 별 하위 페이지에 샘플 데이터 모두 적용
This commit is contained in:
@@ -2,32 +2,21 @@
|
||||
|
||||
import { useMemo } from "react";
|
||||
import type React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import type { DataColumn, SignDisplayMode, SignStyle } from "../types";
|
||||
import SectionTitle from "../SectionTitle";
|
||||
|
||||
type Props<T extends Record<string, unknown>> = {
|
||||
columns: DataColumn<T>[];
|
||||
rows: T[];
|
||||
title?: string;
|
||||
className?: string;
|
||||
wrapperClassName?: string;
|
||||
signStyle?: SignStyle<T>;
|
||||
fillGreaterThan5?: boolean;
|
||||
};
|
||||
|
||||
const parseSignedNumber = (raw: unknown): number | null => {
|
||||
if (raw === null || raw === undefined) return null;
|
||||
if (typeof raw === "number") return Number.isFinite(raw) ? raw : null;
|
||||
if (typeof raw === "string") {
|
||||
const s = raw
|
||||
.trim()
|
||||
.replaceAll(",", "")
|
||||
.replaceAll("−", "-") // 유니코드 마이너스
|
||||
.replaceAll("%", "");
|
||||
if (s === "" || s === "-") return null;
|
||||
const n = Number(s);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
// parseSignedNumber는 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
const defaultFmt = (v: number) =>
|
||||
Number.isInteger(v) ? String(v) : v.toLocaleString();
|
||||
@@ -53,20 +42,22 @@ const signText = (
|
||||
};
|
||||
|
||||
const DataTable = <T extends Record<string, unknown>>({
|
||||
columns,
|
||||
rows,
|
||||
className,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
}: Props<T>) => {
|
||||
columns,
|
||||
rows,
|
||||
title,
|
||||
className,
|
||||
wrapperClassName,
|
||||
signStyle,
|
||||
fillGreaterThan5 = false,
|
||||
}: Props<T>) => {
|
||||
const hasGroup = columns.some((c) => !!c.group) || columns.some((c) => c.headRowSpan === 2);
|
||||
|
||||
// group header(1행): rowspan=2 컬럼(th) + group label(colspan)
|
||||
const groupCells = useMemo(() => {
|
||||
type GroupCell =
|
||||
| { kind: "rowspan"; label: string; colSpan: 1; rowSpan: 2; className?: string }
|
||||
| { kind: "group"; label: string; colSpan: number; rowSpan: 1; className?: string }
|
||||
| { kind: "blank"; label: string; colSpan: 1; rowSpan: 1; className?: string };
|
||||
| { kind: "rowspan"; label: string; colSpan: 1; rowSpan: 2; className?: string }
|
||||
| { kind: "group"; label: string; colSpan: number; rowSpan: 1; className?: string }
|
||||
| { kind: "blank"; label: string; colSpan: 1; rowSpan: 1; className?: string };
|
||||
|
||||
if (!hasGroup) return [] as GroupCell[];
|
||||
|
||||
@@ -148,6 +139,9 @@ const DataTable = <T extends Record<string, unknown>>({
|
||||
|
||||
return (
|
||||
<div className={cn("w-full overflow-x-auto", wrapperClassName)}>
|
||||
{title && (
|
||||
<SectionTitle title={title} />
|
||||
)}
|
||||
<table className={cn("lg-table", className)}>
|
||||
<thead>
|
||||
{hasGroup && (
|
||||
@@ -178,6 +172,7 @@ const DataTable = <T extends Record<string, unknown>>({
|
||||
<tbody>
|
||||
{rows.map((row, rIdx) => {
|
||||
const signRow = isSignRow(row, rIdx);
|
||||
const total = parseNumber(row.total);
|
||||
|
||||
return (
|
||||
<tr key={rIdx}>
|
||||
@@ -191,7 +186,7 @@ const DataTable = <T extends Record<string, unknown>>({
|
||||
const applySign = signRow && isTargetKey(c.key) && !c.rowHead;
|
||||
|
||||
if (applySign) {
|
||||
const n = parseSignedNumber(raw);
|
||||
const n = parseNumber(raw);
|
||||
const displayText =
|
||||
n === null ? "-" : signText(n, display, showPlus, arrowWithNumber, fmt, zeroText);
|
||||
|
||||
@@ -230,8 +225,13 @@ const DataTable = <T extends Record<string, unknown>>({
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const numContent = c.numeric ? parseNumber(content) ?? 0 : 0;
|
||||
const isFill = fillGreaterThan5 && total && numContent > total + 5;
|
||||
return (
|
||||
<td key={String(c.key)} style={{ textAlign: align }} className={c.cellClassName}>
|
||||
<td key={String(c.key)} style={{ textAlign: align }} className={
|
||||
cn(c.cellClassName, isFill? "bg-red-200" :"")
|
||||
}>
|
||||
{content}
|
||||
</td>
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, parseNumber } from "@/lib/utils";
|
||||
import {GaugeColumn} from "../types";
|
||||
|
||||
type Props<T extends Record<string, unknown>> = {
|
||||
@@ -24,14 +24,7 @@ type Props<T extends Record<string, unknown>> = {
|
||||
valueFormatter?: (v: number) => string;
|
||||
};
|
||||
|
||||
const num = (v: unknown) => {
|
||||
if (typeof v === "number" && Number.isFinite(v)) return v;
|
||||
if (typeof v === "string") {
|
||||
const n = Number(v.replaceAll(",", "").replaceAll("%", "").trim());
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
// num은 utils.ts의 parseNumber로 통합됨
|
||||
|
||||
const GaugeTable = <T extends Record<string, unknown>>({
|
||||
columns,
|
||||
@@ -130,7 +123,7 @@ const GaugeTable = <T extends Record<string, unknown>>({
|
||||
}
|
||||
|
||||
const raw = row[c.key];
|
||||
const v = num(raw);
|
||||
const v = parseNumber(raw);
|
||||
const max = c.max ?? 100;
|
||||
const ratio = v === null ? 0 : clamp01((v * scale) / max);
|
||||
const pct = ratio * 100;
|
||||
|
||||
Reference in New Issue
Block a user