From 714a351036042ae9883d821e99feed9802aa8d22 Mon Sep 17 00:00:00 2001 From: yx lin Date: Fri, 3 Mar 2023 13:14:01 +0800 Subject: [PATCH] Feat/cancel event (#2593) * feat(excel): add cancel event for import excel * feat(excel): add cancel emits --- src/components/Excel/src/ImportExcel.vue | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/components/Excel/src/ImportExcel.vue b/src/components/Excel/src/ImportExcel.vue index 3ed98a987..59d7f578b 100644 --- a/src/components/Excel/src/ImportExcel.vue +++ b/src/components/Excel/src/ImportExcel.vue @@ -37,10 +37,11 @@ default: false, }, }, - emits: ['success', 'error'], + emits: ['success', 'error', 'cancel'], setup(props, { emit }) { const inputRef = ref(null); const loadingRef = ref(false); + const cancelRef = ref(true); function shapeWorkSheel(sheet: XLSX.WorkSheet, range: XLSX.Range) { let str = ' ', @@ -184,6 +185,7 @@ if (!rawFile) return; + cancelRef.value = false; if (props.isReturnFile) { emit('success', rawFile); return; @@ -191,12 +193,29 @@ upload(rawFile); } + /** + * @description 文件选择器关闭后,判断取消状态 + */ + function handleFocusChange() { + const timeId = setInterval(() => { + if (cancelRef.value === true) { + emit('cancel'); + } + clearInterval(timeId); + window.removeEventListener('focus', handleFocusChange); + }, 1000); + } + /** * @description: 点击上传按钮 */ function handleUpload() { const inputRefDom = unref(inputRef); - inputRefDom && inputRefDom.click(); + if (inputRefDom) { + cancelRef.value = true; + inputRefDom.click(); + window.addEventListener('focus', handleFocusChange); + } } return { handleUpload, handleInputClick, inputRef };