types(Uploader): fix UploaderBeforeRead type (#10274)

This commit is contained in:
neverland 2022-02-09 14:35:37 +08:00 committed by GitHub
parent 9b7af21bbd
commit 7630bb2c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -56,7 +56,7 @@ const fileList3 = ref([]);
const fileList4 = ref([{ url: 'https://img.yzcdn.cn/vant/sand.jpg' }]); const fileList4 = ref([{ url: 'https://img.yzcdn.cn/vant/sand.jpg' }]);
const fileList5 = ref([ const fileList5 = ref<UploaderFileListItem[]>([
{ url: 'https://img.yzcdn.cn/vant/leaf.jpg' }, { url: 'https://img.yzcdn.cn/vant/leaf.jpg' },
{ {
url: 'https://img.yzcdn.cn/vant/sand.jpg', url: 'https://img.yzcdn.cn/vant/sand.jpg',
@ -73,7 +73,7 @@ const fileList5 = ref([
}, },
]); ]);
const statusFileList = ref([ const statusFileList = ref<UploaderFileListItem[]>([
{ {
url: 'https://img.yzcdn.cn/vant/leaf.jpg', url: 'https://img.yzcdn.cn/vant/leaf.jpg',
status: 'uploading', status: 'uploading',
@ -86,16 +86,19 @@ const statusFileList = ref([
}, },
]); ]);
const previewCoverFiles = ref([ const previewCoverFiles = ref<UploaderFileListItem[]>([
{ {
url: 'https://img.yzcdn.cn/vant/leaf.jpg', url: 'https://img.yzcdn.cn/vant/leaf.jpg',
file: { file: {
name: t('imageName'), 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') { if (file.type !== 'image/jpeg') {
Toast(t('invalidType')); Toast(t('invalidType'));
return false; return false;
@ -103,11 +106,14 @@ const beforeRead = (file: File) => {
return true; return true;
}; };
const afterRead = (file: UploaderFileListItem, detail: unknown) => { const afterRead = (
file: UploaderFileListItem | UploaderFileListItem[],
detail: unknown
) => {
console.log(file, detail); console.log(file, detail);
}; };
const afterReadFailed = (item: UploaderFileListItem) => { const setItemLoading = (item: UploaderFileListItem) => {
item.status = 'uploading'; item.status = 'uploading';
item.message = t('uploading'); item.message = t('uploading');
@ -117,6 +123,16 @@ const afterReadFailed = (item: UploaderFileListItem) => {
}, 1000); }, 1000);
}; };
const afterReadFailed = (
item: UploaderFileListItem | UploaderFileListItem[]
) => {
if (Array.isArray(item)) {
item.forEach(setItemLoading);
} else {
setItemLoading(item);
}
};
const onOversize = (file: UploaderFileListItem, detail: unknown) => { const onOversize = (file: UploaderFileListItem, detail: unknown) => {
console.log(file, detail); console.log(file, detail);
Toast(t('overSizeTip')); Toast(t('overSizeTip'));

View File

@ -20,15 +20,13 @@ export type UploaderFileListItem = {
export type UploaderMaxSize = number | string | ((file: File) => boolean); export type UploaderMaxSize = number | string | ((file: File) => boolean);
type PromiseOrNot<T> = T | Promise<T>;
export type UploaderBeforeRead = ( export type UploaderBeforeRead = (
file: File | File[], file: File | File[],
detail: { detail: {
name: string | number; name: string | number;
index: number; index: number;
} }
) => PromiseOrNot<File | File[] | undefined>; ) => boolean | Promise<File | File[] | undefined>;
export type UploaderAfterRead = ( export type UploaderAfterRead = (
items: UploaderFileListItem | UploaderFileListItem[], items: UploaderFileListItem | UploaderFileListItem[],

View File

@ -3,7 +3,7 @@ import { isPromise } from './validate';
export type Interceptor = ( export type Interceptor = (
...args: any[] ...args: any[]
) => Promise<boolean> | boolean | undefined; ) => Promise<boolean> | boolean | undefined | void;
export function callInterceptor( export function callInterceptor(
interceptor: Interceptor | undefined, interceptor: Interceptor | undefined,