Skip to content

Commit

Permalink
Fix linkProps in upload fileList
Browse files Browse the repository at this point in the history
1. should support json string format
2. should be able to override props like rel, target, title and etc
  • Loading branch information
afc163 committed Sep 14, 2018
1 parent 9d5920b commit efb23c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions components/upload/UploadList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,17 @@ export default class UploadList extends React.Component<UploadListProps, any> {
[`${prefixCls}-list-item`]: true,
[`${prefixCls}-list-item-${file.status}`]: true,
});
const linkProps = typeof file.linkProps === 'string'
? JSON.parse(file.linkProps) : file.linkProps;
const preview = file.url ? (
<a
{...file.linkProps}
href={file.url}
target="_blank"
rel="noopener noreferrer"
className={`${prefixCls}-list-item-name`}
onClick={e => this.handlePreview(file, e)}
title={file.name}
{...linkProps}
href={file.url}
onClick={e => this.handlePreview(file, e)}
>
{file.name}
</a>
Expand Down
39 changes: 39 additions & 0 deletions components/upload/__tests__/upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,43 @@ describe('Upload', () => {
});
});
});

it('should support linkProps as object', () => {
const fileList = [{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: {
download: 'image',
rel: 'noopener',
},
}];
const wrapper = mount(
<Upload fileList={fileList} />
);
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
});

it('should support linkProps as json stringify', () => {
const linkPropsString = JSON.stringify({
download: 'image',
rel: 'noopener',
});
const fileList = [{
uid: '-1',
name: 'foo.png',
status: 'done',
url: 'http://www.baidu.com/xxx.png',
linkProps: linkPropsString,
}];
const wrapper = mount(
<Upload fileList={fileList} />
);
const linkNode = wrapper.find('a.ant-upload-list-item-name');
expect(linkNode.props().download).toBe('image');
expect(linkNode.props().rel).toBe('noopener');
});
});

0 comments on commit efb23c1

Please sign in to comment.