Skip to content

Commit

Permalink
fix: multiple file upload issue (#4347)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
BorjaDV and heath-freenome authored Oct 28, 2024
1 parent 13550f0 commit 6a91898
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/core

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

## @rjsf/validator-ajv8
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/widgets/FileWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function FileWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
processFiles(event.target.files).then((filesInfoEvent) => {
const newValue = filesInfoEvent.map((fileInfo) => fileInfo.dataURL);
if (multiple) {
onChange(value.concat(newValue[0]));
onChange(value.concat(newValue));
} else {
onChange(newValue[0]);
}
Expand Down
35 changes: 35 additions & 0 deletions packages/core/test/ArrayField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,41 @@ describe('ArrayField', () => {
);
});

it('should handle a change event with multiple files that results the same items in the list', async () => {
sandbox.stub(window, 'FileReader').returns({
set onload(fn) {
fn({ target: { result: 'data:text/plain;base64,x=' } });
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
readAsDataUrl() {},
});

const { node, onChange } = createFormComponent({ schema });

act(() => {
fireEvent.change(node.querySelector('.field input[type=file]'), {
target: {
files: [
{ name: 'file1.txt', size: 1, type: 'type' },
{ name: 'file2.txt', size: 2, type: 'type' },
],
},
});
});

await act(() => {
new Promise(setImmediate);
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: ['data:text/plain;name=file1.txt;base64,x=', 'data:text/plain;name=file2.txt;base64,x='],
},
'root'
);
});

it('should fill field with data', () => {
const { node } = createFormComponent({
schema,
Expand Down

0 comments on commit 6a91898

Please sign in to comment.