Skip to content

Commit d2a9e5b

Browse files
authored
feat: data function support return promise(#22529) (#253)
1 parent 261bb6a commit d2a9e5b

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ React.render(<Upload />, container);
6161
|action| string &#124; function(file): string &#124; Promise&lt;string&gt; | | form action url |
6262
|method | string | post | request method |
6363
|directory| boolean | false | support upload whole directory |
64-
|data| object/function(file) | | other data object to post or a function which returns a data object |
64+
|data| object/function(file) | | other data object to post or a function which returns a data object(a promise object which resolve a data object) |
6565
|headers| object | {} | http headers to post, available in modern browsers |
6666
|accept | string | | input accept attribute |
6767
|multiple | boolean | false | only support ie10+|

src/AjaxUploader.jsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,29 @@ class AjaxUploader extends Component {
125125
return;
126126
}
127127
const { props } = this;
128-
let { data } = props;
129128
const {
130129
onStart,
131130
onProgress,
132131
transformFile = (originFile) => originFile,
133132
} = props;
134133

135134
new Promise(resolve => {
136-
const { action } = props;
135+
let { data, action } = props;
137136
if (typeof action === 'function') {
138-
return resolve(action(file));
137+
action = action(file);
139138
}
140-
resolve(action);
141-
}).then(action => {
139+
if (typeof data === 'function') {
140+
data = data(file);
141+
}
142+
resolve(Promise.all([action, data]));
143+
}).then(([action, data]) => {
142144
const { uid } = file;
143145
const request = props.customRequest || defaultRequest;
144146
const transform = Promise.resolve(transformFile(file)).catch(e => {
145147
console.error(e); // eslint-disable-line no-console
146148
});
147149

148150
transform.then((transformedFile) => {
149-
if (typeof data === 'function') {
150-
data = data(file);
151-
}
152-
153151
const requestOption = {
154152
action,
155153
filename: props.name,

tests/uploader.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,22 @@ describe('uploader', () => {
338338
});
339339
});
340340

341-
it('support action is function returns Promise', done => {
341+
it('support action and data is function returns Promise', done => {
342342
const action = () => {
343343
return new Promise(resolve => {
344344
setTimeout(() => {
345345
resolve('/upload.do');
346346
}, 1000);
347347
});
348348
};
349-
ReactDOM.render(<Uploader action={action} />, node, function init() {
349+
const data = () => {
350+
return new Promise(resolve => {
351+
setTimeout(() => {
352+
resolve({ field1: 'a' });
353+
}, 1000);
354+
});
355+
};
356+
ReactDOM.render(<Uploader data={ data } action={action} />, node, function init() {
350357
uploader = this;
351358
const input = TestUtils.findRenderedDOMComponentWithTag(uploader, 'input');
352359
const files = [
@@ -365,6 +372,7 @@ describe('uploader', () => {
365372
console.log(requests);
366373
expect(requests.length).to.be(1);
367374
expect(requests[0].url).to.be('/upload.do');
375+
expect(requests[0].requestBody.get('field1')).to.be('a');
368376
done();
369377
}, 1000);
370378
}, 100);

0 commit comments

Comments
 (0)