Skip to content

Commit 95e46d5

Browse files
authored
fix: beforeUpload调用后获取action & data值(#29563) (#300)
* fix: beforeUpload调用后获取action & data值 * test: add test case
1 parent cac326e commit 95e46d5

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/AjaxUploader.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class AjaxUploader extends Component<UploadProps> {
123123
* Process file before upload. When all the file is ready, we start upload.
124124
*/
125125
processFile = async (file: RcFile, fileList: RcFile[]): Promise<ParsedFileInfo> => {
126-
const { beforeUpload, action, data } = this.props;
126+
const { beforeUpload } = this.props;
127127

128128
let transformedFile: BeforeUploadFileType | void = file;
129129
if (beforeUpload) {
@@ -143,6 +143,9 @@ class AjaxUploader extends Component<UploadProps> {
143143
}
144144
}
145145

146+
// Call beforeUpload could change action & data.
147+
const { action, data } = this.props;
148+
146149
let mergedAction: string;
147150
if (typeof action === 'function') {
148151
mergedAction = await action(file);

tests/uploader.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,53 @@ describe('uploader', () => {
393393
});
394394
});
395395

396+
describe('change action & data in beforeUpload', () => {
397+
const files = [
398+
{
399+
name: 'success.png',
400+
toString() {
401+
return this.name;
402+
},
403+
},
404+
];
405+
406+
files.item = i => files[i];
407+
408+
it('get changed value in customRequest', done => {
409+
const changedAction = '/test2';
410+
const changedData = { b: 2 };
411+
412+
const Demo = () => {
413+
const [action, setAction] = React.useState('/test1');
414+
const [data, setData] = React.useState({ a: 1 });
415+
416+
return (
417+
<Uploader
418+
action={action}
419+
data={data}
420+
beforeUpload={() => {
421+
setAction(changedAction);
422+
setData(changedData);
423+
424+
return true;
425+
}}
426+
customRequest={options => {
427+
expect(options.action).toEqual(changedAction);
428+
expect(options.data).toEqual(changedData);
429+
430+
done();
431+
}}
432+
/>
433+
);
434+
};
435+
436+
const wrapper = mount(<Demo />);
437+
const input = wrapper.find('input').first();
438+
439+
input.simulate('change', { target: { files } });
440+
});
441+
});
442+
396443
describe('transform file before request', () => {
397444
let uploader;
398445
beforeEach(() => {

0 commit comments

Comments
 (0)