yanzhen-design-vue
Version:
193 lines (192 loc) • 5.88 kB
JavaScript
import { toRefs, computed, isVNode, mergeProps, normalizeProps, onUnmounted } from "vue";
import { useModel, useDynamicProps, createWatcher, unref } from "@yanzhen-libs/utils-vue";
import { uniqueId, pick, isArray } from "lodash-es";
import { isEmptyValue, isCompileTemplate, createCompiler } from "@yanzhen-libs/utils";
import { createReusableTemplate } from "@vueuse/core";
import { useFormProviderGlobalConfig } from "./hooks/use-form-provider-schema.mjs";
import { handleFormProviderItemSchemaConfig } from "./utils/handleFormProviderItemSchema.mjs";
import { handleSchema } from "./utils/handleSchema.mjs";
function useFormProviderItem(props, emit) {
const { model: _modelRef, rules: _rulesRef } = toRefs(props);
const { configRef, schema, rules, use: useRef, model: modelRef } = useFormProviderGlobalConfig();
const id = uniqueId("form-provider-item-");
const modelValue = useModel(props, "value");
const updatePropsKey = useDynamicProps({ update: true });
const watcher = createWatcher();
const rulesRef = computed(() => {
const prop = unref(name);
if (!prop) {
return unref(rules);
}
return {
[prop]: unref(_rulesRef)
};
});
const optionsRef = computed(() => {
const options = {
...pick(props, "model", "rules", "customRequest", "schemaConfig"),
form: {},
...unref(configRef)
};
for (const key of Object.keys(options)) {
switch (key) {
case "model":
options[key] = unref(_model);
break;
case "rules":
options[key] = unref(rulesRef);
break;
}
}
return options;
});
function handleUpdateValue(value) {
modelValue.value = value;
}
const schemaRef = computed(() => {
var _a;
let _schema = {};
if (props.schema) {
_schema = unref(props.schema);
} else {
const body = unref((_a = unref(schema)) == null ? void 0 : _a.body);
if (isArray(body)) {
const find = body.find((value) => {
var _a2;
if (isVNode(value)) return false;
return ((_a2 = value == null ? void 0 : value.field) == null ? void 0 : _a2.name) === unref(name);
});
if (find) {
_schema = unref(find);
}
}
}
switch (true) {
case isVNode(_schema):
case isEmptyValue(_schema):
return _schema;
case (unref(useRef) && !unref(updatePropsKey).includes("value")):
return _schema;
default:
return {
..._schema,
props: mergeProps({}, _schema.props ?? {}, {
value: unref(modelValue)
})
};
}
});
const config = computed(() => {
return handleSchema(unref(optionsRef).schema);
});
const name = computed(() => {
var _a, _b, _c;
const schema2 = 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 (isCompileTemplate(template)) {
const { vars } = createCompiler(template);
if (vars.length === 1) return vars[0];
}
return id;
});
const _model = computed(() => {
if (!unref(name)) return;
const proxyModel = unref(_modelRef) ?? unref(modelRef) ?? { [unref(name)]: unref(modelValue) };
return new Proxy(proxyModel, {
get(target, p, receiver) {
return Reflect.get(target, p, receiver);
},
set(target, p, newValue, receiver) {
if (unref(name) === p) {
handleUpdateValue(newValue);
}
return Reflect.set(target, p, newValue, receiver);
}
});
});
const _modelValue = computed(() => {
const key = unref(name);
if (!key) return;
const model = unref(_modelRef) ?? 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 = computed(() => {
var _a;
return unref(useRef) || ((_a = unref(config)) == null ? void 0 : _a.use);
});
const schemaItemRef = computed(() => {
const rawSchema = unref(schemaRef);
if (!rawSchema) return;
const options = unref(optionsRef);
const { config: config2 } = handleFormProviderItemSchemaConfig(
rawSchema,
unref(options.schema),
options
);
return config2;
});
const type = computed(() => {
if (unref(use)) {
const schema2 = unref(schemaRef);
if (isVNode(schema2)) return schema2;
return schema2 == null ? void 0 : schema2.type;
}
return "page";
});
const pageProviderProps = computed(() => {
const options = unref(optionsRef);
return normalizeProps(
mergeProps({}, options, {
schema: {
type: "page",
body: props.schema
}
})
);
});
const [DefineColTemplate, ReusableColTemplate] = createReusableTemplate();
const [DefineFormItemTemplate, ReusableFormItemTemplate] = createReusableTemplate();
const [DefinePageProviderItem, ReusablePageProviderItem] = createReusableTemplate();
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
};
}
export {
useFormProviderItem
};