Browse Source

Fix upload list update logic when beforeUpload return false

close #8020 #8779 #9248 #9191

ref #8036
pull/9335/head
afc163 7 years ago
parent
commit
626ebf2063
  1. 9
      components/upload/Upload.tsx
  2. 10
      components/upload/UploadList.tsx
  3. 6
      components/upload/__tests__/uploadlist.test.js
  4. 8
      components/upload/style/index.less
  5. 1
      package.json
  6. 2
      typings/custom-typings.d.ts

9
components/upload/Upload.tsx

@ -1,6 +1,7 @@
import * as React from 'react'; import * as React from 'react';
import RcUpload from 'rc-upload'; import RcUpload from 'rc-upload';
import classNames from 'classnames'; import classNames from 'classnames';
import uniqBy from 'lodash.uniqby';
import LocaleReceiver from '../locale-provider/LocaleReceiver'; import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default'; import defaultLocale from '../locale-provider/default';
import Dragger from './Dragger'; import Dragger from './Dragger';
@ -153,8 +154,8 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
this.handleRemove(file); this.handleRemove(file);
} }
onChange = (info: UploadChangeParam, updateState = true) => { onChange = (info: UploadChangeParam) => {
if (!('fileList' in this.props) && updateState) { if (!('fileList' in this.props)) {
this.setState({ fileList: info.fileList }); this.setState({ fileList: info.fileList });
} }
@ -186,8 +187,8 @@ export default class Upload extends React.Component<UploadProps, UploadState> {
if (result === false) { if (result === false) {
this.onChange({ this.onChange({
file, file,
fileList, fileList: uniqBy(fileList.concat(this.state.fileList), (item: any) => item.uid),
}, false); });
return false; return false;
} else if (result && (result as PromiseLike<any>).then) { } else if (result && (result as PromiseLike<any>).then) {
return result; return result;

10
components/upload/UploadList.tsx

@ -72,12 +72,10 @@ export default class UploadList extends React.Component<UploadListProps, any> {
let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />; let icon = <Icon type={file.status === 'uploading' ? 'loading' : 'paper-clip'} />;
if (listType === 'picture' || listType === 'picture-card') { if (listType === 'picture' || listType === 'picture-card') {
if (file.status === 'uploading' || (!file.thumbUrl && !file.url)) { if (listType === 'picture-card' && file.status === 'uploading') {
if (listType === 'picture-card') { icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>;
icon = <div className={`${prefixCls}-list-item-uploading-text`}>{locale.uploading}</div>; } else if (!file.thumbUrl && !file.url) {
} else { icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
icon = <Icon className={`${prefixCls}-list-item-thumbnail`} type="picture" />;
}
} else { } else {
icon = ( icon = (
<a <a

6
components/upload/__tests__/uploadlist.test.js

@ -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 handleChange = jest.fn();
const wrapper = mount( const wrapper = mount(
<Upload <Upload
@ -139,8 +139,8 @@ describe('Upload List', () => {
}, },
}); });
expect(wrapper.state().fileList).toBe(fileList); expect(wrapper.state().fileList.length).toBe(fileList.length + 1);
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(1); expect(handleChange.mock.calls[0][0].fileList).toHaveLength(3);
}); });
// https://github.com/ant-design/ant-design/issues/7762 // https://github.com/ant-design/ant-design/issues/7762

8
components/upload/style/index.less

@ -353,9 +353,17 @@
} }
.@{upload-item}-name { .@{upload-item}-name {
margin: 8px 0 0;
padding: 0;
text-align: center;
line-height: @line-height-base;
display: none; display: none;
} }
.anticon-picture + .@{upload-item}-name {
display: block;
}
.@{upload-item}-uploading { .@{upload-item}-uploading {
&.@{upload-item} { &.@{upload-item} {
background-color: @background-color-light; background-color: @background-color-light;

1
package.json

@ -47,6 +47,7 @@
"dom-closest": "^0.2.0", "dom-closest": "^0.2.0",
"enquire.js": "^2.1.1", "enquire.js": "^2.1.1",
"lodash.debounce": "^4.0.8", "lodash.debounce": "^4.0.8",
"lodash.uniqby": "^4.7.0",
"moment": "^2.19.3", "moment": "^2.19.3",
"omit.js": "^1.0.0", "omit.js": "^1.0.0",
"prop-types": "^15.5.7", "prop-types": "^15.5.7",

2
typings/custom-typings.d.ts

@ -95,3 +95,5 @@ declare module "*.json" {
declare module "prop-types" declare module "prop-types"
declare module "lodash.debounce" declare module "lodash.debounce"
declare module "lodash.uniqby"

Loading…
Cancel
Save