fix(upload): UploadFile type definition (#30343)

This commit is contained in:
Kermit 2021-04-28 22:52:31 +08:00 committed by GitHub
parent fa80ae5a0a
commit 4ccbf1fd0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 8 deletions

View File

@ -6,7 +6,7 @@ import PaperClipOutlined from '@ant-design/icons/PaperClipOutlined';
import PictureTwoTone from '@ant-design/icons/PictureTwoTone'; import PictureTwoTone from '@ant-design/icons/PictureTwoTone';
import FileTwoTone from '@ant-design/icons/FileTwoTone'; import FileTwoTone from '@ant-design/icons/FileTwoTone';
import { cloneElement, isValidElement } from '../../_util/reactNode'; import { cloneElement, isValidElement } from '../../_util/reactNode';
import { UploadListProps, UploadFile, UploadListType } from '../interface'; import { UploadListProps, UploadFile, UploadListType, InternalUploadFile } from '../interface';
import { previewImage, isImageUrl } from '../utils'; import { previewImage, isImageUrl } from '../utils';
import collapseMotion from '../../_util/motion'; import collapseMotion from '../../_util/motion';
import { ConfigContext } from '../../config-provider'; import { ConfigContext } from '../../config-provider';
@ -53,7 +53,7 @@ const InternalUploadList: React.ForwardRefRenderFunction<unknown, UploadListProp
if (listType !== 'picture' && listType !== 'picture-card') { if (listType !== 'picture' && listType !== 'picture-card') {
return; return;
} }
(items || []).forEach(file => { (items || []).forEach((file: InternalUploadFile) => {
if ( if (
typeof document === 'undefined' || typeof document === 'undefined' ||
typeof window === 'undefined' || typeof window === 'undefined' ||

View File

@ -46,4 +46,27 @@ describe('Upload.typescript', () => {
); );
expect(upload).toBeTruthy(); expect(upload).toBeTruthy();
}); });
it('defaultFileList/fileList', () => {
const fileList = [
{
uid: '-1',
name: 'xxx.png',
status: 'done' as const,
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
},
{
uid: '-2',
name: 'yyy.png',
status: 'error' as const,
},
];
const upload = (
<Upload fileList={fileList} defaultFileList={fileList}>
<span>click to upload</span>
</Upload>
);
expect(upload).toBeTruthy();
});
}); });

View File

@ -17,7 +17,7 @@ export interface HttpRequestHeader {
export interface UploadFile<T = any> { export interface UploadFile<T = any> {
uid: string; uid: string;
size: number; size?: number;
name: string; name: string;
fileName?: string; fileName?: string;
lastModified?: number; lastModified?: number;
@ -26,15 +26,19 @@ export interface UploadFile<T = any> {
status?: UploadFileStatus; status?: UploadFileStatus;
percent?: number; percent?: number;
thumbUrl?: string; thumbUrl?: string;
originFileObj: RcFile; originFileObj?: RcFile;
response?: T; response?: T;
error?: any; error?: any;
linkProps?: any; linkProps?: any;
type: string; type?: string;
xhr?: T; xhr?: T;
preview?: string; preview?: string;
} }
export interface InternalUploadFile<T = any> extends UploadFile<T> {
originFileObj: RcFile;
}
export interface UploadChangeParam<T extends object = UploadFile> { export interface UploadChangeParam<T extends object = UploadFile> {
// https://github.com/ant-design/ant-design/issues/14420 // https://github.com/ant-design/ant-design/issues/14420
file: T; file: T;
@ -86,7 +90,10 @@ export interface UploadProps<T = any> {
showUploadList?: boolean | ShowUploadListInterface; showUploadList?: boolean | ShowUploadListInterface;
multiple?: boolean; multiple?: boolean;
accept?: string; accept?: string;
beforeUpload?: (file: RcFile, FileList: RcFile[]) => boolean | string | Promise<void | Blob | File>; beforeUpload?: (
file: RcFile,
FileList: RcFile[],
) => boolean | string | Promise<void | Blob | File>;
onChange?: (info: UploadChangeParam) => void; onChange?: (info: UploadChangeParam) => void;
listType?: UploadListType; listType?: UploadListType;
className?: string; className?: string;

View File

@ -1,6 +1,6 @@
import { RcFile, UploadFile } from './interface'; import { RcFile, UploadFile, InternalUploadFile } from './interface';
export function file2Obj(file: RcFile): UploadFile { export function file2Obj(file: RcFile): InternalUploadFile {
return { return {
...file, ...file,
lastModified: file.lastModified, lastModified: file.lastModified,