Skip to content

fix: multiple file upload issue #4347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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