diff --git a/CHANGELOG.md b/CHANGELOG.md index fc58fbb151..c438cbe0a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,17 @@ should change the heading of the (upcoming) version to include a major version b --> +# 5.21.3 + +## @rjsf/core + +- Updated `MultiSchemaField` to call the `onChange` handler after setting the new option, fixing [#3997](https://github.com/rjsf-team/react-jsonschema-form/issues/3977) + +## @rjsf/utils + +- Updated `mergeDefaultsWithFormData()` to not merge `undefined` when there is a proper default for it, fixing [#4322](https://github.com/rjsf-team/react-jsonschema-form/issues/4322) +- Updated `getClosestMatchingOption()` to improve the scoring of sub-property objects that are provided over ones that aren't, fixing [#3997](https://github.com/rjsf-team/react-jsonschema-form/issues/3977) + # 5.21.2 ## @rjsf/core diff --git a/packages/core/src/components/fields/MultiSchemaField.tsx b/packages/core/src/components/fields/MultiSchemaField.tsx index c58b080424..7808f3289c 100644 --- a/packages/core/src/components/fields/MultiSchemaField.tsx +++ b/packages/core/src/components/fields/MultiSchemaField.tsx @@ -128,9 +128,10 @@ class AnyOfField { + onChange(newFormData, undefined, this.getFieldId()); + }); }; getFieldId() { diff --git a/packages/utils/src/mergeDefaultsWithFormData.ts b/packages/utils/src/mergeDefaultsWithFormData.ts index e4ace80eff..f6a4f47814 100644 --- a/packages/utils/src/mergeDefaultsWithFormData.ts +++ b/packages/utils/src/mergeDefaultsWithFormData.ts @@ -49,5 +49,5 @@ export default function mergeDefaultsWithFormData( return acc; }, acc); } - return formData; + return formData === undefined ? defaults : formData; } diff --git a/packages/utils/src/schema/getClosestMatchingOption.ts b/packages/utils/src/schema/getClosestMatchingOption.ts index c81ce42c70..400ebab75e 100644 --- a/packages/utils/src/schema/getClosestMatchingOption.ts +++ b/packages/utils/src/schema/getClosestMatchingOption.ts @@ -51,7 +51,7 @@ export function calculateIndexScore, rootSchema: S, schema?: S, - formData: any = {} + formData?: any ): number { let totalScore = 0; if (schema) { @@ -83,7 +83,11 @@ export function calculateIndexScore(validator, rootSchema, value as S, formValue || {}); + if (isObject(formValue)) { + // If the structure is matching then give it a little boost in score + score += 1; + } + return score + calculateIndexScore(validator, rootSchema, value as S, formValue); } if (value.type === guessType(formValue)) { // If the types match, then we bump the score by one diff --git a/packages/utils/test/mergeDefaultsWithFormData.test.ts b/packages/utils/test/mergeDefaultsWithFormData.test.ts index 919b94ef83..3410b0de78 100644 --- a/packages/utils/test/mergeDefaultsWithFormData.test.ts +++ b/packages/utils/test/mergeDefaultsWithFormData.test.ts @@ -17,6 +17,14 @@ describe('mergeDefaultsWithFormData()', () => { expect(mergeDefaultsWithFormData(undefined, [2])).toEqual([2]); }); + it('should return default when formData is undefined', () => { + expect(mergeDefaultsWithFormData({}, undefined)).toEqual({}); + }); + + it('should return undefined when formData is undefined', () => { + expect(mergeDefaultsWithFormData(undefined, undefined)).toBeUndefined(); + }); + it('should merge two one-level deep objects', () => { expect(mergeDefaultsWithFormData({ a: 1 }, { b: 2 })).toEqual({ a: 1, diff --git a/packages/utils/test/schema/getClosestMatchingOptionTest.ts b/packages/utils/test/schema/getClosestMatchingOptionTest.ts index 5809ea0875..da16f32573 100644 --- a/packages/utils/test/schema/getClosestMatchingOptionTest.ts +++ b/packages/utils/test/schema/getClosestMatchingOptionTest.ts @@ -49,7 +49,7 @@ export default function getClosestMatchingOptionTest(testValidator: TestValidato expect(calculateIndexScore(testValidator, oneOfSchema, firstOption, ONE_OF_SCHEMA_DATA)).toEqual(1); }); it('returns 8 for second option in oneOf schema', () => { - expect(calculateIndexScore(testValidator, oneOfSchema, secondOption, ONE_OF_SCHEMA_DATA)).toEqual(8); + expect(calculateIndexScore(testValidator, oneOfSchema, secondOption, ONE_OF_SCHEMA_DATA)).toEqual(9); }); it('returns 1 for a schema that has a type matching the formData type', () => { expect(calculateIndexScore(testValidator, oneOfSchema, { type: 'boolean' }, true)).toEqual(1);