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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ should change the heading of the (upcoming) version to include a major version b

- Fixed issue in BaseInputTemplate where input props were passed to `slotProps.htmlInput`, which does not work in MUI v5.

## @rjsf/utils

- Fixed issue with schema combinators(allOf, anyOf, oneOf) could not be modified when defaults were set, fixing [#4555](https://github.com/rjsf-team/react-jsonschema-form/issues/4555)

## Dev / docs / playground

- Updated docs for ArrayFieldItemTemplate to include prop `onCopyIndexClick`, fixing [#4507](https://github.com/rjsf-team/react-jsonschema-form/issues/4507)
Expand Down
168 changes: 168 additions & 0 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,174 @@ describeRepeated('Form common', (createFormComponent) => {
sinon.assert.callCount(onChange, 1);
sinon.assert.callCount(secondOnChange, 1);
});
it('should modify an allOf field when the defaults are set', () => {
const schema = {
properties: {
all_of_field: {
allOf: [
{
properties: {
first: {
type: 'string',
},
},
},
{
properties: {
second: {
type: 'string',
},
},
},
],
default: {
second: 'second!',
},
},
},
type: 'object',
};

const { node, onChange } = createFormComponent({
schema,
});

const secondInputID = '#root_all_of_field_second';
expect(node.querySelector(secondInputID).value).to.equal('second!');

act(() => {
fireEvent.change(node.querySelector(secondInputID), {
target: { value: 'changed!' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
all_of_field: {
second: 'changed!',
},
},
schema,
},
'root_all_of_field_second'
);

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
it('should modify an oneOf field when the defaults are set', () => {
const schema = {
properties: {
one_of_field: {
oneOf: [
{
properties: {
first: {
type: 'string',
},
},
},
{
properties: {
second: {
type: 'string',
},
},
},
],
default: {
second: 'second!',
},
},
},
type: 'object',
};

const { node, onChange } = createFormComponent({
schema,
});

const secondInputID = '#root_one_of_field_second';
expect(node.querySelector(secondInputID).value).to.equal('second!');

act(() => {
fireEvent.change(node.querySelector(secondInputID), {
target: { value: 'changed!' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
one_of_field: {
second: 'changed!',
},
},
schema,
},
'root_one_of_field_second'
);

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
it('should modify an anyOf field when the defaults are set', () => {
const schema = {
properties: {
any_of_field: {
anyOf: [
{
properties: {
first: {
type: 'string',
},
},
},
{
properties: {
second: {
type: 'string',
},
},
},
],
default: {
second: 'second!',
},
},
},
type: 'object',
};

const { node, onChange } = createFormComponent({
schema,
});

const secondInputID = '#root_any_of_field_second';
expect(node.querySelector(secondInputID).value).to.equal('second!');

act(() => {
fireEvent.change(node.querySelector(secondInputID), {
target: { value: 'changed!' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
any_of_field: {
second: 'changed!',
},
},
schema,
},
'root_any_of_field_second'
);

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
});

describe('Blur handler', () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
// For object defaults, only override parent defaults that are defined in
// schema.default.
defaults = mergeObjects(defaults!, schema.default as GenericObjectType) as T;
} else if (DEFAULT_KEY in schema) {
} else if (DEFAULT_KEY in schema && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY]) {
// If the schema has a default value, then we should use it as the default.
// And if the schema does not have anyOf or oneOf, this is done because we need to merge the defaults with the formData.
defaults = schema.default as unknown as T;
} else if (REF_KEY in schema) {
const refName = schema[REF_KEY];
Expand Down Expand Up @@ -284,7 +286,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
getClosestMatchingOption<T, S, F>(
validator,
rootSchema,
rawFormData,
rawFormData ?? (schema.default as T),
oneOf as S[],
0,
discriminator,
Expand All @@ -302,7 +304,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
getClosestMatchingOption<T, S, F>(
validator,
rootSchema,
rawFormData,
rawFormData ?? (schema.default as T),
anyOf as S[],
0,
discriminator,
Expand Down Expand Up @@ -347,7 +349,9 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf
);
if (!isObject(rawFormData)) {
if (!isObject(rawFormData) || ALL_OF_KEY in schema) {
// If the formData is not an object which means it's a primitive field, then we need to merge the defaults into the formData.
// Or if the schema has allOf, we need to merge the defaults into the formData because we don't compute the defaults for allOf.
defaultsWithFormData = mergeDefaultsWithFormData<T>(
defaultsWithFormData as T,
matchingFormData as T,
Expand Down
Loading