Commit 714a351036042ae9883d821e99feed9802aa8d22
Committed by
GitHub
1 parent
a78c3a30
Feat/cancel event (#2593)
* feat(excel): add cancel event for import excel * feat(excel): add cancel emits
Showing
1 changed file
with
21 additions
and
2 deletions
src/components/Excel/src/ImportExcel.vue
@@ -37,10 +37,11 @@ | @@ -37,10 +37,11 @@ | ||
37 | default: false, | 37 | default: false, |
38 | }, | 38 | }, |
39 | }, | 39 | }, |
40 | - emits: ['success', 'error'], | 40 | + emits: ['success', 'error', 'cancel'], |
41 | setup(props, { emit }) { | 41 | setup(props, { emit }) { |
42 | const inputRef = ref<HTMLInputElement | null>(null); | 42 | const inputRef = ref<HTMLInputElement | null>(null); |
43 | const loadingRef = ref<Boolean>(false); | 43 | const loadingRef = ref<Boolean>(false); |
44 | + const cancelRef = ref<Boolean>(true); | ||
44 | 45 | ||
45 | function shapeWorkSheel(sheet: XLSX.WorkSheet, range: XLSX.Range) { | 46 | function shapeWorkSheel(sheet: XLSX.WorkSheet, range: XLSX.Range) { |
46 | let str = ' ', | 47 | let str = ' ', |
@@ -184,6 +185,7 @@ | @@ -184,6 +185,7 @@ | ||
184 | 185 | ||
185 | if (!rawFile) return; | 186 | if (!rawFile) return; |
186 | 187 | ||
188 | + cancelRef.value = false; | ||
187 | if (props.isReturnFile) { | 189 | if (props.isReturnFile) { |
188 | emit('success', rawFile); | 190 | emit('success', rawFile); |
189 | return; | 191 | return; |
@@ -192,11 +194,28 @@ | @@ -192,11 +194,28 @@ | ||
192 | } | 194 | } |
193 | 195 | ||
194 | /** | 196 | /** |
197 | + * @description 文件选择器关闭后,判断取消状态 | ||
198 | + */ | ||
199 | + function handleFocusChange() { | ||
200 | + const timeId = setInterval(() => { | ||
201 | + if (cancelRef.value === true) { | ||
202 | + emit('cancel'); | ||
203 | + } | ||
204 | + clearInterval(timeId); | ||
205 | + window.removeEventListener('focus', handleFocusChange); | ||
206 | + }, 1000); | ||
207 | + } | ||
208 | + | ||
209 | + /** | ||
195 | * @description: 点击上传按钮 | 210 | * @description: 点击上传按钮 |
196 | */ | 211 | */ |
197 | function handleUpload() { | 212 | function handleUpload() { |
198 | const inputRefDom = unref(inputRef); | 213 | const inputRefDom = unref(inputRef); |
199 | - inputRefDom && inputRefDom.click(); | 214 | + if (inputRefDom) { |
215 | + cancelRef.value = true; | ||
216 | + inputRefDom.click(); | ||
217 | + window.addEventListener('focus', handleFocusChange); | ||
218 | + } | ||
200 | } | 219 | } |
201 | 220 | ||
202 | return { handleUpload, handleInputClick, inputRef }; | 221 | return { handleUpload, handleInputClick, inputRef }; |