Skip to content

Commit 6a91898

Browse files
fix: multiple file upload issue (#4347)
* fix: multiple file upload issue Resolved a bug where only the first file was uploaded when multiple files were selected. Now, all selected files are uploaded as expected. * feature: Added test to check the expected behaviour * Fix: Move entry of changelog to version 5.22.2 --------- Co-authored-by: Heath C <[email protected]>
1 parent 13550f0 commit 6a91898

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b
2020

2121
## @rjsf/core
2222

23+
- Fix an issue where only the first file was uploaded when users selected multiple files for upload.
2324
- Fixed validation regression Form not revalidating after formData change, fixing [#4343](https://github.com/rjsf-team/react-jsonschema-form/issues/4343)
2425

2526
## @rjsf/validator-ajv8

packages/core/src/components/widgets/FileWidget.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ function FileWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
169169
processFiles(event.target.files).then((filesInfoEvent) => {
170170
const newValue = filesInfoEvent.map((fileInfo) => fileInfo.dataURL);
171171
if (multiple) {
172-
onChange(value.concat(newValue[0]));
172+
onChange(value.concat(newValue));
173173
} else {
174174
onChange(newValue[0]);
175175
}

packages/core/test/ArrayField.test.jsx

+35
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,41 @@ describe('ArrayField', () => {
17021702
);
17031703
});
17041704

1705+
it('should handle a change event with multiple files that results the same items in the list', async () => {
1706+
sandbox.stub(window, 'FileReader').returns({
1707+
set onload(fn) {
1708+
fn({ target: { result: 'data:text/plain;base64,x=' } });
1709+
},
1710+
// eslint-disable-next-line @typescript-eslint/no-empty-function
1711+
readAsDataUrl() {},
1712+
});
1713+
1714+
const { node, onChange } = createFormComponent({ schema });
1715+
1716+
act(() => {
1717+
fireEvent.change(node.querySelector('.field input[type=file]'), {
1718+
target: {
1719+
files: [
1720+
{ name: 'file1.txt', size: 1, type: 'type' },
1721+
{ name: 'file2.txt', size: 2, type: 'type' },
1722+
],
1723+
},
1724+
});
1725+
});
1726+
1727+
await act(() => {
1728+
new Promise(setImmediate);
1729+
});
1730+
1731+
sinon.assert.calledWithMatch(
1732+
onChange.lastCall,
1733+
{
1734+
formData: ['data:text/plain;name=file1.txt;base64,x=', 'data:text/plain;name=file2.txt;base64,x='],
1735+
},
1736+
'root'
1737+
);
1738+
});
1739+
17051740
it('should fill field with data', () => {
17061741
const { node } = createFormComponent({
17071742
schema,

0 commit comments

Comments
 (0)