UNPKG

yanzhen-design-vue

Version:

193 lines (192 loc) 6.33 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const utilsVue = require("@yanzhen-libs/utils-vue"); const lodashEs = require("lodash-es"); const utils = require("@yanzhen-libs/utils"); const core = require("@vueuse/core"); const useFormProviderSchema = require("./hooks/use-form-provider-schema.cjs"); const handleFormProviderItemSchema = require("./utils/handleFormProviderItemSchema.cjs"); const handleSchema = require("./utils/handleSchema.cjs"); function useFormProviderItem(props, emit) { const { model: _modelRef, rules: _rulesRef } = vue.toRefs(props); const { configRef, schema, rules, use: useRef, model: modelRef } = useFormProviderSchema.useFormProviderGlobalConfig(); const id = lodashEs.uniqueId("form-provider-item-"); const modelValue = utilsVue.useModel(props, "value"); const updatePropsKey = utilsVue.useDynamicProps({ update: true }); const watcher = utilsVue.createWatcher(); const rulesRef = vue.computed(() => { const prop = utilsVue.unref(name); if (!prop) { return utilsVue.unref(rules); } return { [prop]: utilsVue.unref(_rulesRef) }; }); const optionsRef = vue.computed(() => { const options = { ...lodashEs.pick(props, "model", "rules", "customRequest", "schemaConfig"), form: {}, ...utilsVue.unref(configRef) }; for (const key of Object.keys(options)) { switch (key) { case "model": options[key] = utilsVue.unref(_model); break; case "rules": options[key] = utilsVue.unref(rulesRef); break; } } return options; }); function handleUpdateValue(value) { modelValue.value = value; } const schemaRef = vue.computed(() => { var _a; let _schema = {}; if (props.schema) { _schema = utilsVue.unref(props.schema); } else { const body = utilsVue.unref((_a = utilsVue.unref(schema)) == null ? void 0 : _a.body); if (lodashEs.isArray(body)) { const find = body.find((value) => { var _a2; if (vue.isVNode(value)) return false; return ((_a2 = value == null ? void 0 : value.field) == null ? void 0 : _a2.name) === utilsVue.unref(name); }); if (find) { _schema = utilsVue.unref(find); } } } switch (true) { case vue.isVNode(_schema): case utils.isEmptyValue(_schema): return _schema; case (utilsVue.unref(useRef) && !utilsVue.unref(updatePropsKey).includes("value")): return _schema; default: return { ..._schema, props: vue.mergeProps({}, _schema.props ?? {}, { value: utilsVue.unref(modelValue) }) }; } }); const config = vue.computed(() => { return handleSchema.handleSchema(utilsVue.unref(optionsRef).schema); }); const name = vue.computed(() => { var _a, _b, _c; const schema2 = utilsVue.unref(schemaRef); const name2 = props.name ?? ((_a = schema2 == null ? void 0 : schema2.field) == null ? void 0 : _a.name); if (name2) return name2; const template = ((_b = schema2 == null ? void 0 : schema2.props) == null ? void 0 : _b["update:value"]) ?? ((_c = schema2 == null ? void 0 : schema2.props) == null ? void 0 : _c["value"]); if (utils.isCompileTemplate(template)) { const { vars } = utils.createCompiler(template); if (vars.length === 1) return vars[0]; } return id; }); const _model = vue.computed(() => { if (!utilsVue.unref(name)) return; const proxyModel = utilsVue.unref(_modelRef) ?? utilsVue.unref(modelRef) ?? { [utilsVue.unref(name)]: utilsVue.unref(modelValue) }; return new Proxy(proxyModel, { get(target, p, receiver) { return Reflect.get(target, p, receiver); }, set(target, p, newValue, receiver) { if (utilsVue.unref(name) === p) { handleUpdateValue(newValue); } return Reflect.set(target, p, newValue, receiver); } }); }); const _modelValue = vue.computed(() => { const key = utilsVue.unref(name); if (!key) return; const model = utilsVue.unref(_modelRef) ?? utilsVue.unref(modelRef); return model == null ? void 0 : model[key]; }); watcher.set("model", { source: _modelValue, handler(value, oldValue) { if (value === oldValue) return; modelValue.value = value; }, deep: true, debounce: 300 }); const use = vue.computed(() => { var _a; return utilsVue.unref(useRef) || ((_a = utilsVue.unref(config)) == null ? void 0 : _a.use); }); const schemaItemRef = vue.computed(() => { const rawSchema = utilsVue.unref(schemaRef); if (!rawSchema) return; const options = utilsVue.unref(optionsRef); const { config: config2 } = handleFormProviderItemSchema.handleFormProviderItemSchemaConfig( rawSchema, utilsVue.unref(options.schema), options ); return config2; }); const type = vue.computed(() => { if (utilsVue.unref(use)) { const schema2 = utilsVue.unref(schemaRef); if (vue.isVNode(schema2)) return schema2; return schema2 == null ? void 0 : schema2.type; } return "page"; }); const pageProviderProps = vue.computed(() => { const options = utilsVue.unref(optionsRef); return vue.normalizeProps( vue.mergeProps({}, options, { schema: { type: "page", body: props.schema } }) ); }); const [DefineColTemplate, ReusableColTemplate] = core.createReusableTemplate(); const [DefineFormItemTemplate, ReusableFormItemTemplate] = core.createReusableTemplate(); const [DefinePageProviderItem, ReusablePageProviderItem] = core.createReusableTemplate(); vue.onUnmounted(() => { watcher.stop(); }); const ctx = { use, type, schemaItem: schemaItemRef, model: _model, pageProviderProps, schemaRef, optionsRef, name, modelValue }; const methods = {}; const templates = { DefineColTemplate, ReusableColTemplate, DefineFormItemTemplate, ReusableFormItemTemplate, DefinePageProviderItem, ReusablePageProviderItem }; return { ctx, methods, templates }; } exports.useFormProviderItem = useFormProviderItem;