adverich-kun-ui
Version:
Una librería de componentes Vue.js con Tailwind CSS
48 lines (47 loc) • 2.14 kB
JavaScript
import { reactive as A, watch as c, computed as f } from "vue";
function D(v, o, l) {
const { page: p, itemsPerPage: g, sortBy: P, mutliSort: B } = v, b = (e) => typeof e == "string" ? [{ key: e, order: "asc" }] : Array.isArray(e) ? e.map((u) => typeof u == "string" ? { key: u, order: "asc" } : u) : [], t = A({
page: p.value,
itemsPerPage: g.value,
sortBy: b(P.value)
});
c(() => p.value, (e) => t.page = e), c(() => g.value, (e) => t.itemsPerPage = e), c(() => t.sortBy, (e) => o == null ? void 0 : o("update:sortBy", e)), c(() => t.page, (e) => o == null ? void 0 : o("update:page", e)), c(() => t.itemsPerPage, (e) => o == null ? void 0 : o("update:itemsPerPage", e));
const y = f(() => (t.page - 1) * t.itemsPerPage), m = f(() => y.value + t.itemsPerPage), d = f(() => Array.isArray(l.value) ? t.sortBy.length ? [...l.value].sort((e, u) => {
for (const { key: i, order: s } of t.sortBy) {
const r = e[i], n = u[i];
if (!(r == null && n == null)) {
if (r == null) return s === "asc" ? -1 : 1;
if (n == null) return s === "asc" ? 1 : -1;
if (r instanceof Date && n instanceof Date) {
const a = r.getTime() - n.getTime();
if (a !== 0) return s === "asc" ? a : -a;
continue;
}
if (typeof r == "number" && typeof n == "number") {
const a = r - n;
if (a !== 0) return s === "asc" ? a : -a;
continue;
}
if (typeof r == "string" && typeof n == "string") {
const a = r.localeCompare(n, void 0, { sensitivity: "base" });
if (a !== 0) return s === "asc" ? a : -a;
continue;
}
if (r !== n) return s === "asc" ? r > n ? 1 : -1 : r < n ? 1 : -1;
}
}
return 0;
}) : l.value : []), x = f(() => d.value.slice(y.value, m.value));
return {
options: t,
paginatedItems: x,
updateSort: ({ key: e, order: u }) => {
const i = t.sortBy.find((s) => s.key === e);
i ? i.order = u : B.value ? t.sortBy.push({ key: e, order: u }) : t.sortBy = [{ key: e, order: u }];
},
sortedItems: d
};
}
export {
D as default
};