From 7eb2792072045b5f98c094450dc062327e8b144b Mon Sep 17 00:00:00 2001 From: Wu Haotian Date: Thu, 19 Apr 2018 11:54:28 +0800 Subject: [PATCH] fix typing for `Upload.beforeUpload` (#10059) * fix typing for `Upload.beforeUpload` fix #9775 * add RcFile interface, fix tests * fix typing of `Upload.onStart` --- components/upload/Upload.tsx | 9 +++++---- components/upload/__tests__/upload.test.js | 6 +++--- components/upload/__tests__/uploadlist.test.js | 8 ++++---- components/upload/interface.tsx | 8 ++++++-- components/upload/utils.tsx | 12 +++++------- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/components/upload/Upload.tsx b/components/upload/Upload.tsx index 5be18321c8..638c329146 100644 --- a/components/upload/Upload.tsx +++ b/components/upload/Upload.tsx @@ -7,6 +7,7 @@ import defaultLocale from '../locale-provider/default'; import Dragger from './Dragger'; import UploadList from './UploadList'; import { + RcFile, UploadProps, UploadState, UploadFile, @@ -55,7 +56,7 @@ export default class Upload extends React.Component { this.clearProgressTimer(); } - onStart = (file: UploadFile) => { + onStart = (file: RcFile) => { let targetItem; let nextFileList = this.state.fileList.concat(); targetItem = fileToObject(file); @@ -187,15 +188,15 @@ export default class Upload extends React.Component { }); } - beforeUpload = (file: UploadFile, fileList: UploadFile[]) => { + beforeUpload = (file: RcFile, fileList: RcFile[]) => { if (!this.props.beforeUpload) { return true; } const result = this.props.beforeUpload(file, fileList); if (result === false) { this.onChange({ - file, - fileList: uniqBy(fileList.concat(this.state.fileList), (item: any) => item.uid), + file: fileToObject(file), + fileList: uniqBy(fileList.map(fileToObject).concat(this.state.fileList), (item: UploadFile) => item.uid), }); return false; } else if (result && (result as PromiseLike).then) { diff --git a/components/upload/__tests__/upload.test.js b/components/upload/__tests__/upload.test.js index babf98e939..6ef892114f 100644 --- a/components/upload/__tests__/upload.test.js +++ b/components/upload/__tests__/upload.test.js @@ -48,7 +48,7 @@ describe('Upload', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { file: 'foo.png' }, ], }, }); @@ -75,7 +75,7 @@ describe('Upload', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { file: 'foo.png' }, ], }, }); @@ -102,7 +102,7 @@ describe('Upload', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { file: 'foo.png' }, ], }, }); diff --git a/components/upload/__tests__/uploadlist.test.js b/components/upload/__tests__/uploadlist.test.js index ff22fb108a..8b990a4f7a 100644 --- a/components/upload/__tests__/uploadlist.test.js +++ b/components/upload/__tests__/uploadlist.test.js @@ -86,7 +86,7 @@ describe('Upload List', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { name: 'foo.png' }, ], }, }); @@ -112,7 +112,7 @@ describe('Upload List', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { name: 'foo.png' }, ], }, }); @@ -134,7 +134,7 @@ describe('Upload List', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { name: 'foo.png' }, ], }, }); @@ -196,7 +196,7 @@ describe('Upload List', () => { wrapper.find('input').simulate('change', { target: { files: [ - { filename: 'foo.png' }, + { name: 'foo.png' }, ], }, }); diff --git a/components/upload/interface.tsx b/components/upload/interface.tsx index 03cb80ff91..64370c5b44 100755 --- a/components/upload/interface.tsx +++ b/components/upload/interface.tsx @@ -6,12 +6,16 @@ export interface HttpRequestHeader { [key: string]: string; } +export interface RcFile extends File { + uid: number; +} + export interface UploadFile { uid: number; size: number; name: string; filename?: string; - lastModified?: string; + lastModified?: number; lastModifiedDate?: Date; url?: string; status?: UploadFileStatus; @@ -57,7 +61,7 @@ export interface UploadProps { showUploadList?: boolean | ShowUploadListInterface; multiple?: boolean; accept?: string; - beforeUpload?: (file: UploadFile, FileList: UploadFile[]) => boolean | PromiseLike; + beforeUpload?: (file: RcFile, FileList: RcFile[]) => boolean | PromiseLike; onChange?: (info: UploadChangeParam) => void; listType?: UploadListType; className?: string; diff --git a/components/upload/utils.tsx b/components/upload/utils.tsx index 27d68fe87e..83ecf70bb2 100644 --- a/components/upload/utils.tsx +++ b/components/upload/utils.tsx @@ -1,4 +1,4 @@ -import { UploadFile } from './interface'; +import { RcFile, UploadFile } from './interface'; export function T() { return true; @@ -6,18 +6,16 @@ export function T() { // Fix IE file.status problem // via coping a new Object -export function fileToObject(file: UploadFile) { +export function fileToObject(file: RcFile): UploadFile { return { lastModified: file.lastModified, lastModifiedDate: file.lastModifiedDate, - name: file.filename || file.name, + name: file.name, size: file.size, type: file.type, - uid: file.uid, - response: file.response, - error: file.error, percent: 0, - originFileObj: file as (File | UploadFile), + uid: file.uid, + originFileObj: file, } as UploadFile; }