Skip to content

Commit

Permalink
Fixed issue with fields inside an array can't be set to empty when a…
Browse files Browse the repository at this point in the history
… default is set
  • Loading branch information
abdalla-rko committed Jan 22, 2025
1 parent 2b5c907 commit c88d512
Show file tree
Hide file tree
Showing 3 changed files with 34 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/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)

# 5.24.1

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

const mapped = overrideArray.map((value, idx) => {
if (overrideOppositeArray[idx]) {
if (typeof 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

0 comments on commit c88d512

Please sign in to comment.