BasicForm.vue 15 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
<template>
  <Form v-bind="$attrs" ref="formElRef" :model="formModel">
    <Row :class="getProps.compact ? 'compact-form-row' : ''">
      <slot name="formHeader" />
      <template v-for="schema in getSchema" :key="schema.field">
        <FormItem
          :schema="schema"
          :formProps="getProps"
          :allDefaultValues="getAllDefaultValues"
          :formModel="formModel"
        >
          <template #[item]="data" v-for="item in Object.keys($slots)">
            <slot :name="item" v-bind="data" />
          </template>
        </FormItem>
      </template>
      <FormAction
        v-bind="{ ...getActionPropsRef, ...advanceState }"
        @toggle-advanced="handleToggleAdvanced"
      />
      <slot name="formFooter" />
    </Row>
  </Form>
</template>
<script lang="ts">
  import type { FormActionType, FormProps, FormSchema } from './types/form';
  import type { Form as FormType, ValidateFields } from 'ant-design-vue/types/form/form';

  import {
    defineComponent,
    reactive,
    ref,
    computed,
    unref,
    toRaw,
    watch,
    toRef,
    onMounted,
  } from 'vue';
  import { Form, Row } from 'ant-design-vue';
  import FormItem from './FormItem';
  import { basicProps } from './props';
  import { deepMerge, unique } from '/@/utils';
  import FormAction from './FormAction';

  import { dateItemType } from './helper';
  import moment from 'moment';
  import { isArray, isBoolean, isFunction, isNumber, isObject, isString } from '/@/utils/is';
  import { cloneDeep } from 'lodash-es';
  import { useBreakpoint } from '/@/hooks/event/useBreakpoint';
  // import { useThrottle } from '/@/hooks/core/useThrottle';
  import { useFormValues } from './hooks/useFormValues';
  import type { ColEx } from './types';
  import { NamePath } from 'ant-design-vue/types/form/form-item';
  const BASIC_COL_LEN = 24;

  export default defineComponent({
    name: 'BasicForm',
    inheritAttrs: false,
    components: { FormItem, Form, Row, FormAction },
    props: basicProps,
    emits: ['advanced-change', 'reset', 'submit', 'register'],
    setup(props, { emit }) {
      let formModel = reactive({});
      const advanceState = reactive({
        isAdvanced: true,
        hideAdvanceBtn: false,
        isLoad: false,
        actionSpan: 6,
      });
      const propsRef = ref<Partial<FormProps>>({});
      const schemaRef = ref<FormSchema[] | null>(null);
      const formElRef = ref<Nullable<FormType>>(null);

      const getMergePropsRef = computed(
        (): FormProps => {
          return deepMerge(toRaw(props), unref(propsRef));
        }
      );
      // 获取表单基本配置
      const getProps = computed(
        (): FormProps => {
          const resetAction = {
            onClick: resetFields,
          };
          const submitAction = {
            onClick: handleSubmit,
          };
          return {
            ...unref(getMergePropsRef),
            resetButtonOptions: deepMerge(
              resetAction,
              unref(getMergePropsRef).resetButtonOptions || {}
            ) as any,
            submitButtonOptions: deepMerge(
              submitAction,
              unref(getMergePropsRef).submitButtonOptions || {}
            ) as any,
          };
        }
      );

      const getActionPropsRef = computed(() => {
        const {
          resetButtonOptions,
          submitButtonOptions,
          showActionButtonGroup,
          showResetButton,
          showSubmitButton,
          showAdvancedButton,
          actionColOptions,
        } = unref(getProps);
        return {
          resetButtonOptions,
          submitButtonOptions,
          show: showActionButtonGroup,
          showResetButton,
          showSubmitButton,
          showAdvancedButton,
          actionColOptions,
        };
      });

      const getSchema = computed((): FormSchema[] => {
        const schemas: FormSchema[] = unref(schemaRef) || (unref(getProps).schemas as any);
        for (const schema of schemas) {
          const { defaultValue, component } = schema;
          if (defaultValue && dateItemType.includes(component!)) {
            schema.defaultValue = moment(defaultValue);
          }
        }
        return schemas as FormSchema[];
      });

      const getAllDefaultValues = computed(() => {
        const schemas = unref(getSchema);
        const obj: any = {};
        schemas.forEach((item) => {
          if (item.defaultValue) {
            obj[item.field] = item.defaultValue;
            (formModel as any)[item.field] = item.defaultValue;
          }
        });
        return obj;
      });
      const getEmptySpanRef = computed((): number => {
        if (!advanceState.isAdvanced) {
          return 0;
        }
        const emptySpan = unref(getMergePropsRef).emptySpan || 0;

        if (isNumber(emptySpan)) {
          return emptySpan;
        }
        if (isObject(emptySpan)) {
          const { span = 0 } = emptySpan;
          const screen = unref(screenRef) as string;

          const screenSpan = (emptySpan as any)[screen.toLowerCase()];
          return screenSpan || span || 0;
        }
        return 0;
      });

      const { realWidthRef, screenEnum, screenRef } = useBreakpoint();
      // const [throttleUpdateAdvanced] = useThrottle(updateAdvanced, 30, { immediate: true });
      watch(
        [() => unref(getSchema), () => advanceState.isAdvanced, () => unref(realWidthRef)],
        () => {
          const { showAdvancedButton } = unref(getProps);
          if (showAdvancedButton) {
            updateAdvanced();
          }
        },
        { immediate: true }
      );
      function updateAdvanced() {
        let itemColSum = 0;
        let realItemColSum = 0;
        for (const schema of unref(getSchema)) {
          const { show, colProps } = schema;
          let isShow = true;

          if (isBoolean(show)) {
            isShow = show;
          }

          if (isFunction(show)) {
            isShow = show({
              schema: schema,
              model: formModel,
              field: schema.field,
              values: {
                ...getAllDefaultValues,
                ...formModel,
              },
            });
          }
          if (isShow && colProps) {
            const { itemColSum: sum, isAdvanced } = getAdvanced(colProps, itemColSum);

            itemColSum = sum || 0;
            if (isAdvanced) {
              realItemColSum = itemColSum;
            }
            schema.isAdvanced = isAdvanced;
          }
        }
        advanceState.actionSpan = (realItemColSum % BASIC_COL_LEN) + unref(getEmptySpanRef);
        getAdvanced(props.actionColOptions || { span: BASIC_COL_LEN }, itemColSum, true);
        emit('advanced-change');
      }
      function getAdvanced(itemCol: Partial<ColEx>, itemColSum = 0, isLastAction = false) {
        const width = unref(realWidthRef);

        const mdWidth =
          parseInt(itemCol.md as string) ||
          parseInt(itemCol.xs as string) ||
          parseInt(itemCol.sm as string) ||
          (itemCol.span as number) ||
          BASIC_COL_LEN;
        const lgWidth = parseInt(itemCol.lg as string) || mdWidth;
        const xlWidth = parseInt(itemCol.xl as string) || lgWidth;
        const xxlWidth = parseInt(itemCol.xxl as string) || xlWidth;
        if (width <= screenEnum.LG) {
          itemColSum += mdWidth;
        } else if (width < screenEnum.XL) {
          itemColSum += lgWidth;
        } else if (width < screenEnum.XXL) {
          itemColSum += xlWidth;
        } else {
          itemColSum += xxlWidth;
        }
        if (isLastAction) {
          advanceState.hideAdvanceBtn = false;
          if (itemColSum <= BASIC_COL_LEN * 2) {
            // 小于等于2行时,不显示收起展开按钮
            advanceState.hideAdvanceBtn = true;
            advanceState.isAdvanced = true;
          } else if (
            itemColSum > BASIC_COL_LEN * 2 &&
            itemColSum <= BASIC_COL_LEN * (props.autoAdvancedLine || 3)
          ) {
            advanceState.hideAdvanceBtn = false;

            // 大于3行默认收起
          } else if (!advanceState.isLoad) {
            advanceState.isLoad = true;
            advanceState.isAdvanced = !advanceState.isAdvanced;
          }
          return { isAdvanced: advanceState.isAdvanced, itemColSum };
        }
        if (itemColSum > BASIC_COL_LEN) {
          return { isAdvanced: advanceState.isAdvanced, itemColSum };
        } else {
          // 第一行始终显示
          return { isAdvanced: true, itemColSum };
        }
      }

      async function resetFields(): Promise<any> {
        const { resetFunc } = unref(getProps);
        resetFunc && isFunction(resetFunc) && (await resetFunc());
        const formEl = unref(formElRef);
        if (!formEl) return;
        Object.keys(formModel).forEach((key) => {
          (formModel as any)[key] = undefined;
        });
        const values = formEl.resetFields();
        emit('reset', toRaw(formModel));
        return values;
      }

      /**
       * @description: 设置表单值
       */
      async function setFieldsValue(values: any): Promise<void> {
        const fields = unref(getSchema)
          .map((item) => item.field)
          .filter(Boolean);
        const formEl = unref(formElRef);
        Object.keys(values).forEach((key) => {
          const element = values[key];
          if (fields.includes(key) && element !== undefined && element !== null) {
            // 时间
            if (itemIsDateType(key)) {
              if (Array.isArray(element)) {
                const arr: any[] = [];
                for (const ele of element) {
                  arr.push(moment(ele));
                }
                (formModel as any)[key] = arr;
              } else {
                (formModel as any)[key] = moment(element);
              }
            } else {
              (formModel as any)[key] = element;
            }
            if (formEl) {
              formEl.validateFields([key]);
            }
          }
        });
      }

      /**
       * @description: 表单提交
       */
      async function handleSubmit(e?: Event): Promise<void> {
        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 formEl.validate();
          const res = handleFormValues(values);
          emit('submit', res);
        } catch (error) {}
      }

      /**
       * @description: 根据字段名删除
       */
      function removeSchemaByFiled(fields: string | string[]): void {
        const schemaList: FormSchema[] = cloneDeep(unref(getSchema));
        if (!fields) {
          return;
        }
        let fieldList: string[] = fields as string[];
        if (isString(fields)) {
          fieldList = [fields];
        }
        for (const field of fieldList) {
          _removeSchemaByFiled(field, schemaList);
        }
        schemaRef.value = schemaList as any;
      }
      /**
       * @description: 根据字段名删除
       */
      function _removeSchemaByFiled(field: string, schemaList: FormSchema[]): void {
        if (isString(field)) {
          const index = schemaList.findIndex((schema) => schema.field === field);
          if (index !== -1) {
            schemaList.splice(index, 1);
          }
        }
      }
      /**
       * @description: 往某个字段后面插入,如果没有插入最后一个
       */
      function appendSchemaByField(schema: FormSchema, prefixField?: string) {
        const schemaList: FormSchema[] = cloneDeep(unref(getSchema));

        const index = schemaList.findIndex((schema) => schema.field === prefixField);
        const hasInList = schemaList.find((item) => item.field === schema.field);

        if (hasInList) {
          return;
        }
        if (!prefixField || index === -1) {
          schemaList.push(schema);
          schemaRef.value = schemaList as any;
          return;
        }
        if (index !== -1) {
          schemaList.splice(index + 1, 0, schema);
        }
        schemaRef.value = schemaList as any;
      }

      function updateSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) {
        let updateData: Partial<FormSchema>[] = [];
        if (isObject(data)) {
          updateData.push(data as FormSchema);
        }
        if (isArray(data)) {
          updateData = [...data];
        }
        const hasField = updateData.every((item) => Reflect.has(item, 'field') && item.field);
        if (!hasField) {
          throw new Error('Must pass in the `field` field!');
        }
        const schema: FormSchema[] = [];
        updateData.forEach((item) => {
          unref(getSchema).forEach((val) => {
            if (val.field === item.field) {
              const newScheam = deepMerge(val, item);
              schema.push(newScheam as FormSchema);
            } else {
              schema.push(val);
            }
          });
        });

        schemaRef.value = unique(schema, 'field') as any;
      }

      function handleToggleAdvanced() {
        advanceState.isAdvanced = !advanceState.isAdvanced;
      }

      const handleFormValues = useFormValues(
        toRef(props, 'transformDateFunc'),
        toRef(props, 'fieldMapToTime')
      );
      function getFieldsValue(): any {
        const formEl = unref(formElRef);
        if (!formEl) return;
        return handleFormValues(toRaw(unref(formModel)));
      }

      /**
       * @description: 是否是时间
       */
      function itemIsDateType(key: string) {
        return unref(getSchema).some((item) => {
          return item.field === key ? dateItemType.includes(item.component!) : false;
        });
      }
      /**
       * @description:设置表单
       */
      function setProps(formProps: Partial<FormProps>): void {
        const mergeProps = deepMerge(unref(propsRef) || {}, formProps);
        propsRef.value = mergeProps;
      }

      function validateFields(nameList?: NamePath[] | undefined) {
        if (!formElRef.value) return;
        return formElRef.value.validateFields(nameList);
      }
      function validate(nameList?: NamePath[] | undefined) {
        if (!formElRef.value) return;
        return formElRef.value.validate(nameList);
      }

      function clearValidate(name: string | string[]) {
        if (!formElRef.value) return;
        formElRef.value.clearValidate(name);
      }

      const methods: Partial<FormActionType> = {
        getFieldsValue,
        setFieldsValue,
        resetFields,
        updateSchema,
        setProps,
        removeSchemaByFiled,
        appendSchemaByField,
        clearValidate,
        validateFields: validateFields as ValidateFields,
        validate: validate as ValidateFields,
      };
      onMounted(() => {
        emit('register', methods);
      });
      return {
        handleToggleAdvanced,
        formModel,
        getActionPropsRef,
        getAllDefaultValues,
        advanceState,
        getProps,
        formElRef,
        getSchema,
        ...methods,
      };
    },
  });
</script>