Browse Source

Fix upload list update logic when beforeUpload return false

Porting from 626ebf2063
pull/9590/head
Wei Zhu 7 years ago
parent
commit
ef6d7a5d90
  1. 9
      components/upload/Upload.tsx
  2. 14
      components/upload/__tests__/upload.test.js
  3. 6
      components/upload/__tests__/uploadlist.test.js
  4. 1
      package.json

9
components/upload/Upload.tsx

@ -2,6 +2,7 @@ import React from 'react';
import RcUpload from 'rc-upload';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import uniqBy from 'lodash.uniqby';
import Dragger from './Dragger';
import UploadList from './UploadList';
import { UploadProps, UploadLocale } from './interface';
@ -194,8 +195,8 @@ export default class Upload extends React.Component<UploadProps, any> {
this.handleRemove(file);
}
onChange = (info, updateState = true) => {
if (!('fileList' in this.props) && updateState) {
onChange = (info) => {
if (!('fileList' in this.props)) {
this.setState({ fileList: info.fileList });
}
@ -227,8 +228,8 @@ export default class Upload extends React.Component<UploadProps, any> {
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;

14
components/upload/__tests__/upload.test.js

@ -3,6 +3,20 @@ import React from 'react';
import { mount } from 'enzyme';
import Upload from '..';
const fileList = [{
uid: -1,
name: 'xxx.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
}, {
uid: -2,
name: 'yyy.png',
status: 'done',
url: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png',
thumbUrl: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
}];
describe('Upload', () => {
// https://github.com/react-component/upload/issues/36
it('should get refs inside Upload in componentDidMount', () => {

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

@ -117,7 +117,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
@ -138,8 +138,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

1
package.json

@ -46,6 +46,7 @@
"css-animation": "^1.2.5",
"dom-closest": "^0.2.0",
"lodash.debounce": "^4.0.8",
"lodash.uniqby": "^4.7.0",
"moment": "^2.19.3",
"omit.js": "^1.0.0",
"prop-types": "^15.5.7",

Loading…
Cancel
Save