tav-ui
Version:
204 lines (201 loc) • 6.54 kB
JavaScript
import { unref, toRaw } from 'vue';
import { cloneDeep, uniqBy } from 'lodash-es';
import { deepMerge } from '../../../../utils/basic2.mjs';
import { dateUtil } from '../../../../utils/dateUtil2.mjs';
import { isFunction, isString, isObject, isArray, isNullOrUnDef } from '../../../../utils/is2.mjs';
import { error } from '../../../../utils/log2.mjs';
import { handleInputNumberValue, dateItemType } from '../helper2.mjs';
function useFormEvents({
emit,
getProps,
formModel,
getSchema,
defaultValueRef,
formElRef,
schemaRef,
handleFormValues
}) {
async function resetFields() {
const { resetFunc, submitOnReset } = unref(getProps);
resetFunc && isFunction(resetFunc) && await resetFunc();
const formEl = unref(formElRef);
if (!formEl)
return;
Object.keys(formModel).forEach((key) => {
formModel[key] = defaultValueRef.value[key];
});
clearValidate();
emit("reset", toRaw(formModel));
submitOnReset && handleSubmit();
}
async function setFieldsValue(values, useValidate = true) {
const fields = unref(getSchema).map((item) => item.field).filter(Boolean);
const validKeys = [];
Object.keys(values).forEach((key) => {
const schema = unref(getSchema).find((item) => item.field === key);
let value = values[key];
const hasKey = Reflect.has(values, key);
value = handleInputNumberValue(schema?.component, value);
if (hasKey && fields.includes(key)) {
if (itemIsDateType(key)) {
if (Array.isArray(value)) {
formModel[key] = value;
} else {
const { componentProps } = schema || {};
let _props = componentProps;
if (typeof componentProps === "function")
_props = _props({ formModel });
formModel[key] = value ? _props?.valueFormat ? value : dateUtil(value) : null;
}
} else {
formModel[key] = value;
}
validKeys.push(key);
}
});
useValidate && validateFields(validKeys).catch((_) => {
});
}
async function removeSchemaByFiled(fields) {
const schemaList = cloneDeep(unref(getSchema));
if (!fields)
return;
let fieldList = isString(fields) ? [fields] : fields;
if (isString(fields))
fieldList = [fields];
for (const field of fieldList)
_removeSchemaByFiled(field, schemaList);
schemaRef.value = schemaList;
}
function _removeSchemaByFiled(field, schemaList) {
if (isString(field)) {
const index = schemaList.findIndex((schema) => schema.field === field);
if (index !== -1) {
delete formModel[field];
schemaList.splice(index, 1);
}
}
}
async function appendSchemaByField(schema, prefixField, first = false) {
const schemaList = cloneDeep(unref(getSchema));
const index = schemaList.findIndex((schema2) => schema2.field === prefixField);
if (!prefixField || index === -1 || first) {
first ? schemaList.unshift(schema) : schemaList.push(schema);
schemaRef.value = schemaList;
return;
}
if (index !== -1)
schemaList.splice(index + 1, 0, schema);
schemaRef.value = schemaList;
}
async function resetSchema(data) {
let updateData = [];
if (isObject(data))
updateData.push(data);
if (isArray(data))
updateData = [...data];
const hasField = updateData.every((item) => item.component === "Divider" || item.component === "FormTitle" || Reflect.has(item, "field") && item.field);
if (!hasField) {
error("All children of the form Schema array that need to be updated must contain the `field` field");
return;
}
schemaRef.value = updateData;
}
async function updateSchema(data) {
let updateData = [];
if (isObject(data)) {
updateData.push(data);
}
if (isArray(data)) {
updateData = [...data];
}
const hasField = updateData.every((item) => item.component === "Divider" || item.component === "FormTitle" || Reflect.has(item, "field") && item.field);
if (!hasField) {
error("All children of the form Schema array that need to be updated must contain the `field` field");
return;
}
const schema = unref(getSchema);
updateData.forEach((item) => {
const findResult = schema.find((val) => val.field === item.field);
if (findResult) {
const newSchema = deepMerge(findResult, item);
schema.push(newSchema);
} else {
schema.push(item);
}
});
schemaRef.value = uniqBy(schema, "field");
}
function transFormFieldsValue(values) {
for (let i = 0; i < unref(getSchema).length; i++) {
const item = unref(getSchema)[i];
if (item.component === "InputNumber" && !isNullOrUnDef(values[item.field])) {
values[item.field] = Number(values[item.field]);
}
}
return values;
}
function getFieldsValue() {
const formEl = unref(formElRef);
if (!formEl)
return {};
const res = handleFormValues(toRaw(unref(formModel)));
return transFormFieldsValue(res);
}
function itemIsDateType(key) {
return unref(getSchema).some((item) => {
return item.field === key ? dateItemType.includes(item.component) : false;
});
}
async function validateFields(nameList) {
return unref(formElRef)?.validateFields(nameList);
}
async function validate(nameList) {
try {
const res = await unref(formElRef)?.validate(nameList);
return transFormFieldsValue(res);
} catch (error2) {
return Promise.reject(error2);
}
}
async function clearValidate(name) {
await unref(formElRef)?.clearValidate(name);
}
async function scrollToField(name, options) {
await unref(formElRef)?.scrollToField(name, options);
}
async function handleSubmit(e) {
e && e.preventDefault();
const { submitFunc } = unref(getProps);
if (submitFunc && isFunction(submitFunc)) {
await submitFunc();
return;
}
const formEl = unref(formElRef);
if (!formEl)
return;
try {
const values = await validate();
const res = handleFormValues(values);
emit("submit", res);
} catch (error2) {
throw new Error(error2);
}
}
return {
handleSubmit,
clearValidate,
validate,
validateFields,
getFieldsValue,
updateSchema,
resetSchema,
appendSchemaByField,
removeSchemaByFiled,
resetFields,
setFieldsValue,
scrollToField
};
}
export { useFormEvents };
//# sourceMappingURL=useFormEvents2.mjs.map