Skip to content

Commit

Permalink
fix: Upload data type (ant-design#33193)
Browse files Browse the repository at this point in the history
* fix: Upload `data` type

* fix eslint errors
  • Loading branch information
afc163 authored Dec 7, 2021
1 parent eb3cf79 commit 2df7cc6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
10 changes: 5 additions & 5 deletions components/upload/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
let clone;

try {
clone = (new File([originFileObj], originFileObj.name, {
clone = new File([originFileObj], originFileObj.name, {
type: originFileObj.type,
}) as any) as UploadFile;
}) as any as UploadFile;
} catch (e) {
clone = (new Blob([originFileObj], {
clone = new Blob([originFileObj], {
type: originFileObj.type,
}) as any) as UploadFile;
}) as any as UploadFile;
clone.name = originFileObj.name;
clone.lastModifiedDate = new Date();
clone.lastModified = new Date().getTime();
Expand Down Expand Up @@ -305,7 +305,7 @@ const InternalUpload: React.ForwardRefRenderFunction<unknown, UploadProps> = (pr
onError,
onProgress,
onSuccess,
...props,
...(props as RcUploadProps),
prefixCls,
beforeUpload: mergedBeforeUpload,
onChange: undefined,
Expand Down
40 changes: 37 additions & 3 deletions components/upload/__tests__/type.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ describe('Upload.typescript', () => {
status: 'error' as const,
},
];
const upload = (
<Upload fileList={fileList} defaultFileList={fileList} />
)
const upload = <Upload fileList={fileList} defaultFileList={fileList} />;
expect(upload).toBeTruthy();
});

it('itemRender', () => {
const upload = (
<Upload
Expand All @@ -123,4 +122,39 @@ describe('Upload.typescript', () => {
);
expect(upload).toBeTruthy();
});

it('data', () => {
const upload1 = (
<Upload
data={() => ({
url: '',
})}
>
<span>click to upload</span>
</Upload>
);
const upload2 = (
<Upload
data={() =>
Promise.resolve({
url: '',
})
}
>
<span>click to upload</span>
</Upload>
);
const upload3 = (
<Upload
data={{
url: '',
}}
>
<span>click to upload</span>
</Upload>
);
expect(upload1).toBeTruthy();
expect(upload2).toBeTruthy();
expect(upload3).toBeTruthy();
});
});
4 changes: 3 additions & 1 deletion components/upload/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export interface UploadProps<T = any> {
fileList?: Array<UploadFile<T>>;
action?: string | ((file: RcFile) => string) | ((file: RcFile) => PromiseLike<string>);
directory?: boolean;
data?: object | ((file: UploadFile<T>) => object);
data?:
| Record<string, unknown>
| ((file: UploadFile<T>) => Record<string, unknown> | Promise<Record<string, unknown>>);
method?: 'POST' | 'PUT' | 'PATCH' | 'post' | 'put' | 'patch';
headers?: HttpRequestHeader;
showUploadList?: boolean | ShowUploadListInterface;
Expand Down

0 comments on commit 2df7cc6

Please sign in to comment.