UNPKG

@open-data-v/ui

Version:

OpenDataV UI基础包

210 lines (209 loc) 6.69 kB
import { defineComponent as H, ref as w, reactive as K, watch as _, toRefs as M, inject as P, computed as J, provide as R, onMounted as U, openBlock as d, createBlock as z, mergeProps as A, withCtx as G, createElementVNode as q, createElementBlock as m, Fragment as Q, renderList as W, withDirectives as X, normalizeClass as L, unref as E, vShow as Y, toDisplayString as Z, createCommentVNode as ee, renderSlot as te } from "vue"; import le from "./SelectDown.vue.esm.js"; /* empty css */ const oe = { class: /* @__PURE__ */ L("o-select") }, ae = ["title", "onClick", "innerHTML"], se = { key: 0, class: "select-empty-options" }, ue = /* @__PURE__ */ H({ __name: "Select", props: { value: {}, multiple: { type: Boolean }, filterable: { type: Boolean }, downHeight: { default: 200 }, multipleLimit: {}, options: { default: () => [] }, optionsKey: { default: () => ({ label: "label", value: "value" }) }, beforeChange: {}, async: { type: Boolean }, emptyText: { default: "无数据" }, allowCreate: { type: Boolean }, placeholder: { default: "" }, clearable: { type: Boolean, default: !1 } }, emits: ["update:value", "updateValue", "change", "limitChange", "clear", "delete", "input", "focus", "blur", "toggleClick"], setup(B, { expose: O, emit: x }) { const l = B, n = x, h = w(), a = K({ checked: [], // 所有已选择的集合 keywords: "", setFirst: !1, // 手动选择改变选项时,在watch时不触发setFirstText事件 tempChecked: "" }), c = w(l.options); _( () => l.value, () => { k(); } ), _( () => l.options, () => { c.value = l.options, k(); }, { deep: !0 } ); const { optionsKey: f } = M(l), i = f.value.label, u = f.value.value, T = (e) => { if (!l.filterable) return; const o = c.value.filter((t) => t[i] === e && !t.disabled); if (o.length > 0) { let t = !1; a.checked.forEach((s) => { s[i] === o[0][i] && (t = !0); }), t || (a.checked.push(o[0]), r(o[0])); } h.value.setValue(y.value), a.keywords = "", setTimeout(() => { c.value.forEach((t) => { delete t._disabled; }); }, 500), n("blur", e); }, V = (e) => { l.filterable && (a.keywords = e, l.async || c.value.forEach((o) => { o._disabled = o[i].indexOf(e) === -1; }), a.setFirst = !0, n("input", e)); }, k = () => { if (!a.setFirst && (a.setFirst = !1, l.value && c.value && c.value.length > 0)) { a.checked = [], typeof l.value == "object" ? l.value.forEach((e) => { a.checked.push({ [i]: e, [u]: e }); }) : a.checked.push({ [i]: l.value, [u]: l.value }); for (let e = 0; e < c.value.length; e++) { const o = c.value[e], t = p(o); if (l.multiple) { if (typeof l.value == "object") { const s = l.value.indexOf(t); s !== -1 && a.checked.splice(s, 1, o); } } else if (t === l.value) { a.checked.splice(0, 1, o); break; } } } }, F = (e) => { const o = p(e); if (l.multiple) { if (typeof l.value == "object") return l.value.indexOf(o) !== -1; } else return o === l.value; }, p = (e) => e[u] === void 0 ? e[i] : e[u], b = (e, o) => { if (!e.disabled) { if (l.beforeChange && !l.beforeChange(e)) { v(), o && o.stopPropagation(); return; } const t = p(e); if (l.multiple) { if (l.multipleLimit && l.multipleLimit > 0 && l.multipleLimit <= a.checked.length) return n("limitChange", l.value), !1; let s = -1; a.checked.forEach((g, N) => { p(g) === t && (s = N); }), s !== -1 ? a.checked.splice(s, 1) : a.checked.push(e), r(e); } else a.checked = [e], v(), r(e); } o && o.stopPropagation(); }, D = (e) => { if (l.multiple && typeof l.value == "object") if (e) a.tempChecked = JSON.stringify(l.value); else { const o = JSON.parse(a.tempChecked); let t = !0; o.forEach((s) => { l.value.indexOf(s) === -1 && (t = !1); }), t || n("delete", -1); } n("toggleClick", e); }, v = () => { h.value.slideUp(); }, C = P("OControlChange", ""), r = (e, o = !0) => { a.setFirst = o; let t = []; a.checked.forEach((s) => { t.push(p(s)); }), l.multiple || (t = t[0] || ""), n("update:value", t), n("updateValue", t), C && C(t), n("change", t, e); }, S = (e) => { if (a.keywords && l.filterable) { const o = new RegExp(`${a.keywords}`, "gi"); return e.replace(o, "<b>$&</b>"); } else return e; }, y = J(() => a.checked.map((e) => e[i])), $ = () => { a.checked = [], r(), n("clear"); }, j = (e) => { a.checked.splice(e, 1), r(), n("delete", e); }; R("OGetChildOption", (e) => { c.value.push(e); }); const I = (e) => { if (!l.allowCreate) return !1; const o = c.value.filter((t) => t[i].includes(e)); if (o && o.length > 0) b(o[0], ""); else { const t = { [i]: e, [u]: e }; a.checked = [e], c.value.push(t), r(t); } }; return U(() => { k(); }), O({ slideUp: v }), (e, o) => (d(), z(le, A(e.$props, { ref_key: "selectDownEl", ref: h, "before-change": "", options: "", optionskey: "", value: y.value, clearable: e.clearable, onClear: $, onDelete: j, onInput: V, onBlur: T, onToggleClick: D, onKeyupEnter: I }), { default: G(() => [ q("ul", oe, [ (d(!0), m(Q, null, W(c.value, (t, s) => X((d(), m("li", { key: s, class: L({ disabled: t.disabled, active: F(t), [t.class]: t.class }), title: t[E(f).label], onClick: (g) => b(t, g), innerHTML: S(t[E(f).label]) }, null, 10, ae)), [ [Y, !t._disabled] ])), 128)), c.value.length === 0 ? (d(), m("p", se, Z(e.emptyText), 1)) : ee("", !0) ]), te(e.$slots, "default") ]), _: 3 }, 16, ["value", "clearable"])); } }); export { ue as default }; //# sourceMappingURL=Select.vue.esm.js.map