Skip to content

Commit

Permalink
fix(useUpload): append file
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Feb 16, 2024
1 parent 23f0d4b commit 58faebd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
46 changes: 26 additions & 20 deletions packages/core/useUpload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { ref } from 'vue'

interface UseUploadOptions {
url: string
multiple?: boolean
accept?: string
maxSize?: number // default infinity
maxCount?: number // default inifinity
}

interface UseUploadFile {
Expand Down Expand Up @@ -72,7 +73,7 @@ const genUploadFile = async (file: File): UseUploadFile => ({
})

export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
const { url, multiple = false, accept = '' } = options
const { url, accept = '', maxSize = Infinity, maxCount = Infinity } = options
const acceptList = accept.split(',').map((type) => type.trim())
const files = ref<UseUploadFile[]>([])

Expand All @@ -97,26 +98,31 @@ export const useUpload = (options: UseUploadOptions): UseUploadReturn => {
})
}

const append = async (file: File | File[]) => {
if (!multiple && Array.isArray(file)) {
console.warn(
'if you want upload multiple files, set options multiple is `true`'
)
}
if (multiple) {
if (!(file as File[]).every(validFileType)) {
throw new Error('Have some File reject')
}
files.value = [
...files.value,
...(await (file as File[]).map(async (f) => genUploadFile(f))),
]
const append = async (file: File | File[] | FileList) => {
let appendFiles = []
if (file instanceof FileList) {
appendFiles = Array.from(file)
} else if (Array.isArray(file)) {
appendFiles = file
} else if (file instanceof File) {
appendFiles = [file]
} else {
if (!validFileType(file as File)) {
throw new Error('File type reject.')
}
files.value.push(await genUploadFile(file))
// nothing
}
// check max size
if (files.value.length + appendFiles.length >= maxCount) {
throw new Error('Exceed the maximum number of files')
}
if (!(appendFiles as File[]).every(validFileType)) {
throw new Error('Have some File reject')
}

files.value = [
...files.value,
...(await Promise.all(
(appendFiles as File[]).map(async (f) => genUploadFile(f))
)),
]
}

const remove = (index: number[] | number): UseUploadFile[] => {
Expand Down
6 changes: 3 additions & 3 deletions src/demo/UseUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ 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 })
const { upload, append, files } = useUpload({ url, maxSize: 100, maxCount: 3 })
const inputChange = (event) => {
append(event.target.files[0])
append(event.target.files)
}
</script>

Expand All @@ -21,7 +21,7 @@ const inputChange = (event) => {
<hr />
</div>
<br />
<input accept="" type="file" @change="inputChange" />
<input accept="" type="file" @change="inputChange" multiple />
<br />
<button @click="upload">upload</button>
</template>

0 comments on commit 58faebd

Please sign in to comment.