Skip to content
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

Bug : Fields inside an array can't be set to empty when default is set. #4461

Merged
merged 5 commits into from
Jan 24, 2025
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/utils

- switch `lodash.isEqualWith` to `fast-equals.createCustomEqual` providing `areFunctionsEqual` assuming any functions are equal.
- Fixed issue with fields inside an array can't be set to empty when a default is set, fixing [#4456](https://github.com/rjsf-team/react-jsonschema-form/issues/4456)
- Fixed issue with file accept attribute, fixing [#4404](https://github.com/rjsf-team/react-jsonschema-form/issues/4404).

## @rjsf/mui
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/mergeDefaultsWithFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export default function mergeDefaultsWithFormData<T = any>(
const overrideOppositeArray = overrideFormDataWithDefaults ? formData : defaultsArray;

const mapped = overrideArray.map((value, idx) => {
if (overrideOppositeArray[idx]) {
// We want to explicitly make sure that the value is NOT undefined since null, 0 and empty space are valid values
if (overrideOppositeArray[idx] !== undefined) {
return mergeDefaultsWithFormData<any>(
defaultsArray[idx],
formData[idx],
Expand Down
32 changes: 32 additions & 0 deletions packages/utils/test/mergeDefaultsWithFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ describe('mergeDefaultsWithFormData()', () => {
expect(mergeDefaultsWithFormData({}, undefined, undefined, undefined, true)).toEqual(undefined);
});

it('should deeply merge and return formData when formData is undefined and defaultSupercedesUndefined false', () => {
expect(
mergeDefaultsWithFormData(
{
arrayWithDefaults: ['Hello World'],
objectWidthDefaults: {
nestedField: 'Hello World!',
},
stringField: 'Hello World!!',
},
{
arrayWithDefaults: [null],
objectWidthDefaults: {
nestedField: undefined,
},
stringField: undefined,
nonEmptyField: 'Hello World!!!',
},
undefined,
undefined,
true
)
).toEqual({
arrayWithDefaults: [null],
objectWidthDefaults: {
nestedField: undefined,
},
stringField: undefined,
nonEmptyField: 'Hello World!!!',
});
});

it('should return default when formData is undefined and defaultSupercedesUndefined true', () => {
expect(mergeDefaultsWithFormData({}, undefined, undefined, true, true)).toEqual({});
});
Expand Down
Loading