transferConfigKafka.vue 6.6 KB
<template>
  <div class="root">
    <div class="root-form">
      <BasicForm :showResetButton="false" :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
                style="
                  width: 5vw;
                  height: 3vh;
                  display: flex;
                  flex-direction: row;
                  justify-content: center;
                  align-items: center;
                  margin-left: 1.8vw;
                "
              >
                <div
                  style="
                    width: 2.6vw;
                    height: 3vh;
                    background-color: #0960bd;
                    border-radius: 10px;
                    cursor: pointer;
                    text-align: center;
                    line-height: 2.89vh;
                  "
                >
                  <span @click="addKeyAndValueFunc" style="color: white">添加</span>
                </div>
                <div
                  style="
                    width: 2.6vw;
                    height: 3vh;
                    margin-left: 1vw;
                    background-color: #ed6f6f;
                    border-radius: 10px;
                    cursor: pointer;
                    text-align: center;
                    line-height: 2.89vh;
                  "
                >
                  <span @click="removeKeyAndValueFunc" style="color: white">删除</span>
                </div>
              </div>
              <div> </div>
            </div>
          </div>
        </template>
      </BasicForm>
      <div
        style="
          width: 2.6vw;
          height: 3vh;
          margin-left: 19vw;
          margin-top: 2vh;
          background-color: #0960bd;
          border-radius: 10px;
          cursor: pointer;
          text-align: center;
          line-height: 2.89vh;
        "
      >
        <span @click="customResetFunc" style="color: white">上一步</span>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive } from 'vue';
  import { BasicForm, useForm, FormActionType } 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 [register, { validate, setFieldsValue }] = useForm({
        labelWidth: 80,
        schemas: modeKafkaForm,
        actionColOptions: {
          span: 14,
        },
        resetButtonOptions: {
          text: '上一步',
        },
        resetFunc: customResetFunc,
      });

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

      const setStepTwoFieldsValueFunc = (v, v1) => {
        setFieldsValue(v);
        vType.value = v1;
        setFieldsValue({
          name: v1,
        });
      };
      const customClearStepTwoValueFunc = () => {
        console.log('clear');
        setTimeout(() => {
          resetFields();
        }, 100);
      };
      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) => {
            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 {
        getSonValueFunc,
        keyAndValueArr,
        register,
        setStepTwoFieldsValueFunc,
        customClearStepTwoValueFunc,
        addKeyAndValueFunc,
        registerKeyAndValue,
        removeKeyAndValueFunc,
        customResetFunc,
      };
    },
  });
</script>
<style lang="less" scoped>
  .root {
    width: 45.55vw;
    border: 1px solid #bfbfbf;
    display: flex;
    margin-top: 1vh;
    border-radius: 8px;
    .root-form {
      width: 44vw;
      margin: 1vh 1vw;
      position: relative;
      // :deep .ant-btn {
      //   position: absolute;
      //   right: 1vw;
      //   top: 1vh;
      // }
    }
  }
</style>