Skip to content

Commit 0d05de1

Browse files
committed
chore: onBatchStart provides all file info
1 parent bc63d25 commit 0d05de1

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/AjaxUploader.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class AjaxUploader extends Component<UploadProps> {
110110
const { onBatchStart } = this.props;
111111
const enabledFiles = fileList.filter(file => file);
112112

113-
onBatchStart?.(enabledFiles.map(file => file.origin));
113+
onBatchStart?.(fileList.map(({ origin, parsedFile }) => ({ file: origin, parsedFile })));
114114

115115
enabledFiles.forEach(file => {
116116
this.post(file);
@@ -126,9 +126,19 @@ class AjaxUploader extends Component<UploadProps> {
126126

127127
let transformedFile: BeforeUploadFileType | void = file;
128128
if (beforeUpload) {
129-
transformedFile = await beforeUpload(file, fileList);
129+
try {
130+
transformedFile = await beforeUpload(file, fileList);
131+
} catch (e) {
132+
// Rejection will also trade as false
133+
transformedFile = false;
134+
}
130135
if (transformedFile === false) {
131-
return null;
136+
return {
137+
origin: file,
138+
parsedFile: null,
139+
action: null,
140+
data: null,
141+
};
132142
}
133143
}
134144

src/interface.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export interface UploadProps
1818
headers?: UploadRequestHeader;
1919
accept?: string;
2020
multiple?: boolean;
21-
onBatchStart?: (fileList: RcFile[]) => void;
21+
onBatchStart?: (
22+
fileList: { file: RcFile; parsedFile: Exclude<BeforeUploadFileType, boolean> }[],
23+
) => void;
2224
onStart?: (file: RcFile) => void;
2325
onError?: (error: Error, ret: object, file: RcFile) => void;
2426
onSuccess?: (response: object, file: RcFile, xhr: object) => void;

tests/uploader.spec.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ describe('uploader', () => {
459459
},
460460
},
461461
];
462+
463+
const batchEventFiles = files.map(file =>
464+
expect.objectContaining({
465+
file,
466+
}),
467+
);
468+
462469
const sleep = (timeout = 500) => new Promise(resolve => setTimeout(resolve, timeout));
463470

464471
async function testWrapper(props) {
@@ -482,7 +489,7 @@ describe('uploader', () => {
482489

483490
it('trigger without pending', async () => {
484491
const onBatchStart = await testWrapper();
485-
expect(onBatchStart).toHaveBeenCalledWith(files);
492+
expect(onBatchStart).toHaveBeenCalledWith(batchEventFiles);
486493
});
487494

488495
it('trigger with beforeUpload delay', async () => {
@@ -497,7 +504,7 @@ describe('uploader', () => {
497504
const onBatchStart = await testWrapper({ beforeUpload });
498505

499506
expect(beforeUpload).toHaveBeenCalledTimes(2);
500-
expect(onBatchStart).toHaveBeenCalledWith(files);
507+
expect(onBatchStart).toHaveBeenCalledWith(batchEventFiles);
501508
});
502509

503510
it('beforeUpload but one is deny', async () => {
@@ -512,7 +519,14 @@ describe('uploader', () => {
512519
const onBatchStart = await testWrapper({ beforeUpload });
513520

514521
expect(beforeUpload).toHaveBeenCalledTimes(2);
515-
expect(onBatchStart).toHaveBeenCalledWith(files.filter(f => f.name !== 'light.png'));
522+
expect(onBatchStart).toHaveBeenCalledWith(
523+
files.map(file =>
524+
expect.objectContaining({
525+
file,
526+
parsedFile: file.name === 'light.png' ? null : file,
527+
}),
528+
),
529+
);
516530
});
517531

518532
it('action delay', async () => {
@@ -524,7 +538,7 @@ describe('uploader', () => {
524538
const onBatchStart = await testWrapper({ action });
525539

526540
expect(action).toHaveBeenCalledTimes(2);
527-
expect(onBatchStart).toHaveBeenCalledWith(files);
541+
expect(onBatchStart).toHaveBeenCalledWith(batchEventFiles);
528542
});
529543

530544
it('data delay', async () => {
@@ -536,7 +550,7 @@ describe('uploader', () => {
536550
const onBatchStart = await testWrapper({ data });
537551

538552
expect(data).toHaveBeenCalledTimes(2);
539-
expect(onBatchStart).toHaveBeenCalledWith(files);
553+
expect(onBatchStart).toHaveBeenCalledWith(batchEventFiles);
540554
});
541555
});
542556
});

0 commit comments

Comments
 (0)