transferConfigKafka.vue 5.02 KB
<template>
  <div class="transfer-config-mode">
    <BasicForm :showSubmitButton="false" @register="register">
      <template #addValue="{ field }">
        <span style="display: none">{{ field }}</span>
        <div>
          <div>
            <div v-if="keyAndValueArr.length > 0">
              <template v-for="(item, index) in keyAndValueArr" :key="index">
                <span style="display: none">{{ item + index }}</span>
                <BasicForm
                  :showResetButton="false"
                  :showSubmitButton="false"
                  @register="registerKeyAndValue"
                />
              </template>
            </div>
            <div>
              <a-button type="primary" class="mr-4" @click="addKeyAndValueFunc">添加</a-button>
              <a-button color="error" @click="removeKeyAndValueFunc">删除</a-button>
            </div>
          </div>
        </div>
      </template>
    </BasicForm>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive, nextTick } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { modeKafkaForm, modeKafkaInseretKeyAndValueForm } from '../config';
  import { Alert, Divider, Descriptions } from 'ant-design-vue';

  interface IKeyAndValue {
    key: string;
    value: string;
  }

  export default defineComponent({
    components: {
      BasicForm,
      [Alert.name]: Alert,
      [Divider.name]: Divider,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    emits: ['next', 'prev', 'register'],
    setup(_, { emit }) {
      const temp = ref({});
      let tempObj = ref({});
      const keyAndValueArr = ref<[]>([]);
      const keyAndValueArrTemp = ref<[]>([]);
      const vType = ref('');
      const keyAndValueObj = reactive<IKeyAndValue>({
        key: '',
        value: '',
      });
      const sonValues = reactive({
        configuration: {},
      });
      const otherPropertiesValues = reactive({
        otherProperties: {},
      });
      const clearName = ref('');

      const [register, { validate, setFieldsValue, resetFields: defineClearFunc, clearValidate }] =
        useForm({
          labelWidth: 80,
          schemas: modeKafkaForm,
          actionColOptions: {
            span: 14,
          },
          resetButtonOptions: {
            text: '上一步',
            type: 'primary',
          },
          resetFunc: customResetFunc,
        });

      const [
        registerKeyAndValue,
        { validate: validateKeyAndValue, resetFields: defineClearKeyAndValueFunc },
      ] = useForm({
        labelWidth: 80,
        schemas: modeKafkaInseretKeyAndValueForm,
        actionColOptions: {
          span: 14,
        },
      });

      const clearValidateFunc = async () => {
        await clearValidate(['name']);
      };

      const setStepTwoFieldsValueFunc = async (v, v1) => {
        clearName.value = v1;
        setFieldsValue(v);
        vType.value = v1;
        setFieldsValue({
          name: v1,
        });
      };

      const customClearStepTwoValueFunc = async () => {
        nextTick(() => {
          defineClearFunc();
          defineClearKeyAndValueFunc();
        });
      };
      async function customResetFunc() {
        emit('prev');
      }

      const tempGetKeyAndVal = async () => {
        temp.value = await validateKeyAndValue();
      };
      // const defaultAddKeyAndValueFunc = () => {
      //   if (keyAndValueArr.value.length == 0) {
      //     keyAndValueArr.value.push(keyAndValueObj as never);
      //   }
      // };
      // defaultAddKeyAndValueFunc();

      const getDefaultValue = async () => {
        await tempGetKeyAndVal();
        keyAndValueArrTemp.value.push(temp.value as never);
      };

      const addKeyAndValueFunc = async () => {
        keyAndValueArr.value.push(keyAndValueObj as never);
        await tempGetKeyAndVal();
        tempObj.value = temp.value;
        keyAndValueArrTemp.value.push(tempObj.value as never);
      };
      const removeKeyAndValueFunc = () => {
        keyAndValueArr.value.splice(0, 1);
      };

      const getSonValueFunc = async () => {
        try {
          sonValues.configuration = await validate();
          if (keyAndValueArrTemp.value.length != 0) {
            await getDefaultValue();
          }
          const kong = {};
          let kongTemp = {};
          keyAndValueArrTemp.value.map((item: any) => {
            kong[item.key] = item.value;
          });
          kongTemp = JSON.parse(JSON.stringify(kong));
          otherPropertiesValues.otherProperties = kongTemp;
          Object.assign(sonValues.configuration, otherPropertiesValues);
          return sonValues;
        } catch (e) {
          return e;
        }
      };
      return {
        clearValidateFunc,
        getSonValueFunc,
        keyAndValueArr,
        register,
        setStepTwoFieldsValueFunc,
        customClearStepTwoValueFunc,
        addKeyAndValueFunc,
        registerKeyAndValue,
        removeKeyAndValueFunc,
        customResetFunc,
      };
    },
  });
</script>