diff --git a/CHANGELOG.md b/CHANGELOG.md index 684372b1dc..ef078e7603 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ should change the heading of the (upcoming) version to include a major version b - Updated `Experimental_DefaultFormStateBehavior` to add a new `nestedDefaultsPrecedence` option - Updated `getDefaultFormState()` to use the new `nestedDefaultsPrecedence` option to control how defaults defined on multiple levels are merged together, fixing [#5089](https://github.com/rjsf-team/react-jsonschema-form/issues/5089) - Updated `omitExtraData()` to better handle `allOf`s containing multiple `if/then/else` blocks, matching fix in `SJSF`, fixing [#5142](https://github.com/rjsf-team/react-jsonschema-form/issues/5142) +- Fixed `optionsList()` to preserve an explicitly empty string `title` on `oneOf`/`anyOf` options instead of falling back to the option's value, fixing [#4448](https://github.com/rjsf-team/react-jsonschema-form/issues/4448) ## @rjsf/validator-ajv8 diff --git a/packages/utils/src/optionsList.ts b/packages/utils/src/optionsList.ts index e5058f1a5b..a302c93827 100644 --- a/packages/utils/src/optionsList.ts +++ b/packages/utils/src/optionsList.ts @@ -87,10 +87,11 @@ export default function optionsList { })), ); }); + it('should keep an empty string title for a oneOf option instead of falling back to the value', () => { + const oneOfSchema: RJSFSchema = { + type: 'string', + oneOf: [ + { + const: 'empty', + title: '', + }, + { + const: 'active', + }, + ], + }; + expect(optionsList(oneOfSchema)).toEqual([ + { schema: oneOfSchema.oneOf![0], label: '', value: 'empty' }, + { schema: oneOfSchema.oneOf![1], label: 'active', value: 'active' }, + ]); + }); + it('should keep an empty string title for a discriminator option instead of falling back to the value', () => { + const anyOfSchema: RJSFSchema = { + discriminator: { + propertyName: 'animal', + }, + anyOf: [ + { + type: 'object', + title: '', + properties: { + animal: { + type: 'string', + const: 'dog', + }, + }, + }, + ], + }; + expect(optionsList(anyOfSchema)).toEqual([{ schema: anyOfSchema.anyOf![0], label: '', value: 'dog' }]); + }); it('should generate options for an anyOf object schema with a discriminator, titles in object', () => { const anyOfSchema: RJSFSchema = { title: 'string',