fix typing for `Upload.beforeUpload` (#10059)

* fix typing for `Upload.beforeUpload`
fix #9775

* add RcFile interface, fix tests

* fix typing of `Upload.onStart`
pull/10138/merge
Wu Haotian 7 years ago committed by 偏右
parent d0d69fbf13
commit 7eb2792072

@ -7,6 +7,7 @@ import defaultLocale from '../locale-provider/default';
import Dragger from './Dragger'; import Dragger from './Dragger';
import UploadList from './UploadList'; import UploadList from './UploadList';
import { import {
RcFile,
UploadProps, UploadProps,
UploadState, UploadState,
UploadFile, UploadFile,
@ -55,7 +56,7 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
this.clearProgressTimer(); this.clearProgressTimer();
} }
onStart = (file: UploadFile) => { onStart = (file: RcFile) => {
let targetItem; let targetItem;
let nextFileList = this.state.fileList.concat(); let nextFileList = this.state.fileList.concat();
targetItem = fileToObject(file); targetItem = fileToObject(file);
@ -187,15 +188,15 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
}); });
} }
beforeUpload = (file: UploadFile, fileList: UploadFile[]) => { beforeUpload = (file: RcFile, fileList: RcFile[]) => {
if (!this.props.beforeUpload) { if (!this.props.beforeUpload) {
return true; return true;
} }
const result = this.props.beforeUpload(file, fileList); const result = this.props.beforeUpload(file, fileList);
if (result === false) { if (result === false) {
this.onChange({ this.onChange({
file, file: fileToObject(file),
fileList: uniqBy(fileList.concat(this.state.fileList), (item: any) => item.uid), fileList: uniqBy(fileList.map(fileToObject).concat(this.state.fileList), (item: UploadFile) => item.uid),
}); });
return false; return false;
} else if (result && (result as PromiseLike<any>).then) { } else if (result && (result as PromiseLike<any>).then) {

@ -48,7 +48,7 @@ describe('Upload', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { file: 'foo.png' },
], ],
}, },
}); });
@ -75,7 +75,7 @@ describe('Upload', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { file: 'foo.png' },
], ],
}, },
}); });
@ -102,7 +102,7 @@ describe('Upload', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { file: 'foo.png' },
], ],
}, },
}); });

@ -86,7 +86,7 @@ describe('Upload List', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { name: 'foo.png' },
], ],
}, },
}); });
@ -112,7 +112,7 @@ describe('Upload List', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { name: 'foo.png' },
], ],
}, },
}); });
@ -134,7 +134,7 @@ describe('Upload List', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { name: 'foo.png' },
], ],
}, },
}); });
@ -196,7 +196,7 @@ describe('Upload List', () => {
wrapper.find('input').simulate('change', { wrapper.find('input').simulate('change', {
target: { target: {
files: [ files: [
{ filename: 'foo.png' }, { name: 'foo.png' },
], ],
}, },
}); });

@ -6,12 +6,16 @@ export interface HttpRequestHeader {
[key: string]: string; [key: string]: string;
} }
export interface RcFile extends File {
uid: number;
}
export interface UploadFile { export interface UploadFile {
uid: number; uid: number;
size: number; size: number;
name: string; name: string;
filename?: string; filename?: string;
lastModified?: string; lastModified?: number;
lastModifiedDate?: Date; lastModifiedDate?: Date;
url?: string; url?: string;
status?: UploadFileStatus; status?: UploadFileStatus;
@ -57,7 +61,7 @@ export interface UploadProps {
showUploadList?: boolean | ShowUploadListInterface; showUploadList?: boolean | ShowUploadListInterface;
multiple?: boolean; multiple?: boolean;
accept?: string; accept?: string;
beforeUpload?: (file: UploadFile, FileList: UploadFile[]) => boolean | PromiseLike<any>; beforeUpload?: (file: RcFile, FileList: RcFile[]) => boolean | PromiseLike<any>;
onChange?: (info: UploadChangeParam) => void; onChange?: (info: UploadChangeParam) => void;
listType?: UploadListType; listType?: UploadListType;
className?: string; className?: string;

@ -1,4 +1,4 @@
import { UploadFile } from './interface'; import { RcFile, UploadFile } from './interface';
export function T() { export function T() {
return true; return true;
@ -6,18 +6,16 @@ export function T() {
// Fix IE file.status problem // Fix IE file.status problem
// via coping a new Object // via coping a new Object
export function fileToObject(file: UploadFile) { export function fileToObject(file: RcFile): UploadFile {
return { return {
lastModified: file.lastModified, lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate, lastModifiedDate: file.lastModifiedDate,
name: file.filename || file.name, name: file.name,
size: file.size, size: file.size,
type: file.type, type: file.type,
uid: file.uid,
response: file.response,
error: file.error,
percent: 0, percent: 0,
originFileObj: file as (File | UploadFile), uid: file.uid,
originFileObj: file,
} as UploadFile; } as UploadFile;
} }

Loading…
Cancel
Save