Vben
2021-06-17 4f20d45f9dcb2e1ed29555c61fc92090e3fe12e5
src/components/Form/src/BasicForm.vue
@@ -1,7 +1,13 @@
<template>
  <Form v-bind="{ ...$attrs, ...$props }" :class="getFormClass" ref="formElRef" :model="formModel">
    <Row :style="getRowWrapStyle">
      <slot name="formHeader" />
  <Form
    v-bind="getBindValue"
    :class="getFormClass"
    ref="formElRef"
    :model="formModel"
    @keypress.enter="handleEnterPress"
  >
    <Row v-bind="getRow">
      <slot name="formHeader"></slot>
      <template v-for="schema in getSchema" :key="schema.field">
        <FormItem
          :tableAction="tableAction"
@@ -13,31 +19,36 @@
          :setFormModel="setFormModel"
        >
          <template #[item]="data" v-for="item in Object.keys($slots)">
            <slot :name="item" v-bind="data" />
            <slot :name="item" v-bind="data"></slot>
          </template>
        </FormItem>
      </template>
      <FormAction
        v-bind="{ ...getProps, ...advanceState }"
        @toggle-advanced="handleToggleAdvanced"
      />
      <slot name="formFooter" />
      <FormAction v-bind="{ ...getProps, ...advanceState }" @toggle-advanced="handleToggleAdvanced">
        <template
          #[item]="data"
          v-for="item in ['resetBefore', 'submitBefore', 'advanceBefore', 'advanceAfter']"
        >
          <slot :name="item" v-bind="data"></slot>
        </template>
      </FormAction>
      <slot name="formFooter"></slot>
    </Row>
  </Form>
</template>
<script lang="ts">
  import type { FormActionType, FormProps, FormSchema } from './types/form';
  import type { AdvanceState } from './types/hooks';
  import type { CSSProperties, Ref, WatchStopHandle } from 'vue';
  import type { Ref } from 'vue';
  import { defineComponent, reactive, ref, computed, unref, onMounted, watch, toRefs } from 'vue';
  import { defineComponent, reactive, ref, computed, unref, onMounted, watch, nextTick } from 'vue';
  import { Form, Row } from 'ant-design-vue';
  import FormItem from './components/FormItem';
  import FormItem from './components/FormItem.vue';
  import FormAction from './components/FormAction.vue';
  import { dateItemType } from './helper';
  import moment from 'moment';
  import { dateUtil } from '/@/utils/dateUtil';
  // import { cloneDeep } from 'lodash-es';
  import { deepMerge } from '/@/utils';
@@ -46,17 +57,21 @@
  import { useFormEvents } from './hooks/useFormEvents';
  import { createFormContext } from './hooks/useFormContext';
  import { useAutoFocus } from './hooks/useAutoFocus';
  import { useModalContext } from '/@/components/Modal';
  import { basicProps } from './props';
  import { useDesign } from '/@/hooks/web/useDesign';
  import type { RowProps } from 'ant-design-vue/lib/grid/Row';
  export default defineComponent({
    name: 'BasicForm',
    components: { FormItem, Form, Row, FormAction },
    props: basicProps,
    emits: ['advanced-change', 'reset', 'submit', 'register'],
    setup(props, { emit }) {
    setup(props, { emit, attrs }) {
      const formModel = reactive<Recordable>({});
      const modalFn = useModalContext();
      const advanceState = reactive<AdvanceState>({
        isAdvanced: true,
@@ -74,11 +89,9 @@
      const { prefixCls } = useDesign('basic-form');
      // Get the basic configuration of the form
      const getProps = computed(
        (): FormProps => {
          return { ...props, ...unref(propsRef) } as FormProps;
        }
      );
      const getProps = computed((): FormProps => {
        return { ...props, ...unref(propsRef) } as FormProps;
      });
      const getFormClass = computed(() => {
        return [
@@ -89,12 +102,17 @@
        ];
      });
      // Get uniform row style
      const getRowWrapStyle = computed(
        (): CSSProperties => {
          const { baseRowStyle = {} } = unref(getProps);
          return baseRowStyle;
        }
      // Get uniform row style and Row configuration for the entire form
      const getRow = computed((): RowProps => {
        const { baseRowStyle = {}, rowProps } = unref(getProps);
        return {
          style: baseRowStyle,
          ...rowProps,
        };
      });
      const getBindValue = computed(
        () => ({ ...attrs, ...props, ...unref(getProps) } as Recordable)
      );
      const getSchema = computed((): FormSchema[] => {
@@ -104,11 +122,11 @@
          // handle date type
          if (defaultValue && dateItemType.includes(component)) {
            if (!Array.isArray(defaultValue)) {
              schema.defaultValue = moment(defaultValue);
              schema.defaultValue = dateUtil(defaultValue);
            } else {
              const def: moment.Moment[] = [];
              defaultValue.forEach((item) => {
                def.push(moment(item));
                def.push(dateUtil(item));
              });
              schema.defaultValue = def;
            }
@@ -126,11 +144,8 @@
        defaultValueRef,
      });
      const { transformDateFunc, fieldMapToTime, autoFocusFirstItem } = toRefs(props);
      const { handleFormValues, initDefault } = useFormValues({
        transformDateFuncRef: transformDateFunc,
        fieldMapToTimeRef: fieldMapToTime,
        getProps,
        defaultValueRef,
        getSchema,
        formModel,
@@ -138,7 +153,7 @@
      useAutoFocus({
        getSchema,
        autoFocusFirstItem,
        getProps,
        isInitedDefault: isInitedDefaultRef,
        formElRef: formElRef as Ref<FormActionType>,
      });
@@ -151,6 +166,7 @@
        validateFields,
        getFieldsValue,
        updateSchema,
        resetSchema,
        appendSchemaByField,
        removeSchemaByFiled,
        resetFields,
@@ -183,11 +199,22 @@
        }
      );
      const stopWatch: WatchStopHandle = watch(
      watch(
        () => unref(getProps).schemas,
        (schemas) => {
          resetSchema(schemas ?? []);
        }
      );
      watch(
        () => getSchema.value,
        (schema) => {
          nextTick(() => {
            //  Solve the problem of modal adaptive height calculation when the form is placed in the modal
            modalFn?.redoModalHeight?.();
          });
          if (unref(isInitedDefaultRef)) {
            return stopWatch();
            return;
          }
          if (schema?.length) {
            initDefault();
@@ -204,11 +231,23 @@
        formModel[key] = value;
      }
      function handleEnterPress(e: KeyboardEvent) {
        const { autoSubmitOnEnter } = unref(getProps);
        if (!autoSubmitOnEnter) return;
        if (e.key === 'Enter' && e.target && e.target instanceof HTMLElement) {
          const target: HTMLElement = e.target as HTMLElement;
          if (target && target.tagName && target.tagName.toUpperCase() == 'INPUT') {
            handleSubmit();
          }
        }
      }
      const formActionType: Partial<FormActionType> = {
        getFieldsValue,
        setFieldsValue,
        resetFields,
        updateSchema,
        resetSchema,
        setProps,
        removeSchemaByFiled,
        appendSchemaByField,
@@ -225,11 +264,13 @@
      });
      return {
        getBindValue,
        handleToggleAdvanced,
        handleEnterPress,
        formModel,
        defaultValueRef,
        advanceState,
        getRowWrapStyle,
        getRow,
        getProps,
        formElRef,
        getSchema,
@@ -243,7 +284,6 @@
  });
</script>
<style lang="less">
  @import (reference) '../../../design/index.less';
  @prefix-cls: ~'@{namespace}-basic-form';
  .@{prefix-cls} {
@@ -265,9 +305,16 @@
          display: flex;
        }
        .ant-form-item-control {
          margin-top: 4px;
        }
        .suffix {
          display: inline-block;
          display: inline-flex;
          padding-left: 6px;
          margin-top: 1px;
          line-height: 1;
          align-items: center;
        }
      }
    }