From 179528af503fe24725d19687666ddbf24962ee1f Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Wed, 29 Nov 2017 20:28:19 +0800 Subject: [PATCH] Don't change fileList when beforeUpload returns false (#8299) Close #8036 --- components/upload/Upload.tsx | 6 +- .../upload/__tests__/uploadlist.test.js | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/components/upload/Upload.tsx b/components/upload/Upload.tsx index a641f45d7d..bbf750c94f 100644 --- a/components/upload/Upload.tsx +++ b/components/upload/Upload.tsx @@ -194,8 +194,8 @@ export default class Upload extends React.Component { this.handleRemove(file); } - onChange = (info) => { - if (!('fileList' in this.props)) { + onChange = (info, updateState = true) => { + if (!('fileList' in this.props) && updateState) { this.setState({ fileList: info.fileList }); } @@ -228,7 +228,7 @@ export default class Upload extends React.Component { this.onChange({ file, fileList, - }); + }, false); return false; } else if (result && (result as PromiseLike).then) { return result; diff --git a/components/upload/__tests__/uploadlist.test.js b/components/upload/__tests__/uploadlist.test.js index 004ac48d4d..c5b2e697bb 100644 --- a/components/upload/__tests__/uploadlist.test.js +++ b/components/upload/__tests__/uploadlist.test.js @@ -1,6 +1,7 @@ import React from 'react'; import { mount } from 'enzyme'; import Upload from '..'; +import Form from '../../form'; import { errorRequest, successRequest } from './requests'; 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( + false} + > + + + ); + + 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 ( +
+ + {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(); + } + }, + }, + ], + })( + false} +