浏览代码

Feat/cancel event (#2593)

* feat(excel): add cancel event for import excel

* feat(excel): add cancel emits
yx lin 2 年之前
父节点
当前提交
714a351036
共有 1 个文件被更改,包括 21 次插入2 次删除
  1. 21 2
      src/components/Excel/src/ImportExcel.vue

+ 21 - 2
src/components/Excel/src/ImportExcel.vue

@@ -37,10 +37,11 @@
         default: false,
         default: false,
       },
       },
     },
     },
-    emits: ['success', 'error'],
+    emits: ['success', 'error', 'cancel'],
     setup(props, { emit }) {
     setup(props, { emit }) {
       const inputRef = ref<HTMLInputElement | null>(null);
       const inputRef = ref<HTMLInputElement | null>(null);
       const loadingRef = ref<Boolean>(false);
       const loadingRef = ref<Boolean>(false);
+      const cancelRef = ref<Boolean>(true);
 
 
       function shapeWorkSheel(sheet: XLSX.WorkSheet, range: XLSX.Range) {
       function shapeWorkSheel(sheet: XLSX.WorkSheet, range: XLSX.Range) {
         let str = ' ',
         let str = ' ',
@@ -184,6 +185,7 @@
 
 
         if (!rawFile) return;
         if (!rawFile) return;
 
 
+        cancelRef.value = false;
         if (props.isReturnFile) {
         if (props.isReturnFile) {
           emit('success', rawFile);
           emit('success', rawFile);
           return;
           return;
@@ -192,11 +194,28 @@
       }
       }
 
 
       /**
       /**
+       * @description 文件选择器关闭后,判断取消状态
+       */
+       function handleFocusChange() {
+        const timeId = setInterval(() => {
+          if (cancelRef.value === true) {
+            emit('cancel');
+          }
+          clearInterval(timeId);
+          window.removeEventListener('focus', handleFocusChange);
+        }, 1000);
+      }
+
+      /**
        * @description: 点击上传按钮
        * @description: 点击上传按钮
        */
        */
       function handleUpload() {
       function handleUpload() {
         const inputRefDom = unref(inputRef);
         const inputRefDom = unref(inputRef);
-        inputRefDom && inputRefDom.click();
+        if (inputRefDom) {
+          cancelRef.value = true;
+          inputRefDom.click();
+          window.addEventListener('focus', handleFocusChange);
+        }
       }
       }
 
 
       return { handleUpload, handleInputClick, inputRef };
       return { handleUpload, handleInputClick, inputRef };