@payfit/unity-components
Version:
126 lines (125 loc) • 5.18 kB
JavaScript
import { CircularIconButton as e } from "../../../../core/components/icon-button/CircularIconButton.js";
import { Table as t, TableRoot as n } from "../../../../core/components/table/Table.js";
import { TableBody as r } from "../../../../core/components/table/parts/TableBody.js";
import { TableColumnHeader as i } from "../../../../core/components/table/parts/TableColumnHeader.js";
import { TableEmptyStateError as a, TableEmptyStateLoading as o, TableEmptyStateNoData as s, TableNoSearchResults as c } from "../../../../core/components/table/parts/TableEmptyState.js";
import { TableHeader as l } from "../../../../core/components/table/parts/TableHeader.js";
import { TablePagination as u } from "../../../../core/components/table/parts/TablePagination.js";
import { getPaginationTable as d } from "./DataTablePagination.js";
import { ColumnSortHeader as f, isSortableHeader as p } from "./parts/ColumnSortHeader.js";
import { useMemo as m, useRef as h } from "react";
import { Fragment as g, jsx as _, jsxs as v } from "react/jsx-runtime";
import { useId as y } from "react-aria/useId";
import { flexRender as b } from "@tanstack/react-table";
import { useSelector as x } from "@tanstack/react-store";
//#region src/modules/tanstack-table/components/data-table/DataTable.tsx
var S = ({ children: t, columnNameId: n, helperText: r }) => r === void 0 ? /* @__PURE__ */ _(g, { children: t }) : /* @__PURE__ */ v("div", {
className: "uy:flex uy:gap-50 uy:items-center",
children: [t, /* @__PURE__ */ _(e, {
"aria-labelledby": n,
icon: "QuestionOutlined",
title: r,
color: "content.neutral.lowest"
})]
}), C = ({ table: e, tableRef: t, pagination: n, onPageChange: r, onPageHover: i, onPageSizeChange: a }) => {
let o = e.atoms.pagination, { pageIndex: s, pageSize: c } = x(m(() => ({
get: () => o.get(),
subscribe: (e) => o.subscribe((n) => {
t.current?.scrollToTop(), e(n);
})
}), [o, t]));
return e.getPageCount() <= 0 ? null : /* @__PURE__ */ _(u, {
pageCount: e.getPageCount(),
rowCount: e.getRowCount(),
currentPage: s + 1,
rowsPerPage: c,
pageSizeOptions: n.pageSizeOptions,
onPageChange: (t, n, i) => {
e.setPageIndex(t - 1), r?.(t - 1, n, i);
},
onPageHover: (e) => {
i?.(e);
},
onPageSizeChange: (t) => {
e.setPageSize(t), a?.(t);
},
itemLabel: n.labels?.itemLabel
});
};
function w({ table: e, isLoading: u, error: m, emptyState: g, loadingState: x, errorState: w, pagination: T, minRows: E, maxRows: D, onPageChange: O, onPageHover: k, onPageSizeChange: A, children: j, enableVirtualization: M, estimatedRowHeight: N, overscan: P, onErrorButtonPress: F, onNoDataButtonPress: I, noDataDescription: L, onNoSearchResultsButtonPress: R, ...z }) {
let B = h(null), V = y(), H = d(e), U = {
label: z.label,
description: z.description,
isHorizontalScrollEnabled: z.isHorizontalScrollEnabled,
layout: z.layout
}, W = () => {
if (u) return x ?? /* @__PURE__ */ _(o, {});
if (m) return w ?? /* @__PURE__ */ _(a, { onButtonPress: F });
}, G = () => {
let t = e.state, n = t.globalFilter !== void 0 && t.globalFilter !== "", r = t.columnFilters !== void 0 && t.columnFilters.length > 0;
return n || r;
}, K = () => u || m ? W() : e.getRowModel().rows.length === 0 ? G() && R ? /* @__PURE__ */ _(c, { onButtonPress: R }) : g ?? /* @__PURE__ */ _(s, {
onButtonPress: I,
description: L
}) : g;
return /* @__PURE__ */ v(n, {
minRows: E,
maxRows: D,
children: [/* @__PURE__ */ v(t, {
ref: B,
...U,
enableVirtualization: M,
estimatedRowHeight: N,
overscan: P,
children: [/* @__PURE__ */ _(l, { children: e.getHeaderGroups().map((t) => {
let n = 0;
return /* @__PURE__ */ _("tr", { children: t.headers.map((r) => {
let a = n;
if (n += r.colSpan, r.rowSpan === 0) return null;
let o = !r.isPlaceholder || r.rowSpan > 1, s = `${V}-column-name-${r.column.id}`, c = r.column.columnDef.meta, l = p(r);
return /* @__PURE__ */ _(i, {
colIndex: a,
rowIndex: t.depth,
colSpan: r.colSpan,
rowSpan: r.rowSpan,
scope: r.colSpan > 1 ? "colgroup" : "col",
isFocusable: c?.isFocusable,
className: c?.headerClassName,
children: o && e.getRowModel().rows.length > 0 && l ? /* @__PURE__ */ _(f, {
header: r,
children: /* @__PURE__ */ _(S, {
columnNameId: s,
helperText: c?.helperText,
children: /* @__PURE__ */ _("span", {
id: s,
children: b(r.column.columnDef.header, r.getContext())
})
})
}) : o ? /* @__PURE__ */ _(S, {
columnNameId: s,
helperText: c?.helperText,
children: /* @__PURE__ */ _("span", {
id: s,
children: b(r.column.columnDef.header, r.getContext())
})
}) : null
}, r.id);
}) }, t.id);
}) }), /* @__PURE__ */ _(r, {
renderEmptyState: K,
renderDataState: W,
children: !u && !m && e.getRowModel().rows.map(j)
})]
}), H !== void 0 && /* @__PURE__ */ _(C, {
table: H,
tableRef: B,
pagination: T ?? {},
onPageChange: O,
onPageHover: k,
onPageSizeChange: A
})]
});
}
w.displayName = "DataTable";
//#endregion
export { w as DataTable };