feat: enhance Excel table header and register styling with Navy fill, white bold font, borders, and AutoFilter

This commit is contained in:
Atharv1705
2026-11-16 10:57:16 +05:30
parent 5d98aa57cb
commit 13fd84a53c
6 changed files with 226 additions and 12 deletions
@@ -1,14 +1,20 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { ColumnDef } from "@tanstack/react-table";
import * as XLSX from "xlsx";
import XLSX from "xlsx-js-style";
import { DataTable } from "./DataTable";
vi.mock("xlsx", async () => {
const actual = await vi.importActual<typeof import("xlsx")>("xlsx");
vi.mock("xlsx-js-style", async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
const base = ((actual as { default?: Record<string, unknown> }).default ?? actual) as Record<string, unknown>;
const writeFileMock = vi.fn();
return {
...actual,
writeFile: vi.fn(),
...base,
writeFile: writeFileMock,
default: {
...base,
writeFile: writeFileMock,
},
};
});
@@ -14,7 +14,7 @@ import {
type Table as TanStackTable,
} from "@tanstack/react-table";
import { useState } from "react";
import * as XLSX from "xlsx";
import XLSX from "xlsx-js-style";
import {
ChevronUp,
ChevronDown,
@@ -69,6 +69,75 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
const ws = XLSX.utils.json_to_sheet(exportRows);
// Apply rich executive styling to table header row (Navy fill, bold white font, borders)
for (let c = 0; c < visibleCols.length; c++) {
const headerAddr = XLSX.utils.encode_cell({ r: 0, c });
if (ws[headerAddr]) {
ws[headerAddr].s = {
fill: { fgColor: { rgb: "1E3A8A" } }, // Poona Hospital Deep Navy
font: { name: "Calibri", sz: 11, bold: true, color: { rgb: "FFFFFF" } },
alignment: { horizontal: "center", vertical: "center", wrapText: true },
border: {
top: { style: "thin", color: { rgb: "334155" } },
bottom: { style: "medium", color: { rgb: "0F172A" } },
left: { style: "thin", color: { rgb: "334155" } },
right: { style: "thin", color: { rgb: "334155" } },
},
};
}
}
// Apply zebra striping, borders, and column-aware alignment to data rows
for (let r = 0; r < rows.length; r++) {
const isOdd = r % 2 === 1;
for (let c = 0; c < visibleCols.length; c++) {
const cellAddr = XLSX.utils.encode_cell({ r: r + 1, c });
if (!ws[cellAddr]) continue;
const col = visibleCols[c];
const headerLabel = (typeof col.columnDef.header === "string" ? col.columnDef.header : col.id).toLowerCase();
const val = rows[r].getValue(col.id);
let horizontalAlign: "left" | "center" | "right" = "left";
if (
typeof val === "number" ||
headerLabel.includes("cost") ||
headerLabel.includes("value") ||
headerLabel.includes("amount") ||
headerLabel.includes("price") ||
headerLabel.includes("minutes") ||
headerLabel.includes("downtime") ||
headerLabel.includes("total")
) {
horizontalAlign = "right";
} else if (
headerLabel.includes("code") ||
headerLabel.includes("date") ||
headerLabel.includes("status") ||
headerLabel.includes("priority") ||
headerLabel.includes("criticality") ||
headerLabel.includes("serial") ||
headerLabel.includes("mode") ||
headerLabel.includes("id") ||
typeof val === "boolean"
) {
horizontalAlign = "center";
}
ws[cellAddr].s = {
fill: { fgColor: { rgb: isOdd ? "F8FAFC" : "FFFFFF" } },
font: { name: "Calibri", sz: 10, color: { rgb: "0F172A" } },
alignment: { horizontal: horizontalAlign, vertical: "center" },
border: {
top: { style: "thin", color: { rgb: "E2E8F0" } },
bottom: { style: "thin", color: { rgb: "E2E8F0" } },
left: { style: "thin", color: { rgb: "E2E8F0" } },
right: { style: "thin", color: { rgb: "E2E8F0" } },
},
};
}
}
// Auto-fit column widths with generous padding so text/codes are never truncated
const colWidths = visibleCols.map((col) => {
const headerLabel =
@@ -85,9 +154,9 @@ function exportToExcel<TData>(table: TanStackTable<TData>, filename: string) {
});
ws["!cols"] = colWidths;
// Add comfortable row heights: header row taller (26pt), data rows spacious (20pt)
// Add comfortable row heights: header row taller (28pt), data rows spacious (20pt)
ws["!rows"] = [
{ hpt: 26 },
{ hpt: 28 },
...rows.map(() => ({ hpt: 20 })),
];