From decb6d8390aedf2a78f25ee03f7687ed4b58b9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=9B=86=E9=B9=84?= Date: Sun, 11 Mar 2018 22:24:09 +0800 Subject: [PATCH] Compatible with non-image format file preview in UploadList (#9621) * Compatible with non-image format file preview in UploadList * Add unit test about Upload List should non-image format file preview --- components/upload/UploadList.tsx | 14 ++++- .../__snapshots__/uploadlist.test.js.snap | 63 +++++++++++++++++++ .../upload/__tests__/uploadlist.test.js | 22 +++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/components/upload/UploadList.tsx b/components/upload/UploadList.tsx index df69927a0d..15f0913c87 100644 --- a/components/upload/UploadList.tsx +++ b/components/upload/UploadList.tsx @@ -13,6 +13,10 @@ const previewFile = (file: File, callback: Function) => { reader.readAsDataURL(file); }; +const isImageUrl = (url: string): boolean => { + return /^data:image\//.test(url) || /\.(webp|svg|png|gif|jpg|jpeg)$/.test(url); +}; + export default class UploadList extends React.Component { static defaultProps = { listType: 'text', // or picture @@ -77,6 +81,14 @@ export default class UploadList extends React.Component { } else if (!file.thumbUrl && !file.url) { icon = ; } else { + let thumbnail = isImageUrl((file.thumbUrl || file.url) as string) ? ( + {file.name} + ) : ( + + ); icon = ( { target="_blank" rel="noopener noreferrer" > - {file.name} + {thumbnail} ); } diff --git a/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap b/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap index 422ee281ea..350fa6101c 100644 --- a/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap +++ b/components/upload/__tests__/__snapshots__/uploadlist.test.js.snap @@ -221,3 +221,66 @@ exports[`Upload List should be uploading when upload a file 2`] = ` `; + +exports[`Upload List should non-image format file preview 1`] = ` + +
+ + + + +
+
+
+ + +
+
+
+`; diff --git a/components/upload/__tests__/uploadlist.test.js b/components/upload/__tests__/uploadlist.test.js index f555b0463b..cf730f9381 100644 --- a/components/upload/__tests__/uploadlist.test.js +++ b/components/upload/__tests__/uploadlist.test.js @@ -261,4 +261,26 @@ describe('Upload List', () => { await delay(20); expect(wrapper.state().fileList[2].thumbUrl).not.toBeFalsy(); }); + + it('should non-image format file preview', () => { + const list = [ + { + ...fileList[0], + uid: -3, + url: 'https://cdn.xxx.com/aaa.zip', + thumbUrl: 'data:application/zip;base64,UEsDBAoAAAAAADYZYkwAAAAAAAAAAAAAAAAdAAk', + originFileObj: new File([], 'aaa.zip'), + }, + ]; + + const wrapper = mount( + + + + ); + expect(wrapper.render()).toMatchSnapshot(); + }); });