@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
86 lines (85 loc) • 2.22 kB
JavaScript
import { jsx as k } from "react/jsx-runtime";
import { useQuery as A } from "@tanstack/react-query";
import { u as D, g as Q, a as $ } from "../../../index-CSWGJT-v.js";
import { useState as t, useMemo as G, useCallback as c } from "react";
import { TableContext as U } from "./table.context.js";
import { DEFAULT_PAGE_SIZE as W } from "../constants/pagination.js";
const X = ({
children: p,
id: r,
data: g = [],
columns: S = [],
totalItems: y,
isPaginationEnabled: b,
queryOptions: M = {},
fetchData: m
}) => {
const [f, d] = t([]), [C, P] = t(!0), [i, h] = t(0), [a, w] = t(), [s, F] = t(W), [l, O] = t(y), T = G(
() => Math.ceil(l / s),
[l, s]
), [n, L] = t({}), I = () => {
const e = typeof r == "string" || typeof r == "number" ? [r] : r;
return b && e.push(i, s), a && e.push(a), Object.entries(n).forEach(([o, u]) => {
e.push(`${o}:${u.join(",")}`);
}), e;
}, { data: R, isLoading: j, isFetching: x } = A({
queryKey: I(),
refetchOnMount: !1,
refetchOnWindowFocus: !1,
initialData: g,
queryFn: async () => m ? m({
page: Math.max(i + 1, 1),
pageSize: s,
termOfSearch: a,
...Object.keys(n).length > 0 ? n : {}
}).then(({ data: e, totalItemsCount: o }) => (P(!1), o && O(o), e)) : g ?? [],
...M
}), E = c((e) => {
w(e), h(0);
}, []), K = c((e, o) => {
const u = e.toLowerCase().replace(/\s+/g, "_");
L((_) => ({
..._,
[u]: o
}));
}, []), q = c((e) => h(e), []), v = c(
(e) => F(e),
[]
), z = D({
data: R,
columns: S,
state: {
sorting: f
},
onSortingChange: d,
getCoreRowModel: $(),
getSortedRowModel: Q()
});
return /* @__PURE__ */ k(
U.Provider,
{
value: {
sortedData: f,
table: z,
tableFetching: x,
tableLoading: j,
totalItems: l,
termOfSearch: a,
page: i,
multiselectSelected: n,
pageSize: s,
totalPages: T,
isFirstLoad: C,
handlePage: q,
onPageSize: v,
onChangeTermOfSearch: E,
onSorting: d,
onSelectMultiselect: K
},
children: p
}
);
};
export {
X as TableProvider
};