File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ class AjaxUploader extends Component<UploadProps> {
123
123
* Process file before upload. When all the file is ready, we start upload.
124
124
*/
125
125
processFile = async ( file : RcFile , fileList : RcFile [ ] ) : Promise < ParsedFileInfo > => {
126
- const { beforeUpload, action , data } = this . props ;
126
+ const { beforeUpload } = this . props ;
127
127
128
128
let transformedFile : BeforeUploadFileType | void = file ;
129
129
if ( beforeUpload ) {
@@ -143,13 +143,17 @@ class AjaxUploader extends Component<UploadProps> {
143
143
}
144
144
}
145
145
146
+ // Get latest action
147
+ const { action } = this . props ;
146
148
let mergedAction : string ;
147
149
if ( typeof action === 'function' ) {
148
150
mergedAction = await action ( file ) ;
149
151
} else {
150
152
mergedAction = action ;
151
153
}
152
154
155
+ // Get latest data
156
+ const { data } = this . props ;
153
157
let mergedData : object ;
154
158
if ( typeof data === 'function' ) {
155
159
mergedData = await data ( file ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import { mount } from 'enzyme';
5
5
import sinon from 'sinon' ;
6
6
import Uploader from '../index' ;
7
7
8
+ const sleep = ( timeout = 500 ) => new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
9
+
8
10
function Item ( name ) {
9
11
this . name = name ;
10
12
this . toString = ( ) => this . name ;
@@ -466,8 +468,6 @@ describe('uploader', () => {
466
468
} ) ,
467
469
) ;
468
470
469
- const sleep = ( timeout = 500 ) => new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
470
-
471
471
async function testWrapper ( props ) {
472
472
const onBatchStart = jest . fn ( ) ;
473
473
const wrapper = mount ( < Uploader onBatchStart = { onBatchStart } { ...props } /> ) ;
@@ -555,4 +555,36 @@ describe('uploader', () => {
555
555
expect ( onBatchStart ) . toHaveBeenCalledWith ( batchEventFiles ) ;
556
556
} ) ;
557
557
} ) ;
558
+
559
+ it ( 'dynamic change action in beforeUpload should work' , async ( ) => {
560
+ const Test = ( ) => {
561
+ const [ action , setAction ] = React . useState ( 'light' ) ;
562
+
563
+ async function beforeUpload ( ) {
564
+ setAction ( 'bamboo' ) ;
565
+ await sleep ( 100 ) ;
566
+ return true ;
567
+ }
568
+
569
+ return < Uploader beforeUpload = { beforeUpload } action = { action } /> ;
570
+ } ;
571
+
572
+ const wrapper = mount ( < Test /> ) ;
573
+ wrapper . find ( 'input' ) . simulate ( 'change' , {
574
+ target : {
575
+ files : [
576
+ {
577
+ name : 'little.png' ,
578
+ toString ( ) {
579
+ return this . name ;
580
+ } ,
581
+ } ,
582
+ ] ,
583
+ } ,
584
+ } ) ;
585
+
586
+ await sleep ( 200 ) ;
587
+
588
+ expect ( requests [ 0 ] . url ) . toEqual ( 'bamboo' ) ;
589
+ } ) ;
558
590
} ) ;
You can’t perform that action at this time.
0 commit comments