UNPKG

adverich-kun-ui

Version:

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

64 lines (63 loc) 1.98 kB
import { ref as p, computed as r, watch as M, nextTick as T } from "vue"; function k({ items: S, search: s, searchableKeys: m = [], itemsPerIntersection: i = 20 }) { const l = p(1), g = p(null), I = r(() => Array.isArray(m) ? m.filter((e) => e != null && e !== "").map((e) => String(e)) : []), w = r( () => typeof i == "number" && i > 0 ? i : 20 ), u = r(() => { const e = S.value || [], t = s.value == null ? "" : String(s.value).trim().toLowerCase(); if (!t) return e; const n = I.value; return e.filter((o) => n.length > 0 ? n.some((v) => { const d = b(o, v); return d == null ? !1 : a(d, 1).some((L) => L.toLowerCase().includes(t)); }) : a(o, 1).some((v) => v.toLowerCase().includes(t))); }), B = (e) => e === null || typeof e != "object" && typeof e != "function", a = (e, t = 1) => e == null ? [] : B(e) ? [String(e)] : Array.isArray(e) ? t <= 0 ? [] : e.flatMap((n) => a(n, t - 1)) : t <= 0 ? [] : Object.values(e).flatMap((n) => a(n, t - 1)), b = (e, t) => { if (t) return String(t).split(".").reduce((n, o) => n != null ? n[o] : void 0, e); }, x = r(() => u.value.length), c = r( () => u.value.slice( 0, l.value * w.value ) ), f = r( () => c.value.length >= u.value.length ), A = r(() => l.value === 1); function y() { f.value || l.value++; } function h() { l.value = 1; } M( () => s.value, () => { h(); }, { flush: "pre" } ); async function C(e) { if (typeof e != "number" || e < 0) return; g.value = e; const t = u.value.length - 1, n = Math.min(e, t); for (; n >= c.value.length && !f.value; ) y(), await T(); } return { visibleItems: c, loadNextBatch: y, resetCurrentBatchStep: h, lastBatchReached: f, isFirstBatch: A, totalItems: x, setScrollToIndex: C, scrollToIndex: g // opcional, si lo necesitás desde el componente padre }; } export { k as useKunInfiniteScroll };