vben
2021-11-10 3fcfac1f37c2aeabbb2af4897ada6ba8c225c667
src/components/Form/src/hooks/useFormValues.ts
@@ -1,22 +1,21 @@
import { isArray, isFunction, isObject, isString } from '/@/utils/is';
import moment from 'moment';
import { isArray, isFunction, isObject, isString, isNullOrUnDef } from '/@/utils/is';
import { dateUtil } from '/@/utils/dateUtil';
import { unref } from 'vue';
import type { Ref, ComputedRef } from 'vue';
import type { FieldMapToTime, FormSchema } from '../types/form';
import type { FormProps, FormSchema } from '../types/form';
import { set } from 'lodash-es';
interface UseFormValuesContext {
  transformDateFuncRef: Ref<Fn>;
  fieldMapToTimeRef: Ref<FieldMapToTime>;
  defaultValueRef: Ref<any>;
  getSchema: ComputedRef<FormSchema[]>;
  getProps: ComputedRef<FormProps>;
  formModel: Recordable;
}
export function useFormValues({
  transformDateFuncRef,
  fieldMapToTimeRef,
  defaultValueRef,
  getSchema,
  formModel,
  getProps,
}: UseFormValuesContext) {
  // Processing form values
  function handleFormValues(values: Recordable) {
@@ -27,21 +26,22 @@
    for (const item of Object.entries(values)) {
      let [, value] = item;
      const [key] = item;
      if ((isArray(value) && value.length === 0) || isFunction(value)) {
      if (!key || (isArray(value) && value.length === 0) || isFunction(value)) {
        continue;
      }
      const transformDateFunc = unref(transformDateFuncRef);
      const transformDateFunc = unref(getProps).transformDateFunc;
      if (isObject(value)) {
        value = transformDateFunc(value);
        value = transformDateFunc?.(value);
      }
      if (isArray(value) && value[0]._isAMomentObject && value[1]._isAMomentObject) {
        value = value.map((item) => transformDateFunc(item));
      if (isArray(value) && value[0]?.format && value[1]?.format) {
        value = value.map((item) => transformDateFunc?.(item));
      }
      // Remove spaces
      if (isString(value)) {
        value = value.trim();
      }
      res[key] = value;
      set(res, key, value);
    }
    return handleRangeTimeValue(res);
  }
@@ -50,21 +50,21 @@
   * @description: Processing time interval parameters
   */
  function handleRangeTimeValue(values: Recordable) {
    const fieldMapToTime = unref(fieldMapToTimeRef);
    const fieldMapToTime = unref(getProps).fieldMapToTime;
    if (!fieldMapToTime || !Array.isArray(fieldMapToTime)) {
      return values;
    }
    for (const [field, [startTimeKey, endTimeKey, format = 'YYYY-MM-DD']] of fieldMapToTime) {
    for (const [field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD'] of fieldMapToTime) {
      if (!field || !startTimeKey || !endTimeKey || !values[field]) {
        continue;
      }
      const [startTime, endTime]: string[] = values[field];
      values[startTimeKey] = moment(startTime).format(format);
      values[endTimeKey] = moment(endTime).format(format);
      values[startTimeKey] = dateUtil(startTime).format(format);
      values[endTimeKey] = dateUtil(endTime).format(format);
      Reflect.deleteProperty(values, field);
    }
@@ -75,9 +75,10 @@
    const schemas = unref(getSchema);
    const obj: Recordable = {};
    schemas.forEach((item) => {
      if (item.defaultValue) {
        obj[item.field] = item.defaultValue;
        formModel[item.field] = item.defaultValue;
      const { defaultValue } = item;
      if (!isNullOrUnDef(defaultValue)) {
        obj[item.field] = defaultValue;
        formModel[item.field] = defaultValue;
      }
    });
    defaultValueRef.value = obj;