diff --git a/docs/examples/asyncAction.tsx b/docs/examples/asyncAction.tsx index 5aff04a..c523cf3 100644 --- a/docs/examples/asyncAction.tsx +++ b/docs/examples/asyncAction.tsx @@ -4,7 +4,7 @@ import Upload from 'rc-upload'; const props = { action: () => { - return new Promise(resolve => { + return new Promise(resolve => { setTimeout(() => { resolve('/upload.do'); }, 2000); diff --git a/docs/examples/beforeUpload.tsx b/docs/examples/beforeUpload.tsx index fe78577..cefd775 100644 --- a/docs/examples/beforeUpload.tsx +++ b/docs/examples/beforeUpload.tsx @@ -1,10 +1,10 @@ /* eslint no-console:0 */ -import React from 'react'; +import { Action } from '@/interface'; import Upload from 'rc-upload'; const props = { - action: '/upload.do', + action: '/upload.do' as Action, multiple: true, onStart(file) { console.log('onStart', file, file.name); @@ -17,7 +17,7 @@ const props = { }, beforeUpload(file, fileList) { console.log(file, fileList); - return new Promise(resolve => { + return new Promise(resolve => { console.log('start check'); setTimeout(() => { console.log('check finshed'); diff --git a/docs/examples/customRequest.tsx b/docs/examples/customRequest.tsx index 83c13af..afe58fd 100644 --- a/docs/examples/customRequest.tsx +++ b/docs/examples/customRequest.tsx @@ -2,6 +2,7 @@ import React from 'react'; import axios from 'axios'; import Upload from 'rc-upload'; +import { UploadRequestOption } from '@/interface'; const uploadProps = { action: '/upload.do', @@ -32,13 +33,13 @@ const uploadProps = { onProgress, onSuccess, withCredentials, - }) { + }: UploadRequestOption) { // EXAMPLE: post form-data with 'axios' // eslint-disable-next-line no-undef const formData = new FormData(); if (data) { Object.keys(data).forEach(key => { - formData.append(key, data[key]); + formData.append(key, data[key] as string); }); } formData.append(filename, file); @@ -48,7 +49,7 @@ const uploadProps = { withCredentials, headers, onUploadProgress: ({ total, loaded }) => { - onProgress({ percent: Math.round((loaded / total) * 100).toFixed(2) }, file); + onProgress({ percent: Number(Math.round((loaded / total) * 100).toFixed(2)) }, file); }, }) .then(({ data: response }) => { diff --git a/src/interface.tsx b/src/interface.tsx index 9d02f27..1c6e1d5 100644 --- a/src/interface.tsx +++ b/src/interface.tsx @@ -29,7 +29,7 @@ export interface UploadProps file: RcFile, FileList: RcFile[], ) => BeforeUploadFileType | Promise | void; - customRequest?: (option: UploadRequestOption) => void; + customRequest?: (option: UploadRequestOption) => void | { abort: () => void }; withCredentials?: boolean; openFileDialogOnClick?: boolean; prefixCls?: string; @@ -54,6 +54,8 @@ export type UploadRequestMethod = 'POST' | 'PUT' | 'PATCH' | 'post' | 'put' | 'p export type UploadRequestHeader = Record; +export type UploadRequestFile = Exclude | RcFile; + export interface UploadRequestError extends Error { status?: number; method?: UploadRequestMethod; @@ -61,12 +63,12 @@ export interface UploadRequestError extends Error { } export interface UploadRequestOption { - onProgress?: (event: UploadProgressEvent) => void; + onProgress?: (event: UploadProgressEvent, file?: UploadRequestFile) => void; onError?: (event: UploadRequestError | ProgressEvent, body?: T) => void; - onSuccess?: (body: T, xhr?: XMLHttpRequest) => void; + onSuccess?: (body: T, fileOrXhr?: UploadRequestFile | XMLHttpRequest) => void; data?: Record; filename?: string; - file: Exclude | RcFile; + file: UploadRequestFile; withCredentials?: boolean; action: string; headers?: UploadRequestHeader;