DeviceModal.vue 2.95 KB
<template>
  <BasicModal
    v-bind="$attrs"
    width="55rem"
    @register="register"
    :title="getTitle"
    @visible-change="handleVisibleChange"
    @cancel="handleCancel"
  >
    <div class="step-form-form">
      <a-steps :current="current">
        <a-step title="填写设备信息" />
        <a-step title="添加设备凭证" />
      </a-steps>
    </div>
    <div class="mt-5">
      <DeviceStep1 @next="handleStep1Next" v-show="current === 0" />
      <DeviceStep2
        @prev="handleStepPrev"
        @next="handleStep2Next"
        v-show="current === 1"
        v-if="initStep2"
      />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, nextTick, computed, unref, reactive, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import DeviceStep1 from '/@/views/device/step/DeviceStep1.vue';
  import DeviceStep2 from '/@/views/device/step/DeviceStep2.vue';
  import { Steps } from 'ant-design-vue';
  export default defineComponent({
    name: 'DeviceModal',
    components: {
      BasicModal,
      DeviceStep1,
      DeviceStep2,
      [Steps.name]: Steps,
      [Steps.Step.name]: Steps.Step,
    },
    props: {
      userData: { type: Object },
    },
    setup(props) {
      const state = reactive({
        initStep2: false,
      });
      const current = ref(0);
      const isUpdate = ref(true);
      const modelRef = ref({});
      const getTitle = computed(() => (!unref(isUpdate) ? '新增设备' : '编辑设备'));

      const [register] = useModalInner((data) => {
        isUpdate.value = !!data?.isUpdate;
        data && onDataReceive(data);
      });

      function handleStepPrev() {
        current.value--;
      }
      function handleStep1Next(step1Values: any) {
        current.value++;
        state.initStep2 = true;
        console.log(step1Values);
      }
      function handleStep2Next(step2Values: any) {
        current.value++;
        console.log(step2Values);
      }
      function handleRedo() {
        current.value = 0;
        state.initStep2 = false;
      }
      function handleCancel() {
        console.log('关闭了弹框');
      }

      function onDataReceive(data) {
        console.log('Data Received', data);
        // 方式1;
        // setFieldsValue({
        //   field2: data.data,
        //   field1: data.info,
        // });

        // // 方式2
        modelRef.value = { field2: data.data, field1: data.info };

        // setProps({
        //   model:{ field2: data.data, field1: data.info }
        // })
      }

      function handleVisibleChange(v) {
        v && props.userData && nextTick(() => onDataReceive(props.userData));
      }

      return {
        register,
        model: modelRef,
        getTitle,
        handleVisibleChange,
        current,
        ...toRefs(state),
        handleStepPrev,
        handleStep1Next,
        handleStep2Next,
        handleCancel,
        handleRedo,
      };
    },
  });
</script>