Skip to content

Commit

Permalink
fix(useUpload): remove file
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Feb 16, 2024
1 parent 58faebd commit 06fc5fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
30 changes: 16 additions & 14 deletions packages/core/useUpload/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ref } from 'vue'
import { ref, type Ref } from 'vue'

/**
* TODO:
* - [ ] 允许并发上传数量,默认1
* - [ ] 允许设置失败重传次数,默认0
* - [ ] 限制可上传的文件大小,默认Infinity表示不限制
* - [ ] 清空文件列表 delete(index: number)
* - [ ] 清空文件列表 remove(index: number)
*/

interface UseUploadOptions {
Expand All @@ -28,7 +27,7 @@ interface UseUploadReturn {
upload: () => void
append: (file: File | File[]) => void
clearFiles: () => boolean
remove: (index: number[]) => boolean
remove: (index: number[] | number) => UseUploadFile[]
files: Ref<UseUploadFile[]>
}

Expand Down Expand Up @@ -125,10 +124,19 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
]
}

const remove = (index: number[] | number): UseUploadFile[] => {
if (index >= files.value.length) {
console.warn('cannot find file by index.')
return false
const remove = (index: number | number[]): UseUploadFile[] => {
let removeIndexes = []
if (Array.isArray(index)) {
removeIndexes = index
} else {
removeIndexes = [index]
}
if (
!removeIndexes.every((i) => {
return i < files.value.length
})
) {
throw new Error('Have a index out of range.')
}
if (Array.isArray(index)) {
const removeFiles = files.value.filter((_, i) => index.includes(i))
Expand All @@ -138,16 +146,10 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
return files.value.splice(index, 1)
}

const clear = (): boolean => {
files.value = []
return true
}

return {
upload,
files,
append,
remove,
clear,
}
}
8 changes: 2 additions & 6 deletions src/demo/UseUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { watch, ref, h, VNode } from 'vue'
import { useUpload } from '@noi/core'
const url = 'https://run.mocky.io/v3/da20c84f-fbe5-4510-8f24-80fa61474790'
const { upload, append, files } = useUpload({ url, maxSize: 100, maxCount: 3 })
const { upload, append, files, remove } = useUpload({ url, maxSize: 100 })
const inputChange = (event) => {
append(event.target.files)
Expand All @@ -13,11 +13,7 @@ const inputChange = (event) => {
<template>
<div v-for="(item, index) in files" :key="index">
<!-- <img :src="item.data" alt="" /> -->
{{ item.name }}
<br>
{{ item.ext }}
<br />
{{ item.status }}
{{ item.name }} | {{ item.ext }} | {{ item.status }} | <button @click="remove(index)">delete</button>
<hr />
</div>
<br />
Expand Down

0 comments on commit 06fc5fd

Please sign in to comment.