UNPKG

adverich-kun-ui

Version:

Una librería de componentes Vue.js con Tailwind CSS

115 lines (114 loc) 4.03 kB
import { reactive as G, ref as A, computed as p, unref as i, watch as V } from "vue"; import { debounce as H } from "../../../../utils/utils.js"; import { getValue as E, formatValue as J } from "../../../../utils/tableFormatters.js"; function U(C, L, O, l = !1) { const c = G({ search: "", byColumn: {} }), { items: m, customFilter: K, searchableKeys: j, headers: N } = C, x = A(!1), h = (e) => { if (e == null) return ""; if (typeof e == "string") return e; if (typeof e == "number" || typeof e == "boolean") return String(e); if (e instanceof Date) return e.toISOString(); if (typeof e == "object") { if ("label" in e) return String(e.label); if ("name" in e) return String(e.name); try { return String(e); } catch { return ""; } } try { return String(e); } catch { return ""; } }, g = (e, r) => { const n = h(e).toLowerCase(), t = h(r).toLowerCase(); return t ? n ? n.includes(t) : !1 : !0; }, F = p(() => O.value ?? []), w = p(() => { const e = i(j); return Array.isArray(e) && e.length ? e : F.value.map((r) => r == null ? void 0 : r.value).filter(Boolean); }), d = A(/* @__PURE__ */ new Map()); function R() { d.value.clear(); const e = i(m); Array.isArray(e) && (e.forEach((r) => { const n = {}; w.value.forEach((t) => { const o = F.value.find((f) => f.value === t); let s, u; try { s = o ? E(o, r) : r[t]; } catch (f) { s = "", l && console.error("[useFilter] ERROR getValue", { item: r, key: t, err: f }); } try { u = o ? J(o, s) : s; } catch (f) { u = s, l && console.error("[useFilter] ERROR formatValue", { header: o, raw: s, err: f }); } n[t] = h(u).toLowerCase(); }), d.value.set(r, n); }), l && console.log("[useFilter] Cache reconstruida", d.value)); } V(() => i(m), () => R(), { immediate: !0 }), V(() => { var e; return (e = i(m)) == null ? void 0 : e.length; }, () => R()); const S = (e, r, n) => { const t = D(e, r), o = i(K); if (typeof o == "function") { const a = F.value.find((y) => (y == null ? void 0 : y.value) === r); try { return o(e, r, n, a); } catch (y) { return l && console.error("[matchesFilter] ERROR customFilter", { item: e, key: r, value: n, header: a, err: y }), !1; } } const s = i(C.filters) ?? C.filters, u = Array.isArray(s) ? s.find((a) => a.value === r) : void 0, f = (u == null ? void 0 : u["item-value"]) ?? "id", b = (a) => a != null && typeof a == "object" ? a[f] : a; return Array.isArray(n) ? n.some((a) => g(t, b(a))) : g(t, b(n)); }, I = p(() => { const e = i(m); return Array.isArray(e) ? e.filter((r) => { const n = c.search; if (n && !w.value.some((s) => S(r, s, n))) return !1; for (const t in c.byColumn) { const o = c.byColumn[t]; if (o != null && o !== "" && !S(r, t, o)) return !1; } return !0; }) : []; }), q = H((e) => { c.search = h(e).toLowerCase(), l && console.log("[setSearch]", c.search); }, i(L) ?? 150), B = (e, r) => { c.byColumn[e] = r, l && console.log("[setColumnFilter]", e, r); }, M = (e) => { for (const r in e) B(r, e[r]); l && console.log("[applyColumnFilters]", e); }, z = () => { c.search = "", c.byColumn = {}, l && console.log("[clearFilters] filtros reseteados"); }, D = (e, r) => { const n = F.value.find((t) => (t == null ? void 0 : t.value) === r); try { return n ? E(n, e) : e[r]; } catch (t) { return l && console.error("[useFilter] ERROR getRawValue", { item: e, key: r, err: t }), null; } }; return { modalFilter: x, appliedFilters: c, filteredItems: I, setSearch: q, applyColumnFilters: M, clearFilters: z, // 👇 Exponemos también por si querés refrescar manualmente refreshCache: R }; } export { U as default };