@trail-ui/react
Version:
104 lines (101 loc) • 3.26 kB
JavaScript
import {
useFocusHandler
} from "./chunk-F5W73ZFA.mjs";
import {
useTableContext
} from "./chunk-A4IWBDSQ.mjs";
// src/editable-table/table-footer/footer-cell.tsx
import { flexRender } from "@tanstack/react-table";
import { useEffect, useRef } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function EditableTableFooterCell({
header
}) {
var _a;
const cellRef = useRef(null);
const { table } = useTableContext();
const { isCurrentCellFocused, focusCurrentCell } = useFocusHandler({
cellType: "footerCell" /* footerCell */,
columnIndex: header.column.getIndex(),
groupId: null,
rowId: null,
rowIndex: -1
});
const colId = header.column.id;
const colDef = header.column.columnDef;
const colMeta = (_a = header.column.columnDef.meta) != null ? _a : {};
useEffect(() => {
var _a2;
if (isCurrentCellFocused) {
(_a2 = cellRef.current) == null ? void 0 : _a2.focus();
}
}, [isCurrentCellFocused]);
function getCellValue() {
var _a2;
if (colDef.footer) {
return flexRender(colDef.footer, header.getContext());
}
if (colMeta.footerType === "addition") {
const total = (_a2 = table.getFilteredRowModel().rows.reduce((sum, row) => {
const value = Number.parseFloat(row.getValue(colId));
return isNaN(value) ? sum : sum + value;
}, 0)) == null ? void 0 : _a2.toFixed(2);
return /* @__PURE__ */ jsxs(Fragment, { children: [
total.toString(),
colId === "sum" ? "%" : ""
] });
}
if (colMeta.footerType === "average") {
const { total, count } = table.getFilteredRowModel().rows.reduce(
(acc, row) => {
const value = Number.parseFloat(row.getValue(colId));
if (!Number.isNaN(value)) {
acc.total += value;
acc.count += 1;
}
return acc;
},
{ total: 0, count: 0 }
);
const average = count > 0 ? (total / count).toFixed(2) : "0.00";
return /* @__PURE__ */ jsxs(Fragment, { children: [
average,
colId === "sum" ? "%" : ""
] });
}
if (colMeta.footerType === "isTotalField") {
return "Total";
}
}
const Element = colMeta.footerType === "isTotalField" ? "th" : "td";
return /* @__PURE__ */ jsx(
Element,
{
...Element === "th" ? { scope: "row" } : {},
className: "border-r border-neutral-200 p-2",
style: {
minWidth: header.column.columnDef.size === 100 ? "auto" : header.column.columnDef.size,
maxWidth: header.column.columnDef.size === 100 ? "auto" : header.column.columnDef.size,
width: header.column.columnDef.size === 100 ? "auto" : header.column.columnDef.size
},
children: /* @__PURE__ */ jsx(
"div",
{
ref: cellRef,
className: `th-content flex min-h-5 items-center justify-between rounded px-2 text-sm font-semibold outline-purple-600 focus:outline focus:outline-2`,
tabIndex: isCurrentCellFocused ? 0 : -1,
onClick: () => {
focusCurrentCell();
},
onKeyDown: () => {
},
children: getCellValue()
}
)
},
colId
);
}
export {
EditableTableFooterCell
};