transferConfigRabbitMq.vue 6.22 KB
<template>
  <div class="root">
    <div class="root-form">
      <BasicForm :showSubmitButton="false" @register="register">
        <template #addKeyAndValue="{ field }">
          <span style="display: none">{{ field }}</span>
          <div>
            <div>
              <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
                style="
                  width: 7vw;
                  height: 3.3vh;
                  display: flex;
                  flex-direction: row;
                  justify-content: center;
                  align-items: center;
                  margin-left: 1.8vw;
                "
              >
                <div
                  style="
                    width: 2.9vw;
                    height: 3.3vh;
                    background-color: #0960bd;
                    border-radius: 1px;
                    cursor: pointer;
                    text-align: center;
                    line-height: 3.1vh;
                  "
                >
                  <span @click="addKeyAndValueFunc" style="color: white">添加</span>
                </div>
                <div
                  style="
                    width: 2.9vw;
                    height: 3.3vh;
                    margin-left: 1vw;
                    background-color: #ed6f6f;
                    border-radius: 1px;
                    cursor: pointer;
                    text-align: center;
                    line-height: 3.1vh;
                  "
                >
                  <span @click="removeKeyAndValueFunc" style="color: white">删除</span>
                </div>
              </div>
              <div> </div>
            </div>
          </div>
        </template>
      </BasicForm>
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { modeRabbitMqForm, 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 otherPropertiesValues = reactive({
        clientProperties: {},
      });

      const keyAndValueArrTemp = ref<[]>([]);
      const keyAndValueObj = reactive<IKeyAndValue>({
        key: '',
        value: '',
      });
      const keyAndValueArr = ref<[]>([]);
      const sonValues = reactive({
        configuration: {},
      });

      const [register, { validate, setFieldsValue, resetFields: defineClearFunc }] = useForm({
        labelWidth: 80,
        schemas: modeRabbitMqForm,
        actionColOptions: {
          span: 14,
        },
        resetButtonOptions: {
          text: '上一步',
        },

        resetFunc: customResetFunc,
        submitFunc: customSubmitFunc,
      });

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

      const setStepTwoFieldsValueFunc = (v, v1) => {
        setFieldsValue(v);
        setFieldsValue({
          name: v1,
        });
      };
      const customClearStepTwoValueFunc = async () => {
        defineClearFunc();
      };
      async function customResetFunc() {
        emit('prev');
      }
      async function customSubmitFunc() {
        try {
          const values = await validate();
          emit('next', values);
        } catch (error) {
        } finally {
        }
      }
      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 () => {
        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.clientProperties = kongTemp;
        Object.assign(sonValues.configuration, otherPropertiesValues);
        return sonValues;
      };

      return {
        getSonValueFunc,
        register,
        setStepTwoFieldsValueFunc,
        customClearStepTwoValueFunc,
        keyAndValueArr,
        registerKeyAndValue,
        addKeyAndValueFunc,
        removeKeyAndValueFunc,
      };
    },
  });
</script>
<style lang="less" scoped>
  .root {
    width: 47.55vw;
    border: 1px solid #d9d9d9;
    display: flex;
    margin-top: 1vh;
    margin-left: 1.5vw;
    border-radius: 8px;

    .root-form {
      width: 44vw;
      margin: 1vh 1vw;
      position: relative;
      :deep .ant-btn {
        position: absolute;
        right: 1vw;
        background-color: #0960bd;
        border-radius: 1px;
        span {
          color: white;
        }
      }
    }
  }
</style>