UNPKG

v-show-case

Version:

conditional logic for vue's v-show directive

63 lines (62 loc) 1.9 kB
import { resolveDirective as f, withDirectives as v, vShow as p } from "vue"; function E(e, { vShowWhen: t, vShowElse: r }) { var n, a; const o = (n = e.dirs) == null ? void 0 : n.find((s) => s.dir === t); return o ? o.value : !!((a = e.dirs) != null && a.find((s) => s.dir === r)); } const m = { name: "VShowCase", render() { const e = f("showWhen"), t = f("showElse"); let r = !1, o; return this.$slots.default().map((n) => (r ? o = !1 : (o = E(n, { vShowWhen: e, vShowElse: t }), o && (r = !0)), v(n, [[p, o]]))); } }, h = "0", w = "1", u = "2", l = "3", S = `^(${w}${l})+(${u}${l})?$`; function R(e) { return new RegExp(S).test(d(e)); } function d(e) { let t = []; return Array.isArray(e) ? (e.filter((r) => r.type !== 3).forEach((r) => { r.type !== 1 && c( "v-show-case children must be an element or component", r.loc ); let o = ""; r.props.find((i) => i.name === "for" || i.name === "if") && c( "v-show-case children cannot contain v-if or v-for directives", r.loc ); const a = r.props.find((i) => i.name === "show-when"); a && (a.exp || c( "v-show-when directive requires an argument", r.loc ), o += w); const s = r.props.find((i) => i.name === "show-else"); s && (s.exp && c( "v-show-else directive does not take any arguments", r.loc ), o += u), o === "" && (o += h), o += l, t.push(o); }), t.length || t.push(h)) : t.push(h), t.join(""); } function c(e, t) { throw new Error( `${e} (Line: ${t.start.line}) ${" ".repeat(t.start.column)}${t.source}` ); } function A(e) { e.tag === "v-show-case" && (R(e.children) || c("Invalid syntax for v-show-case", e.loc)); } const _ = { install(e) { e.component("v-show-case", m), e.directive("showWhen", {}), e.directive("showElse", {}); } }; export { _ as vShowCase, A as validateVShowCase };