UNPKG

yanzhen-design-vue

Version:

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