UNPKG

adverich-kun-ui

Version:

Una librería de componentes Vue.js con Tailwind CSS

93 lines (92 loc) 2.69 kB
import { ref as i, computed as h, watch as E, nextTick as m, onMounted as A, onBeforeUnmount as F } from "vue"; function $(t, H, l) { const s = i(""), c = i(t.modelValue), J = i(!1), a = i([]), f = h(() => t.formatModel === "json" || t.formatModel === "auto" && typeof t.modelValue == "object"); function v(e) { if (f.value && e != null) try { return JSON.stringify(e, null, 2); } catch { return ""; } return e ?? ""; } function y(e) { if (f.value && typeof e == "string") try { return JSON.parse(e); } catch { return null; } return e; } function x(e) { const u = y(e); c.value = u, H("update:modelValue", u); } E( () => t.modelValue, (e) => { const u = JSON.stringify(e), n = JSON.stringify(c.value); u !== n && (c.value = e, s.value = v(e), t.autoGrow && m(() => d())); }, { immediate: !0 } ); const d = () => { if (!l.value) return; l.value.style.height = "auto", l.value.style.overflowY = "hidden"; const e = l.value.scrollHeight, u = parseFloat(getComputedStyle(l.value).lineHeight) || 24, n = Number(t.maxRows || 0); if (t.maxRows && n > 0) { const o = n * u; l.value.style.height = Math.min(e, o) + "px"; } else l.value.style.height = e - 16 + "px"; }; function N(e) { var S; if (!f.value) return; const u = l.value; if (!u) return; const n = u.selectionStart, o = s.value, r = " ", g = o.slice(0, n), j = o.slice(n), C = g.lastIndexOf(` `) + 1, w = ` ${((S = g.slice(C).match(/^\s*/)) == null ? void 0 : S[0]) ?? ""}${r}`; s.value = g + w + j, m(() => { const V = n + w.length; u.setSelectionRange(V, V); }), e.preventDefault(); } A(() => { s.value = v(t.modelValue), m(() => { t.autoGrow && d(); }); }), F(() => { }); const M = h(() => { var e; return !!(a.value.length || (e = t.errorMessages) != null && e.length); }), O = h(() => M.value ? [...t.errorMessages || [], ...a.value].slice(0, t.maxErrors) : []); function I() { const e = t.rules || [], u = t.required && !s.value ? null : y(s.value), n = []; for (const o of e) { const r = typeof o == "function" ? o(u) : o; Array.isArray(r) ? n.push(...r) : typeof r == "string" ? n.push(r) : r === !1 && n.push("Campo inválido"); } return a.value = n, n.length === 0; } function b() { a.value = []; } return { internalValue: s, isFocused: J, hasError: M, displayedMessages: O, validate: I, resetValidation: b, updateModel: x, adjustHeight: d, handleJsonEnter: N }; } export { $ as default };