UNPKG

dk-plus

Version:
93 lines (92 loc) 3.1 kB
import { defineComponent as D, ref as R, reactive as C, watch as V, computed as F, onBeforeUnmount as H, toRefs as I } from "vue"; import { dkTimeSelectProps as P } from "./index3.js"; import { timeSelectEmits as U } from "./index4.js"; import { buildTimeList as _, parseTimeToSeconds as m } from "./index5.js"; const z = D({ name: "DkTimeSelect", props: P, emits: U, setup(t, { emit: i }) { const r = R(null), n = C({ open: !1, text: t.modelValue, committed: t.modelValue }); V( () => t.modelValue, (e) => { n.text = e || "", n.committed = e || ""; }, { immediate: !0 } ); const a = F(() => { const e = _(t.start, t.end, t.step), o = t.minTime ? m(t.minTime) : null, s = t.maxTime ? m(t.maxTime) : null; return e.map((d) => { const l = m(d), L = l == null || o != null && l < o || s != null && l > s; let x = !1; if (l != null) { const c = Math.floor(l / 3600), y = Math.floor(l % 3600 / 60), k = l % 60, T = typeof t.disabledHours == "function" ? t.disabledHours() : [], h = typeof t.disabledMinutes == "function" ? t.disabledMinutes(c) : [], S = typeof t.disabledSeconds == "function" ? t.disabledSeconds(c, y) : []; x = Array.isArray(T) && T.includes(c) || Array.isArray(h) && h.includes(y) || Array.isArray(S) && S.includes(k); } return { value: d, disabled: L || x }; }); }), g = (e) => (t.modelValue || "") === e, A = (e) => { const o = a.value.find((s) => s.value === e); return !!(o != null && o.disabled); }, f = (e) => { n.text = e, n.committed = e, i("update:modelValue", e), i("change", e); }, w = (e) => { t.disabled || A(e) || (f(e), n.open = !1); }, b = () => { if (!t.disabled) { if (!n.text) { n.open = !1; return; } n.text = "", n.committed = "", i("update:modelValue", ""), i("change", ""), i("clear"), n.open = !1; } }, M = (e) => { n.text = String(e != null ? e : ""); }, B = (e) => { t.disabled || (n.open = !0, i("focus", e)); }, E = (e) => { if (!t.editable || t.disabled) { i("blur", e); return; } const o = (n.text || "").trim(); if (!o) { n.committed && b(), i("blur", e); return; } a.value.some((d) => d.value === o && !d.disabled) ? o !== n.committed && f(o) : n.text = n.committed, i("blur", e); }, u = (e) => { if (!n.open) return; const o = r.value, s = e.target; !o || !s || o.contains(s) || (n.open = !1); }; return V( () => n.open, (e) => { e ? document.addEventListener("mousedown", u) : document.removeEventListener("mousedown", u); }, { immediate: !0 } ), H(() => { document.removeEventListener("mousedown", u); }), { ...I(n), rootRef: r, optionItems: a, isSelected: g, onPick: w, onClear: b, onInputUpdate: M, onFocus: B, onBlur: E }; } }); export { z as default };