uikit/dist/table.js
2026-06-16 15:59:27 +03:30

383 lines
10 KiB
JavaScript

import { jsx as r, jsxs as X, Fragment as ne } from "react/jsx-runtime";
import { VStack as oe, Box as _, Tbody as re, Tr as H, Td as F, Text as R, Thead as le, Th as ce, Table as se, Checkbox as W, HStack as Y, Spacer as ie } from "@chakra-ui/react";
import { P as ue } from "./chunks/Pagination-BFHGSAKW.js";
import { toFaDigits as q } from "./utils.js";
import { useReactTable as ae, getCoreRowModel as de, flexRender as J } from "@tanstack/react-table";
import { createContext as me, useMemo as P, useCallback as p, useState as fe, useRef as ge, useEffect as he, useContext as be } from "react";
const K = me(null);
function C() {
const e = be(K);
if (!e)
throw new Error("DataTable components must be used inside <DataTable>");
return e;
}
function xe({
children: e,
data: t = [],
columns: o = [],
page: s = 1,
size: c = 10,
pagination: l,
onPageChange: a,
getRowId: g = (d) => String(d == null ? void 0 : d.id),
enableSelection: b = !1,
showIndex: S = !0,
defaultSelectedIds: L = [],
onSelectionChange: k,
...O
}) {
const d = Number(s) || 1, h = Number(c) || 10, y = (l == null ? void 0 : l.total) ?? (l == null ? void 0 : l.totalCount) ?? 0, v = (l == null ? void 0 : l.total_pages) ?? (l == null ? void 0 : l.totalPages) ?? (y > 0 ? Math.ceil(y / h) : 0), z = P(() => ({
pageIndex: Math.max(d - 1, 0),
pageSize: h
}), [d, h]), Q = p(
(n) => {
const u = (typeof n == "function" ? n(z) : n).pageIndex + 1;
u !== d && (a == null || a(u));
},
[z, d, a]
), [A, U] = fe(L), m = p(
(n) => {
U((i) => {
const u = typeof n == "function" ? n(i) : n;
return k == null || k(u), u;
});
},
[k]
), M = ge({
page: d,
size: h
});
he(() => {
if (!b)
return;
const n = M.current, i = n.page !== d || n.size !== h;
M.current = {
page: d,
size: h
}, i && m([]);
}, [b, d, h, m]);
const f = P(() => t.map((n) => g(n)).filter((n) => n != null && n !== ""), [t, g]), T = P(() => A.filter((n) => f.includes(n)), [f, A]), $ = f.length > 0 && T.length === f.length, N = T.length > 0 && T.length < f.length, V = p(() => {
m([]);
}, [m]), B = p(
(n) => {
m((i) => f.includes(n) ? i.includes(n) ? i.filter((u) => u !== n) : [...i, n] : i);
},
[f, m]
), Z = B, I = p(() => {
m(f);
}, [f, m]), w = p(() => {
m([]);
}, [m]), j = p(
(n) => {
if (n) {
I();
return;
}
w();
},
[I, w]
), E = P(() => {
const n = [];
return b && n.push({
id: "__selection",
header: ({ table: i }) => {
const u = i.options.meta;
return /* @__PURE__ */ r(
W,
{
isChecked: u.isAllSelected,
isIndeterminate: u.isIndeterminate,
onChange: (x) => u.toggleSelectAll(x.target.checked),
colorScheme: "purple",
size: "md"
}
);
},
cell: ({ row: i, table: u }) => {
const x = u.options.meta, D = x.getRowId(i.original);
return /* @__PURE__ */ r(
W,
{
isChecked: x.selectedIds.includes(D),
onChange: () => x.onSelectionChangedId(D),
colorScheme: "purple",
size: "md"
}
);
},
meta: { isCentered: !0 }
}), S && n.push({
id: "__index",
header: "ردیف",
cell: ({ row: i, table: u }) => {
const { pageIndex: x, pageSize: D } = u.getState().pagination, te = x * D + i.index + 1;
return /* @__PURE__ */ r(R, { fontWeight: "bold", sx: { fontVariantNumeric: "normal" }, children: q(te) });
},
meta: { isCentered: !0 }
}), [...n, ...o];
}, [o, b, S]), ee = {
table: ae({
data: t,
columns: E,
getCoreRowModel: de(),
getRowId: g,
manualPagination: !0,
pageCount: v,
rowCount: y,
state: {
pagination: z
},
onPaginationChange: Q,
meta: {
selectedIds: A,
onSelectionChangedId: B,
getRowId: g,
allPageIds: f,
pageSelectedIds: T,
isAllSelected: $,
isIndeterminate: N,
selectAll: I,
unselectAll: w,
toggleSelectAll: j,
clearSelection: V,
totalCount: y,
totalPages: v
}
}),
data: t,
columns: E,
page: d,
size: h,
pagination: l,
onPageChange: a,
paginationState: z,
totalCount: y,
totalPages: v,
getRowId: g,
enableSelection: b,
selectedIds: A,
setSelectedIds: m,
clearSelection: V,
allPageIds: f,
pageSelectedIds: T,
isAllSelected: $,
isIndeterminate: N,
selectAll: I,
unselectAll: w,
toggleSelectAll: j,
onSelectionChangedId: B,
toggleRowSelection: Z
};
return /* @__PURE__ */ r(K.Provider, { value: ee, children: /* @__PURE__ */ r(oe, { spacing: 4, w: "full", ...O, children: e }) });
}
function pe({ children: e, ...t }) {
return /* @__PURE__ */ r(Y, { w: "full", px: 2, spacing: 4, ...t, children: e });
}
function Ce({ children: e, ...t }) {
return /* @__PURE__ */ r(R, { fontSize: "lg", textAlign: "start", fontWeight: "bold", ...t, children: e });
}
function Se(e) {
return /* @__PURE__ */ r(ie, { ...e });
}
function ye({ children: e, ...t }) {
const o = C();
return /* @__PURE__ */ r(Y, { spacing: 4, ...t, children: typeof e == "function" ? e(o) : e });
}
function Te({ children: e }) {
const t = C();
return t.selectedIds.length === 0 ? null : typeof e == "function" ? e(t) : e;
}
function ke({ children: e = "انتخاب همه", ...t }) {
const {
data: o,
enableSelection: s,
isAllSelected: c,
isIndeterminate: l,
toggleSelectAll: a
} = C();
return !s || o.length === 0 ? null : /* @__PURE__ */ r(
W,
{
isChecked: c,
isIndeterminate: l,
onChange: (g) => a(g.target.checked),
colorScheme: "purple",
size: "lg",
...t,
children: e
}
);
}
function ze({ children: e, ...t }) {
const { selectedIds: o } = C();
return o.length === 0 ? null : typeof e == "function" ? e({ count: o.length, selectedIds: o }) : /* @__PURE__ */ X(R, { fontSize: "sm", color: "gray.600", ...t, children: [
q(o.length),
" مورد انتخاب شده"
] });
}
function Ae({ children: e, ...t }) {
return /* @__PURE__ */ r(
_,
{
display: "flex",
flexDirection: "column",
w: "full",
h: "100%",
minH: 0,
gap: 4,
...t,
children: e
}
);
}
function Ie({
children: e,
minW: t = "1200px",
heightOffset: o = 355,
containerProps: s,
tableProps: c
}) {
return /* @__PURE__ */ r(
_,
{
flex: "1",
minH: `calc(100vh - ${o}px)`,
maxH: `calc(100vh - ${o}px)`,
overflowX: "auto",
overflowY: "auto",
bg: "white",
borderRadius: "lg",
borderWidth: "1px",
borderColor: "gray.200",
sx: {
"::-webkit-scrollbar": {
width: "6px",
height: "6px"
},
"::-webkit-scrollbar-thumb": {
background: "#CBD5E0",
borderRadius: "8px"
},
"::-webkit-scrollbar-track": {
background: "transparent"
}
},
...s,
children: /* @__PURE__ */ r(
se,
{
variant: "simple",
minW: t,
sx: {
"th, td": {
borderColor: "gray.200",
whiteSpace: "nowrap",
px: 2
},
"tbody tr:last-child td": {
borderBottom: "none"
}
},
...c,
children: e ?? /* @__PURE__ */ X(ne, { children: [
/* @__PURE__ */ r(G.Head, {}),
/* @__PURE__ */ r(G.Body, {})
] })
}
)
}
);
}
function we() {
const { table: e } = C();
return /* @__PURE__ */ r(le, { position: "sticky", top: 0, bg: "white", zIndex: 1, children: e.getHeaderGroups().map((t) => /* @__PURE__ */ r(H, { borderBottomWidth: "1px", borderColor: "gray.200", children: t.headers.map((o) => {
var s;
return /* @__PURE__ */ r(
ce,
{
py: 4,
borderRightWidth: "1px",
borderRightColor: "gray.200",
fontSize: "md",
color: "gray.600",
textAlign: (s = o.column.columnDef.meta) != null && s.isCentered ? "center" : "start",
verticalAlign: "middle",
textTransform: "none",
children: o.isPlaceholder ? null : J(
o.column.columnDef.header,
o.getContext()
)
},
o.id
);
}) }, t.id)) });
}
function De({ emptyText: e = "موردی وجود ندارد" }) {
const { table: t, columns: o } = C(), s = t.getRowModel().rows;
return /* @__PURE__ */ r(re, { children: s.length > 0 ? s.map((c) => /* @__PURE__ */ r(
H,
{
borderBottomWidth: "1px",
borderColor: "gray.200",
_hover: { bg: "blue.50" },
transition: "0.2s",
children: c.getVisibleCells().map((l) => {
var a;
return /* @__PURE__ */ r(
F,
{
py: 3,
fontSize: "md",
color: "gray.700",
borderRightWidth: "1px",
borderRightColor: "gray.200",
textAlign: (a = l.column.columnDef.meta) != null && a.isCentered ? "center" : "start",
verticalAlign: "middle",
children: l.column.columnDef.cell ? J(l.column.columnDef.cell, l.getContext()) : l.getValue()
},
l.id
);
})
},
c.id
)) : /* @__PURE__ */ r(H, { children: /* @__PURE__ */ r(F, { colSpan: o.length, py: 8, textAlign: "center", children: /* @__PURE__ */ r(R, { color: "gray.500", children: e }) }) }) });
}
function Pe({
pagination: e,
onPageChange: t
}) {
const o = C(), { table: s } = o, c = e ?? o.pagination, l = (c == null ? void 0 : c.total) ?? (c == null ? void 0 : c.totalCount) ?? o.totalCount ?? 0, a = (c == null ? void 0 : c.total_pages) ?? (c == null ? void 0 : c.totalPages) ?? o.totalPages ?? s.getPageCount() ?? 0, g = s.getState().pagination.pageIndex + 1, b = (S) => {
if (t) {
t(S);
return;
}
s.setPageIndex(S - 1);
};
return !o.onPageChange && !t || a <= 0 ? null : /* @__PURE__ */ r(_, { mt: 1, flexShrink: 0, children: /* @__PURE__ */ r(
ue,
{
currentPage: g,
onPageChange: b,
totalCount: l,
totalPages: a
}
) });
}
const G = Object.assign(xe, {
Topbar: pe,
Title: Ce,
Spacer: Se,
Actions: ye,
SelectionActions: Te,
SelectAll: ke,
SelectedCount: ze,
Content: Ae,
Table: Ie,
Head: we,
Body: De,
Pagination: Pe
});
export {
G as DataTable
};