Browse Source

Don't change fileList when beforeUpload returns false (#8299)

Close #8036
pull/8374/head
Wei Zhu 7 years ago
committed by GitHub
parent
commit
179528af50
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      components/upload/Upload.tsx
  2. 87
      components/upload/__tests__/uploadlist.test.js

6
components/upload/Upload.tsx

@ -194,8 +194,8 @@ export default class Upload extends React.Component<UploadProps, any> {
this.handleRemove(file); this.handleRemove(file);
} }
onChange = (info) => { onChange = (info, updateState = true) => {
if (!('fileList' in this.props)) { if (!('fileList' in this.props) && updateState) {
this.setState({ fileList: info.fileList }); this.setState({ fileList: info.fileList });
} }
@ -228,7 +228,7 @@ export default class Upload extends React.Component<UploadProps, any> {
this.onChange({ this.onChange({
file, file,
fileList, fileList,
}); }, 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;

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

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { mount } from 'enzyme'; import { mount } from 'enzyme';
import Upload from '..'; import Upload from '..';
import Form from '../../form';
import { errorRequest, successRequest } from './requests'; import { errorRequest, successRequest } from './requests';
const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout));
@ -115,4 +116,90 @@ describe('Upload List', () => {
}, },
}); });
}); });
it('does not change filelist when beforeUpload returns false', () => {
const handleChange = jest.fn();
const wrapper = mount(
<Upload
listType="picture"
defaultFileList={fileList}
onChange={handleChange}
beforeUpload={() => false}
>
<button>upload</button>
</Upload>
);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
expect(wrapper.state().fileList).toBe(fileList);
expect(handleChange.mock.calls[0][0].fileList).toHaveLength(1);
});
// https://github.com/ant-design/ant-design/issues/7762
it('work with form validation', () => {
let errors;
class TestForm extends React.Component {
handleSubmit = () => {
const { validateFields } = this.props.form;
validateFields((err) => {
errors = err;
});
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form onSubmit={this.handleSubmit}>
<Form.Item>
{getFieldDecorator('file', {
valuePropname: 'fileList',
getValueFromEvent: e => e.fileList,
rules: [
{
required: true,
validator: (rule, value, callback) => {
if (!value || value.length === 0) {
callback('file required');
} else {
callback();
}
},
},
],
})(
<Upload
beforeUpload={() => false}
>
<button>upload</button>
</Upload>
)}
</Form.Item>
</Form>
);
}
}
const App = Form.create()(TestForm);
const wrapper = mount(<App />);
wrapper.find(Form).simulate('submit');
expect(errors.file.errors).toEqual([{ message: 'file required', field: 'file' }]);
wrapper.find('input').simulate('change', {
target: {
files: [
{ filename: 'foo.png' },
],
},
});
wrapper.find(Form).simulate('submit');
expect(errors).toBeNull();
});
}); });

Loading…
Cancel
Save