UNPKG

adverich-kun-ui

Version:

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

92 lines (91 loc) 2.62 kB
import { ref as i, computed as h, watch as O, nextTick as g, onMounted as A, onBeforeUnmount as F } from "vue"; function $(t, H, l) { const s = i(""), m = i(t.modelValue), x = i(!1), a = i([]), c = h(() => t.formatModel === "json" || t.formatModel === "auto" && typeof t.modelValue == "object"); function v(e) { if (c.value && e != null) try { return JSON.stringify(e, null, 2); } catch { return ""; } return e ?? ""; } function y(e) { if (c.value && typeof e == "string") try { return JSON.parse(e); } catch { return e; } return e; } function I(e) { const u = y(e); m.value = u, H("update:modelValue", u); } O( () => t.modelValue, (e) => { m.value = e, s.value = v(e), t.autoGrow && g(() => d()); }, { immediate: !0, deep: !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 J(e) { var V; if (!c.value) return; const u = l.value; if (!u) return; const n = u.selectionStart, o = s.value, r = " ", f = o.slice(0, n), E = o.slice(n), N = f.lastIndexOf(` `) + 1, w = ` ${((V = f.slice(N).match(/^\s*/)) == null ? void 0 : V[0]) ?? ""}${r}`; s.value = f + w + E, g(() => { const S = n + w.length; u.setSelectionRange(S, S); }), e.preventDefault(); } A(() => { s.value = v(t.modelValue), g(() => { t.autoGrow && d(); }); }), F(() => { }); const M = h(() => { var e; return !!(a.value.length || (e = t.errorMessages) != null && e.length); }), b = h(() => M.value ? [...t.errorMessages || [], ...a.value].slice(0, t.maxErrors) : []); function j() { 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 C() { a.value = []; } return { internalValue: s, isFocused: x, hasError: M, displayedMessages: b, validate: j, resetValidation: C, updateModel: I, adjustHeight: d, handleJsonEnter: J }; } export { $ as default };