Skip to content

Commit

Permalink
Support renderFileInfo on <Uploader> (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Dec 11, 2018
1 parent f9fb55a commit 64f866f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/UploadFileItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Props = {
onReupload?: (file: FileType, event: SyntheticEvent<*>) => void,
className?: string,
maxPreviewFileSize: number,
classPrefix?: string
classPrefix?: string,
renderFileInfo?: (file: FileType, fileElement: React.Node) => React.Node
};

type State = {
Expand Down Expand Up @@ -204,13 +205,16 @@ class UploadFileItem extends React.Component<Props, State> {
}

renderFilePanel() {
const { file } = this.props;
const { file, renderFileInfo } = this.props;
const fileElement = (
<a role="presentation" className={this.addPrefix('title')} onClick={this.handlePreview}>
{file.name}
</a>
);
return (
<div className={this.addPrefix('panel')}>
<div className={this.addPrefix('content')}>
<a role="presentation" className={this.addPrefix('title')} onClick={this.handlePreview}>
{file.name}
</a>
{renderFileInfo ? renderFileInfo(file, fileElement) : fileElement}
{this.renderErrorStatus()}
{this.renderFileSize()}
</div>
Expand Down
12 changes: 10 additions & 2 deletions src/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ type Props = {
onRemove?: (file: FileType) => void,
maxPreviewFileSize?: number,
style?: Object,
toggleComponentClass?: React.ElementType
toggleComponentClass?: React.ElementType,
renderFileInfo?: (file: FileType, fileElement: React.Node) => React.Node
};

type State = {
Expand Down Expand Up @@ -304,7 +305,13 @@ class Uploader extends React.Component<Props, State> {
};

renderFileItems() {
const { disabledFileItem, listType, onPreview, maxPreviewFileSize } = this.props;
const {
disabledFileItem,
listType,
onPreview,
maxPreviewFileSize,
renderFileInfo
} = this.props;
const fileList = this.getFileList();

return (
Expand All @@ -319,6 +326,7 @@ class Uploader extends React.Component<Props, State> {
onPreview={onPreview}
onReupload={this.handleReupload}
onCancel={this.handleRemoveFile}
renderFileInfo={renderFileInfo}
/>
))}
</div>
Expand Down
13 changes: 13 additions & 0 deletions test/UploadFileItemSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ describe('UploadFileItem', () => {
assert.include(findDOMNode(instance).className, 'custom');
});

it('Should output a custom file name', () => {
const instance = ReactTestUtils.renderIntoDocument(
<UploadFileItem
file={file}
className="custom"
renderFileInfo={file => {
return <div className="file-info">{file.name}</div>;
}}
/>
);
assert.include(findDOMNode(instance).querySelector('.file-info').innerText, 'a');
});

it('Should have a custom style', () => {
const fontSize = '12px';
const instance = ReactTestUtils.renderIntoDocument(
Expand Down

0 comments on commit 64f866f

Please sign in to comment.