65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
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: any;
|
|
}
|
|
export function useFormValues({
|
|
defaultFormModel,
|
|
getSchema,
|
|
formModel,
|
|
}: UseFormValuesContext) {
|
|
// 加工 form values
|
|
function handleFormValues(values: any) {
|
|
if (!isObject(values)) {
|
|
return {};
|
|
}
|
|
const res: any = {};
|
|
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();
|
|
}
|
|
set(res, key, value);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
//初始化默认值
|
|
function initDefault() {
|
|
const schemas = unref(getSchema);
|
|
const obj: any = {};
|
|
schemas.forEach((item) => {
|
|
const { defaultValue } = item;
|
|
if (!isNullOrUnDef(defaultValue)) {
|
|
obj[item.field] = defaultValue;
|
|
formModel[item.field] = defaultValue;
|
|
}
|
|
});
|
|
defaultFormModel.value = obj;
|
|
}
|
|
|
|
return { handleFormValues, initDefault };
|
|
}
|