UNPKG

@layui/layui-vue

Version:

a component library for Vue 3 base on layui-vue

113 lines (112 loc) 5.55 kB
import { ref as n, computed as S, watch as g, onMounted as C } from "vue"; function I(r) { var A, x, z, F; const f = n([]), w = n(r.onlyLastLevel), o = n(r.multiple), h = n(r.decollator), s = n(/* @__PURE__ */ new Map()), M = n(r.alwaysLazy), j = n((A = r.style) == null ? void 0 : A.loadingTheme), m = n(r.checkStrictly), K = n(r.changeOnSelect), p = n([]), u = n(r.modelValue ?? []), i = n(typeof u.value == "string" ? u.value.split(h.value ?? "/") : Array.isArray(u.value) ? [...u.value] : []), y = n({ label: ((x = r.replaceFields) == null ? void 0 : x.label) ?? "label", value: ((z = r.replaceFields) == null ? void 0 : z.value) ?? "value", children: ((F = r.replaceFields) == null ? void 0 : F.children) ?? "children" }), k = (e, a) => e.map((l) => { const t = { label: l[y.value.label], value: l[y.value.value], loading: !1, slot: l.slot, indeterminate: !1, checked: !1, selected: !1, disabled: l.disabled, children: void 0, orig: void 0, parent: a }; return t.children = l[y.value.children] ? k(l[y.value.children], t) : void 0, t.orig = l, t; }); f.value = k(r.options ?? [], void 0); const O = S(() => { const e = o.value ? [...s.value.values()].map((t) => v.value.find((c) => c.value === t.value)) : i.value.map((t) => v.value.find((c) => c.value === t)), a = (t) => { if (!t) return []; let c = []; return c = t.parent ? [...a(t.parent), t.label] : [t.label], c; }, l = e.map((t) => r.fullpath && o.value ? a(t).join(r.decollator) : (t == null ? void 0 : t.label) ?? ""); return o.value ? l : w.value ? (l == null ? void 0 : l.pop()) ?? "" : l.join(` ${r.decollator} `); }), T = S(() => ((e) => { const a = []; a.push(e); let l = e; return p.value.forEach((t) => { var c; l = l instanceof Array ? l.find((d) => d.value === t) : (c = l == null ? void 0 : l.children) == null ? void 0 : c.find((d) => d.value === t), a.push((l == null ? void 0 : l.children) ?? []); }), a.filter((t) => t.length); })(f.value).filter((e, a) => a <= p.value.length)), v = S(() => { const e = [], a = (l) => { l.forEach((t) => { var c; e.push(t), (c = t.children) != null && c.length && a(t.children); }); }; return a(f.value), e; }); g(() => r.options, () => { f.value = k(r.options ?? [], void 0); }, { deep: !0 }), g(() => r.checkStrictly, (e) => m.value = e ?? !1), g(() => o.value, () => { const e = s.value.values(); let a = e.next(); do { if (a.done) break; a.value.checked = !1, a = e.next(); } while (!a.done); s.value.clear(); }); const b = () => { m.value || (v.value.filter((e) => { var a; return !((a = e.children) != null && a.length); }).forEach((e) => { var a, l; e.parent && (e.parent.checked = ((a = e.parent.children) == null ? void 0 : a.every((t) => t.checked)) || !1, e.parent.checked || (e.parent.indeterminate = ((l = e.parent.children) == null ? void 0 : l.some((t) => t.checked)) || !1)); }), v.value.filter((e) => e.parent).forEach((e) => { var a, l; e.parent.checked = ((a = e.parent.children) == null ? void 0 : a.every((t) => t.checked)) || !1, e.parent.checked || (e.parent.indeterminate = ((l = e.parent.children) == null ? void 0 : l.some((t) => t.checked || t.indeterminate)) || !1); }), v.value.filter((e) => { var a; return !e.parent && ((a = e.children) == null ? void 0 : a.length); }).forEach((e) => { var a, l; e.checked = ((a = e.children) == null ? void 0 : a.every((t) => t.checked)) || !1, e.checked || (e.indeterminate = ((l = e.children) == null ? void 0 : l.some((t) => t.indeterminate || t.checked)) || !1); })); }, L = () => { var e; if ((e = u.value) != null && e.length) if (o.value) { let a = u.value; typeof a == "string" && (a = a.split(h.value ?? "")), o.value && a.forEach((l) => { const t = v.value.find((c) => c.value === l); t && (t.checked = !0, s.value.set(l, t)); }), b(); } else { if (i.value = u.value instanceof Array ? u.value : u.value.split(h.value ?? ""), m.value) { const a = v.value.find((l) => l.value === i.value.at(i.value.length - 1)); a && (a.selected = !0); } i.value.length && (p.value = i.value); } else i.value = []; }; g(() => u.value, (e, a) => { if (o.value) { if (e instanceof Array) { const [l, t] = V(e, a); l.forEach((c) => { const d = v.value.find((E) => E.value === c); d && (d.checked = !0, s.value.set(c, d)); }), t.forEach((c) => { const d = v.value.find((E) => E.value === c); d && (d.checked = !1, s.value.delete(c)); }); } b(); } else i.value = e instanceof Array ? e : e.split(h.value ?? ""), i.value.length && (p.value = i.value); }); const V = (e = [], a = []) => [e.filter((l) => !a.includes(l)) || [], a.filter((l) => !e.includes(l)) || []]; return C(() => L()), { dataSource: T, sanitizer: k, onlyLastLevel: w, multiple: o, checkStrictly: m, decollator: h, multipleSelectItem: s, alwaysLazy: M, loadingTheme: j, selectKeys: i, showKeys: p, selectLabel: O, iterCollector: (e) => { const a = []; let l = e.next(); do { if (l.done) break; a.push(l.value), l = e.next(); } while (!l.done); return a; }, flatData: v, changeOnSelect: K, buildMultipleStatus: b, modelValue: u, setup: L }; } export { I as default };