UNPKG

yanzhen-design-vue

Version:

149 lines (148 loc) 4.82 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const utilsVue = require("@yanzhen-libs/utils-vue"); const utils = require("@yanzhen-libs/utils"); const lodashEs = require("lodash-es"); const core = require("@yanzhen-lowcode/core"); const constant = require("../constant.cjs"); const handleSchema = require("../utils/handleSchema.cjs"); const handleSchemaOptions = require("../utils/handleSchemaOptions.cjs"); const handleFormProviderItemSchema = require("../utils/handleFormProviderItemSchema.cjs"); function useFormProviderGlobalConfig(key) { const config = vue.inject(constant.formProviderContextKey, {}); const use = vue.computed(() => { switch (true) { case utilsVue.unref(utilsVue.unref(config).use): case !!utilsVue.unref(utilsVue.unref(config).formId): return true; } return false; }); const keys = Object.keys(utilsVue.unref(config)); const _config = keys.reduce( (prev, key2) => { if (key2 === "use") { prev[key2] = use; } else { prev[key2] = vue.computed(() => { var _a; const value = (_a = utilsVue.unref(config)) == null ? void 0 : _a[key2]; return utilsVue.unref(value); }); } return prev; }, { use } ); if (key) return _config[key]; const ref = vue.computed(() => { return utilsVue.unRefs(_config); }); return { ..._config, configRef: ref }; } function useFormProviderSchema(schemaRef, options, ref) { const bodySchema = vue.computed(() => { const schema = utilsVue.unref(schemaRef); const { slots } = utilsVue.unRefs(options); const fn = (slots == null ? void 0 : slots.default) || (slots == null ? void 0 : slots.body); let { body, grid, use, ...schemaOptions } = handleSchema.handleSchema(schema); if (lodashEs.isFunction(fn)) { body = fn({ body, grid, use, options: schemaOptions }); } if (lodashEs.isFunction(body)) { body = body(); } return lodashEs.isArray(body) ? body : [body]; }); const rowSchema = vue.computed(() => { const schema = utilsVue.unRefs(schemaRef); if (!schema || utils.isEmptyObject(schema)) return; const { grid, row } = handleSchema.handleSchema(schema); if (!grid) return; const params = utilsVue.unRefs(options); const { row: rowProps2, rowSchema: rowSchema2 } = handleSchemaOptions.handleSchemaOptions(params); if (!utils.isEmptyValue(rowSchema2)) return; return { type: "row", props: vue.mergeProps({}, row ?? {}, rowProps2) }; }); const rowProps = vue.computed(() => { var _a; return (_a = utilsVue.unref(rowSchema)) == null ? void 0 : _a.props; }); const formSchema = vue.computed(() => { const schema = utilsVue.unref(schemaRef); const params = utilsVue.unRefs(options); let formRef = ref; const { form: formProps2 } = handleSchemaOptions.handleSchemaOptions(params); const _formProps = { scrollToFirstError: true, ...lodashEs.omit(formProps2, "model") // model, // rules, }; if (schema) { const { ref: ref2 } = handleSchema.handleSchema(schema); const refs = lodashEs.flattenDepth([formRef, ref2]); formRef = core.creatSchemaRef(refs); } return { type: "form", ref: formRef, props: _formProps }; }); const formProps = vue.computed(() => { var _a; return (_a = utilsVue.unref(formSchema)) == null ? void 0 : _a.props; }); const formId = Symbol("form-provider-schema-config"); function handleFormProviderItemProps(item) { const schema = utilsVue.unref(schemaRef); const params = utilsVue.unRefs(options); const { field, formItemProps, colProps } = handleFormProviderItemSchema.handleFormProviderItemSchemaConfig( item ?? void 0, schema, params ); if (vue.isVNode(field)) return { vnode: field }; return { formItemProps, colProps, ...field }; } const bodySchemaConfig = vue.computed(() => { const body = utilsVue.unref(bodySchema); const schema = utilsVue.unref(schemaRef); const params = utilsVue.unRefs(options); return body.map((item) => { const { config } = handleFormProviderItemSchema.handleFormProviderItemSchemaConfig(item, schema, params); return config; }); }); const ctx = { ...options, formId, schema: schemaRef, bodySchema, formSchema, rowSchema, form: formProps, row: rowProps }; vue.provide(constant.formProviderContextKey, ctx); return { ctx, ...ctx, bodySchemaConfig, handleFormProviderItemProps }; } exports.useFormProviderGlobalConfig = useFormProviderGlobalConfig; exports.useFormProviderSchema = useFormProviderSchema;