76 lines
1.9 KiB
Plaintext
76 lines
1.9 KiB
Plaintext
import { isArray, isFunction, isObject, isString, isNullOrUnDef } from '@/utils/is';
|
|
import { unref } from 'vue';
|
|
import type { Ref, ComputedRef } from 'vue';
|
|
import type { FormSchema } from '../types/form';
|
|
import { set } from 'lodash-es';
|
|
|
|
interface UseFormValuesContext {
|
|
defaultFormModel: Ref<any>;
|
|
getSchema: ComputedRef<FormSchema[]>;
|
|
formModel: Recordable;
|
|
}
|
|
|
|
/**
|
|
* @desription 变异目标对象
|
|
*/
|
|
function strShiftObject(key: string, value: any, target: Recordable) {
|
|
const pattern = /^\{(.+)\}$/;
|
|
if (pattern.test(key)) {
|
|
const match = key.match(pattern);
|
|
if (match && match[1]) {
|
|
const keys = match[1].split(',');
|
|
value = isObject(value) ? value : {};
|
|
keys.forEach((k) => {
|
|
set(target, k.trim(), value[k.trim()]);
|
|
});
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function useFormValues({ defaultFormModel, getSchema, formModel }: UseFormValuesContext) {
|
|
// 加工 form values
|
|
function handleFormValues(values: Recordable) {
|
|
if (!isObject(values)) {
|
|
return {};
|
|
}
|
|
const res: Recordable = {};
|
|
for (const item of Object.entries(values)) {
|
|
let [, value] = item;
|
|
const [key] = item;
|
|
if (
|
|
!key ||
|
|
(isArray(value) && value.length === 0) ||
|
|
isFunction(value) ||
|
|
isNullOrUnDef(value)
|
|
) {
|
|
continue;
|
|
}
|
|
// 删除空格
|
|
if (isString(value)) {
|
|
value = value.trim();
|
|
}
|
|
if (!strShiftObject(key, value, res)) {
|
|
set(res, key, value);
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
//初始化默认值
|
|
function initDefault() {
|
|
const schemas = unref(getSchema);
|
|
const obj: Recordable = {};
|
|
schemas.forEach((item) => {
|
|
const { defaultValue } = item;
|
|
if (!isNullOrUnDef(defaultValue)) {
|
|
obj[item.name] = defaultValue;
|
|
formModel[item.name] = defaultValue;
|
|
}
|
|
});
|
|
defaultFormModel.value = obj;
|
|
}
|
|
|
|
return { handleFormValues, initDefault };
|
|
}
|