@indielayer/ui
Version:
Indielayer UI Components with Tailwind CSS build for Vue 3
395 lines (394 loc) • 14.7 kB
JavaScript
import { defineComponent as re, mergeModels as X, useModel as se, ref as oe, computed as b, watch as ie, openBlock as s, createElementBlock as i, mergeProps as D, unref as c, renderSlot as p, createElementVNode as y, normalizeStyle as K, normalizeClass as m, createVNode as v, withCtx as f, createBlock as g, withModifiers as L, createCommentVNode as k, Fragment as S, renderList as C, createTextVNode as W, toDisplayString as q } from "vue";
import { useTheme as ue } from "../../composables/useTheme.js";
import { useVirtualList as de } from "../../composables/useVirtualList.js";
import ce from "./TableHead.vue.js";
import E from "./TableHeader.vue.js";
import fe from "./TableBody.js";
import G from "./TableRow.vue.js";
import A from "./TableCell.vue.js";
import pe from "../spinner/Spinner.vue.js";
import ve from "../skeleton/Skeleton.vue.js";
import ge from "../icon/Icon.vue.js";
import J from "../checkbox/Checkbox.vue.js";
import { chevronDownIcon as ye } from "../../common/icons.js";
const me = { key: 1 }, he = ["colspan"], be = { key: 2 }, ke = ["colspan"], Se = ["onClick"], we = ["colspan"], $e = {
headers: {
type: Array,
default: () => []
},
items: {
type: Array,
default: () => []
},
sort: {
type: Array,
default: () => []
},
loading: Boolean,
loadingSkeleton: Boolean,
loadingLines: {
type: [Number, String],
default: 3
},
error: Boolean,
dense: Boolean,
fixed: Boolean,
striped: Boolean,
pointer: Boolean,
scrollable: {
type: Boolean,
default: !0
},
stickyHeader: {
type: Boolean,
default: !0
},
expandable: Boolean,
virtualList: Boolean,
virtualListOffsetTop: Number,
virtualListOffsetBottom: Number,
virtualListItemHeight: {
type: Number,
default: 54
},
virtualListOverscan: {
type: Number,
default: 5
},
keyProp: String,
selectable: Boolean,
singleSelect: Boolean,
autoClearSelected: {
type: Boolean,
default: !0
},
toFn: Function,
hrefFn: Function,
hrefTarget: String
}, Ce = { name: "XTable" }, Ve = /* @__PURE__ */ re({
...Ce,
props: /* @__PURE__ */ X({
...$e,
items: {
type: Array,
default: () => []
},
toFn: Function,
hrefFn: Function,
hrefTarget: String
}, {
selected: {},
selectedModifiers: {}
}),
emits: /* @__PURE__ */ X(["update:sort", "click-row"], ["update:selected"]),
setup(u, { emit: Q }) {
const l = u, r = se(u, "selected"), w = oe(/* @__PURE__ */ new Map()), $ = b(() => l.items), { list: U, containerProps: Y, wrapperProps: Z } = de(
$,
{
disabled: !l.virtualList,
itemHeight: l.virtualListItemHeight || 54,
topOffset: l.virtualListOffsetTop || 0,
bottomOffset: l.virtualListOffsetBottom || 0,
overscan: l.virtualListOverscan
}
);
function d(e, a) {
if (!l.keyProp || !e || typeof e != "object" || e === null)
return a;
const t = e[l.keyProp];
return t == null ? (console.warn(`[XTable] keyProp "${l.keyProp}" is undefined/null for item at index ${a}. Falling back to index.`), a) : typeof t != "string" && typeof t != "number" ? (console.warn(`[XTable] keyProp "${l.keyProp}" must be a string or number, got ${typeof t}. Falling back to index.`), a) : t;
}
function B(e) {
return e.index;
}
const M = Q;
function j(e, a) {
if (e)
for (let t = 0; t < a.length; t++) {
const { 0: n, 1: o } = a[t].split(",");
if (e === n)
return parseInt(o) > 0 ? 1 : -1;
}
}
function _(e) {
const a = l.sort.slice(0);
let t = !1;
for (let n = 0; n < a.length; n++) {
const { 0: o, 1: h } = a[n].split(",");
if (o === e.value) {
if (t = !0, h === "-1") {
a.splice(n, 1, `${e.value},1`);
break;
} else if (h === "1") {
a.splice(n, 1);
break;
}
}
}
t || a.push(`${e.value},-1`), M("update:sort", a);
}
const F = /* @__PURE__ */ new Map();
function ee(e, a) {
if (!a || !e)
return "";
let t;
return Array.isArray(a) ? t = a : F.has(a) ? t = F.get(a) : (t = a.match(/([^[.\]])+/g), t && F.set(a, t)), !t || t.length === 0 ? "" : t.reduce((o, h) => o == null || typeof o != "object" ? null : o[h], e) ?? "";
}
const x = b(() => l.selectable ? $.value.map((e, a) => d(e, a)) : []), O = b(() => !l.selectable || l.singleSelect ? /* @__PURE__ */ new Set() : Array.isArray(r.value) ? new Set(r.value) : /* @__PURE__ */ new Set()), V = b(() => {
if (!l.selectable || l.singleSelect || !Array.isArray(r.value) || r.value.length === 0)
return !1;
const e = x.value.length;
return e === 0 ? !1 : r.value.length === e;
}), T = b(() => {
if (!l.selectable || l.singleSelect || !Array.isArray(r.value) || r.value.length === 0)
return !1;
const e = x.value.length;
return e === 0 ? !1 : r.value.length > 0 && r.value.length !== e;
});
function z(e) {
return l.selectable ? l.singleSelect ? r.value === e : O.value.has(e) : !1;
}
function N(e) {
l.selectable && (l.singleSelect ? r.value = r.value === e ? void 0 : e : (Array.isArray(r.value) || (r.value = []), O.value.has(e) ? r.value = r.value.filter((a) => a !== e) : r.value = [...r.value, e]));
}
function R() {
!l.selectable || l.singleSelect || (V.value || T.value ? r.value = [] : r.value = x.value);
}
function te(e) {
if (!l.expandable)
return;
const a = d(e.data, B(e));
w.value.set(a, !w.value.get(a));
}
function P(e) {
if (!l.expandable)
return !1;
const a = d(e.data, B(e));
return w.value.get(a) ?? !1;
}
function le(e, a) {
const t = B(a);
if (l.selectable && l.singleSelect) {
const n = d(e, t);
N(n);
}
M("click-row", e, t);
}
const H = b(() => {
let e = l.headers.length;
return l.selectable && !l.singleSelect && e++, l.expandable && e++, e;
});
ie($, (e) => {
const a = /* @__PURE__ */ new Set();
e.forEach((t, n) => {
a.add(d(t, n));
}), l.expandable && w.value.forEach((t, n) => {
a.has(n) || w.value.delete(n);
}), l.selectable && l.autoClearSelected && (l.singleSelect ? a.has(r.value) || (r.value = void 0) : Array.isArray(r.value) && r.value.length > 0 && (r.value = r.value.filter((t) => a.has(t))));
}, { immediate: !0 });
const { styles: ae, classes: I, className: ne } = ue("Table", {}, l);
return (e, a) => (s(), i("div", D({
class: [c(ne), c(I).wrapper]
}, c(Y)), [
p(e.$slots, "title"),
p(e.$slots, "actions"),
y("div", D(c(Z), {
class: ["relative", {
"!h-auto": l.loading
}]
}), [
y("table", {
style: K(c(ae)),
class: m(c(I).table)
}, [
v(ce, { "sticky-header": e.stickyHeader }, {
default: f(() => [
l.selectable && !l.singleSelect ? (s(), g(E, {
key: 0,
width: "40",
class: "!pl-3.5 !pr-0.5 !py-2.5 cursor-pointer",
onClick: R
}, {
default: f(() => [
v(J, {
"model-value": V.value || T.value,
indeterminate: T.value,
"hide-footer": "",
"aria-label": "Select all rows",
"skip-form-registry": "",
onClick: L(R, ["prevent", "stop"])
}, null, 8, ["model-value", "indeterminate"])
]),
_: 1
})) : k("", !0),
e.expandable ? (s(), g(E, {
key: 1,
width: "48",
class: "!p-0"
})) : k("", !0),
(s(!0), i(S, null, C(e.headers, (t, n) => (s(), g(E, {
key: n,
"text-align": t.align,
sort: j(t.value, e.sort),
sortable: t.sortable,
width: t.width,
tooltip: t.tooltip,
onClick: (o) => t.sortable ? _(t) : null
}, {
default: f(() => [
p(e.$slots, `header-${t.value}`, { header: t }, () => [
W(q(t.text), 1)
])
]),
_: 2
}, 1032, ["text-align", "sort", "sortable", "width", "tooltip", "onClick"]))), 128))
]),
_: 3
}, 8, ["sticky-header"]),
v(c(fe), null, {
default: f(() => [
e.loading ? (s(!0), i(S, { key: 0 }, C(Number(e.loadingLines), (t, n) => (s(), g(G, {
key: n,
striped: e.striped
}, {
default: f(() => [
(s(!0), i(S, null, C(e.headers, (o, h) => (s(), g(A, {
key: h,
"text-align": o.align,
width: o.width,
dense: e.dense,
fixed: e.fixed
}, {
default: f(() => [
p(e.$slots, `loading-${o.value}`, { item: t }, () => [
v(ve, {
class: m(["max-w-[60%]", {
"mx-auto": o.align === "center",
"ml-auto": o.align === "right"
}]),
shape: o.skeletonShape || "line"
}, null, 8, ["shape", "class"])
])
]),
_: 2
}, 1032, ["text-align", "width", "dense", "fixed"]))), 128))
]),
_: 2
}, 1032, ["striped"]))), 128)) : e.error ? (s(), i("tr", me, [
y("td", { colspan: H.value }, [
p(e.$slots, "error")
], 8, he)
])) : !$.value || $.value.length === 0 ? (s(), i("tr", be, [
y("td", { colspan: H.value }, [
p(e.$slots, "empty")
], 8, ke)
])) : (s(!0), i(S, { key: 3 }, C(c(U), (t) => (s(), i(S, {
key: d(t.data, t.index)
}, [
v(G, {
pointer: e.pointer || !!u.toFn || !!u.hrefFn,
striped: e.striped,
selected: z(d(t.data, t.index)),
"single-select": e.singleSelect,
onClick: (n) => le(t.data, t)
}, {
default: f(() => [
l.selectable && !e.singleSelect ? (s(), g(A, {
key: 0,
width: "40",
class: "!pl-3.5 !pr-0.5 cursor-pointer",
onClick: L((n) => N(d(t.data, t.index)), ["stop"])
}, {
default: f(() => [
v(J, {
"model-value": z(d(t.data, t.index)),
"hide-footer": "",
"aria-label": `Select row ${B(t) + 1}`,
"skip-form-registry": "",
onClick: L((n) => N(d(t.data, t.index)), ["prevent", "stop"])
}, null, 8, ["model-value", "aria-label", "onClick"])
]),
_: 2
}, 1032, ["onClick"])) : k("", !0),
e.expandable ? (s(), g(A, {
key: 1,
width: "48",
class: "!p-1"
}, {
default: f(() => [
y("button", {
type: "button",
class: m(["px-3 p-2", [e.dense ? "p-0.5" : "px-3 py-2"]]),
onClick: L((n) => te(t), ["stop"])
}, [
v(ge, {
icon: c(ye),
size: e.dense ? "xs" : "md",
class: m(["transition-transform", {
"rotate-180": P(t)
}])
}, null, 8, ["icon", "size", "class"])
], 10, Se)
]),
_: 2
}, 1024)) : k("", !0),
(s(!0), i(S, null, C(e.headers, (n, o) => (s(), g(A, {
key: o,
"text-align": n.align,
truncate: n.truncate,
width: n.width,
dense: e.dense,
style: K([l.virtualList ? {
height: `${l.virtualListItemHeight}px`,
maxHeight: `${l.virtualListItemHeight}px`,
overflow: "hidden",
whiteSpace: "nowrap"
} : {}]),
href: u.hrefFn ? u.hrefFn(t.data) : void 0,
to: u.toFn ? u.toFn(t.data) : void 0,
target: u.hrefFn ? u.hrefTarget : void 0
}, {
default: f(() => [
p(e.$slots, `item-${n.value}`, {
item: t.data
}, () => [
W(q(ee(t.data, n.value)), 1)
])
]),
_: 2
}, 1032, ["text-align", "truncate", "width", "dense", "style", "href", "to", "target"]))), 128))
]),
_: 2
}, 1032, ["pointer", "striped", "selected", "single-select", "onClick"]),
e.expandable ? (s(), i("tr", {
key: 0,
class: m({ hidden: !P(t) })
}, [
y("td", { colspan: H.value }, [
y("div", {
class: m(["overflow-hidden transition-opacity", [P(t) ? "" : "opacity-0 max-h-0"]])
}, [
p(e.$slots, "expanded-row", {
item: t.data
})
], 2)
], 8, we)
], 2)) : k("", !0)
], 64))), 128))
]),
_: 3
})
], 6),
e.loading ? (s(), i("div", {
key: 0,
class: m(c(I).loadingWrapper)
}, [
v(pe, { size: "lg" })
], 2)) : k("", !0)
], 16)
], 16));
}
});
export {
Ve as default
};