yanzhen-design-vue
Version:
134 lines (133 loc) • 2.67 kB
JavaScript
import { isVNode, mergeProps } from "vue";
import { isEmptyObject } from "@yanzhen-libs/utils";
import { isFunction, mergeWith } from "lodash-es";
import { handleSchemaOptions } from "./handleSchemaOptions.mjs";
import { handleSchema } from "./handleSchema.mjs";
import { handleRawSchema } from "./handleRawSchema.mjs";
function handleFormProviderItemSchemaConfig(rawField, schema, options) {
if (isVNode(rawField)) {
return {
field: rawField,
config: {
type: rawField
}
};
}
if (isEmptyObject(rawField)) {
return {
visible: false
};
}
const { grid } = handleSchema(schema);
const {
col: _colProps,
formItem: _formItemProps,
emit,
useModelValue
} = handleSchemaOptions(options);
const {
col,
field,
draggable,
use,
key,
visible,
hidden,
readOnly,
props: formItemNodeProps,
...fieldOption
} = handleRawSchema(rawField, schema);
const { name, label, ...formItem } = field;
if (isFunction(emit)) {
const onUpdateValue = formItemNodeProps["onUpdate:value"];
formItemNodeProps["onUpdate:value"] = (value, ...args) => {
if (isFunction(onUpdateValue)) {
try {
onUpdateValue(value, ...args);
} catch {
}
}
if (isFunction(emit)) {
emit("update:value", value, ...args);
useModelValue && emit("update:modelValue", value, ...args);
}
};
}
const fieldBody = {
...fieldOption,
readOnly,
visible,
hidden,
props: formItemNodeProps
};
if (!use) {
const field2 = fieldBody;
return {
field: field2,
config: field2
};
}
fieldBody.hidden = false;
const readOnlyClass = {
"is-read-only": readOnly
};
const formItemProps = mergeProps(
{
validateTrigger: ["blur", "change"]
},
_formItemProps,
formItem,
{
class: readOnlyClass,
name,
label
}
);
const formItemBody = {
type: "form-item",
key,
use,
props: formItemProps,
body: fieldBody
};
if (!grid) {
const config2 = {
...formItemBody,
draggable,
visible,
hidden
};
return {
formItemProps,
field: fieldBody,
config: config2
};
}
const colProps = mergeWith(
{
span: 24
},
mergeProps(_colProps, col)
);
const colBody = {
type: "col",
key,
props: colProps,
body: formItemBody
};
const config = {
...colBody,
draggable,
visible,
hidden
};
return {
colProps,
formItemProps,
field: fieldBody,
config
};
}
export {
handleFormProviderItemSchemaConfig
};