diff --git a/packages/vant/src/uploader/demo/index.vue b/packages/vant/src/uploader/demo/index.vue index 2843a8c96..f03ca5b5b 100644 --- a/packages/vant/src/uploader/demo/index.vue +++ b/packages/vant/src/uploader/demo/index.vue @@ -56,7 +56,7 @@ const fileList3 = ref([]); const fileList4 = ref([{ url: 'https://img.yzcdn.cn/vant/sand.jpg' }]); -const fileList5 = ref([ +const fileList5 = ref([ { url: 'https://img.yzcdn.cn/vant/leaf.jpg' }, { url: 'https://img.yzcdn.cn/vant/sand.jpg', @@ -73,7 +73,7 @@ const fileList5 = ref([ }, ]); -const statusFileList = ref([ +const statusFileList = ref([ { url: 'https://img.yzcdn.cn/vant/leaf.jpg', status: 'uploading', @@ -86,16 +86,19 @@ const statusFileList = ref([ }, ]); -const previewCoverFiles = ref([ +const previewCoverFiles = ref([ { url: 'https://img.yzcdn.cn/vant/leaf.jpg', file: { name: t('imageName'), - }, + } as File, }, ]); -const beforeRead = (file: File) => { +const beforeRead = (file: File | File[]) => { + if (Array.isArray(file)) { + return true; + } if (file.type !== 'image/jpeg') { Toast(t('invalidType')); return false; @@ -103,11 +106,14 @@ const beforeRead = (file: File) => { return true; }; -const afterRead = (file: UploaderFileListItem, detail: unknown) => { +const afterRead = ( + file: UploaderFileListItem | UploaderFileListItem[], + detail: unknown +) => { console.log(file, detail); }; -const afterReadFailed = (item: UploaderFileListItem) => { +const setItemLoading = (item: UploaderFileListItem) => { item.status = 'uploading'; item.message = t('uploading'); @@ -117,6 +123,16 @@ const afterReadFailed = (item: UploaderFileListItem) => { }, 1000); }; +const afterReadFailed = ( + item: UploaderFileListItem | UploaderFileListItem[] +) => { + if (Array.isArray(item)) { + item.forEach(setItemLoading); + } else { + setItemLoading(item); + } +}; + const onOversize = (file: UploaderFileListItem, detail: unknown) => { console.log(file, detail); Toast(t('overSizeTip')); diff --git a/packages/vant/src/uploader/types.ts b/packages/vant/src/uploader/types.ts index 5b8530918..883181ce3 100644 --- a/packages/vant/src/uploader/types.ts +++ b/packages/vant/src/uploader/types.ts @@ -20,15 +20,13 @@ export type UploaderFileListItem = { export type UploaderMaxSize = number | string | ((file: File) => boolean); -type PromiseOrNot = T | Promise; - export type UploaderBeforeRead = ( file: File | File[], detail: { name: string | number; index: number; } -) => PromiseOrNot; +) => boolean | Promise; export type UploaderAfterRead = ( items: UploaderFileListItem | UploaderFileListItem[], diff --git a/packages/vant/src/utils/interceptor.ts b/packages/vant/src/utils/interceptor.ts index 6644642d1..87b5c813f 100644 --- a/packages/vant/src/utils/interceptor.ts +++ b/packages/vant/src/utils/interceptor.ts @@ -3,7 +3,7 @@ import { isPromise } from './validate'; export type Interceptor = ( ...args: any[] -) => Promise | boolean | undefined; +) => Promise | boolean | undefined | void; export function callInterceptor( interceptor: Interceptor | undefined,