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

Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/optionsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ export default function optionsList<T = any, S extends StrictRJSFSchema = RJSFSc
if (selectorField) {
const innerSchema: S = get(aSchema, [PROPERTIES_KEY, selectorField], {}) as S;
value = get(innerSchema, DEFAULT_KEY, get(innerSchema, CONST_KEY));
label = label || innerSchema?.title || aSchema.title || String(value);
// Use nullish coalescing so that an explicitly empty string title is preserved
label = label ?? innerSchema?.title ?? aSchema.title ?? String(value);
} else {
value = toConstant(aSchema);
label = label || aSchema.title || String(value);
label = label ?? aSchema.title ?? String(value);
}
return {
schema: aSchema,
Expand Down
38 changes: 38 additions & 0 deletions packages/utils/test/optionsList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,44 @@ describe('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',
Expand Down
Loading