UNPKG

@tuoyuan/json-schema

Version:

json-schema编辑器及表单组件

120 lines (119 loc) 3.87 kB
import { defineComponent as K, mergeModels as f, useModel as E, ref as O, computed as B, watch as v, onBeforeMount as M, onMounted as k, resolveComponent as h, openBlock as i, createBlock as w, withCtx as g, createVNode as q, createElementBlock as A, Fragment as N, renderList as $, nextTick as z } from "vue"; import D from "./components/form-item.vue.js"; import P from "ajv"; import y from "ajv-i18n/localize/zh"; import R from "ajv-formats"; import S from "ajv-formats-draft2019"; import { deepEqual as T, setFormData as _ } from "../utils/utils.js"; const Y = /* @__PURE__ */ K({ name: "AJsonSchemaForm", __name: "index", props: /* @__PURE__ */ f({ // json-schema 数据 options: { type: Object, required: !0 }, // 表单组件列表 widgets: { type: Array, default: () => [] }, // 是否在字段change时校验表单 validateOnChange: { type: Boolean } }, { modelValue: { type: Object, required: !0 }, modelModifiers: {} }), emits: /* @__PURE__ */ f(["change"], ["update:modelValue"]), setup(m, { expose: V, emit: b }) { const a = E(m, "modelValue"), o = m, p = b, r = new P({ strict: "log", allErrors: !0 }); r.addKeyword("order"), r.addKeyword("widget"), r.addKeyword("layout"), r.addKeyword("x-hidden"), r.addKeyword("x-disabled"), r.addKeyword("enumDescriptions"), R(r), S(r), r.addFormat("time", /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/), r.addFormat( "date-time", /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/ ); let n = r.compile(o.options); const u = O(), x = B(() => { let e = []; return o.options.order ? e = o.options.order : e = Object.keys(o.options.properties), e; }); v( a, (e, t) => { }, { deep: !0 } ), v( () => o.options, (e, t) => { T(e, t) || (_(a.value, o.options.properties), n = r.compile(o.options)); }, { deep: !0 } ), M(() => { _(a.value, o.options.properties), p("change", a.value); }), k(() => { }); const F = (e) => { let t = ""; return e.forEach((d, s) => { s === 0 ? t = d : t += `[${d}]`; }), t; }, c = (e) => { if (!e) return; const t = {}; e.forEach((d) => { const s = d.instancePath.split("/").slice(1); d.keyword === "required" && s.push(d.params.missingProperty); const l = F(s); t[l] = { status: "error", message: d.message }; }), z(() => { u.value.setFields(t); }); }, j = () => { o.validateOnChange && (n(a.value), y(n.errors), c(n.errors)), p("change", a.value); }; return V({ validate: () => { const e = n(a.value); return y(n.errors), c(n.errors), e; } }), (e, t) => { const d = h("a-row"), s = h("a-form"); return i(), w(s, { ref_key: "formRef", ref: u, class: "schema-form-wrapper", model: a.value, layout: "vertical", "auto-label-width": "" }, { default: g(() => [ q(d, null, { default: g(() => [ (i(!0), A(N, null, $(x.value, (l, J) => (i(), w(D, { key: l, modelValue: a.value, "onUpdate:modelValue": t[0] || (t[0] = (C) => a.value = C), fieldName: l, options: o.options.properties[l], parent: o.options, breadcrumb: l, widgets: o.widgets, onChange: j }, null, 8, ["modelValue", "fieldName", "options", "parent", "breadcrumb", "widgets"]))), 128)) ]), _: 1 }) ]), _: 1 }, 8, ["model"]); }; } }); export { Y as default };