Skip to content

Commit

Permalink
Fix upload list update logic when beforeUpload return false
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Feb 11, 2018
1 parent e3f115d commit 626ebf2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
9 changes: 5 additions & 4 deletions components/upload/Upload.tsx
@@ -1,6 +1,7 @@
import * as React from 'react';
import RcUpload from 'rc-upload';
import classNames from 'classnames';
import uniqBy from 'lodash.uniqby';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import Dragger from './Dragger';
Expand Down Expand Up @@ -153,8 +154,8 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
this.handleRemove(file);
}

onChange = (info: UploadChangeParam, updateState = true) => {
if (!('fileList' in this.props) && updateState) {
onChange = (info: UploadChangeParam) => {
if (!('fileList' in this.props)) {
this.setState({ fileList: info.fileList });
}

Expand Down Expand Up @@ -186,8 +187,8 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
if (result === false) {
this.onChange({
file,
fileList,
}, false);
fileList: uniqBy(fileList.concat(this.state.fileList), (item: any) => item.uid),
});
return false;
} else if (result && (result as PromiseLike<any>).then) {
return result;
Expand Down
10 changes: 4 additions & 6 deletions components/upload/UploadList.tsx
Expand Up @@ -72,12 +72,10 @@ export default class UploadList extends React.Component<UploadListProps, any> {
let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />;

if (listType === 'picture' || listType === 'picture-card') {
if (file.status === 'uploading' || (!file.thumbUrl && !file.url)) {
if (listType === 'picture-card') {
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
} else {
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
}
if (listType === 'picture-card' && file.status === 'uploading') {
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
} else if (!file.thumbUrl && !file.url) {
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
} else {
icon = (
<a
Expand Down
6 changes: 3 additions & 3 deletions components/upload/__tests__/uploadlist.test.js
Expand Up @@ -118,7 +118,7 @@ describe('Upload List', () => {
});
});

it('does not change filelist when beforeUpload returns false', () => {
it('does concat filelist when beforeUpload returns false', () => {
const handleChange = jest.fn();
const wrapper = mount(
<Upload
Expand All @@ -139,8 +139,8 @@ describe('Upload List', () => {
},
});

expect(wrapper.state().fileList).toBe(fileList);
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(1);
expect(wrapper.state().fileList.length).toBe(fileList.length + 1);
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(3);
});

// https://github.com/ant-design/ant-design/issues/7762
Expand Down
8 changes: 8 additions & 0 deletions components/upload/style/index.less
Expand Up @@ -353,9 +353,17 @@
}

.@{upload-item}-name {
margin: 8px 0 0;
padding: 0;
text-align: center;
line-height: @line-height-base;
display: none;
}

.anticon-picture + .@{upload-item}-name {
display: block;
}

.@{upload-item}-uploading {
&.@{upload-item} {
background-color: @background-color-light;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -47,6 +47,7 @@
"dom-closest": "^0.2.0",
"enquire.js": "^2.1.1",
"lodash.debounce": "^4.0.8",
"lodash.uniqby": "^4.7.0",

This comment has been minimized.

Copy link
@benjycui

benjycui Feb 22, 2018

Contributor

统一使用 lodash/xxx 吧,以后升级依赖版本也方便。

"moment": "^2.19.3",
"omit.js": "^1.0.0",
"prop-types": "^15.5.7",
Expand Down
2 changes: 2 additions & 0 deletions typings/custom-typings.d.ts
Expand Up @@ -95,3 +95,5 @@ declare module "*.json" {
declare module "prop-types"

declare module "lodash.debounce"

declare module "lodash.uniqby"

4 comments on commit 626ebf2

@afc163
Copy link
Member Author

@afc163 afc163 commented on 626ebf2 Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed #8946 too

@Gavinchen92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

@xyy94813
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afc163 为什么 beforeUpload return false 仍然要触发 onChange

const result = this.props.beforeUpload(file, fileList);
if (result === false) {
this.onChange({
file,
fileList: uniqBy(
this.state.fileList.concat(fileList.map(fileToObject)),
(item: UploadFile) => item.uid,
),
});
return false;
}

onChange = (info: UploadChangeParam) => {
if (!('fileList' in this.props)) {
this.setState({ fileList: info.fileList });
}
const { onChange } = this.props;
if (onChange) {
onChange(info);
}
};

之前有过一个 PR 用于解决这个问题
#8299

@afc163
Copy link
Member Author

@afc163 afc163 commented on 626ebf2 Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.