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

# 6.0.0-beta.8

## @rjsf/util

- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)

## @rjsf/chakra-ui

- Added `getChakra` to package exports
- Restored the `ui:options` customization

## @rjsf/util

- Fixed form data propagation with `patternProperties` [#4617](https://github.com/rjsf-team/react-jsonschema-form/pull/4617)
- Fixed issue where oneOf schema references could not be modified when defaults were set, fixing [#4580](https://github.com/rjsf-team/react-jsonschema-form/issues/4580).

## Dev / docs / playground

- Updated precompiled schemas documentation in `validation.md` based on v6 changes, addressingg [#4618](https://github.com/rjsf-team/react-jsonschema-form/issues/4618)
Expand Down
54 changes: 54 additions & 0 deletions packages/core/test/Form.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,60 @@ describeRepeated('Form common', (createFormComponent) => {

expect(node.querySelector(secondInputID).value).to.equal('changed!');
});
it('Should modify oneOf object with references when the defaults are set.', () => {
const schema = {
type: 'object',
$defs: {
protocol: {
type: 'string',
enum: ['fast', 'balanced', 'stringent'],
default: 'fast',
},
},
oneOf: [
{
properties: {
protocol: {
$ref: '#/$defs/protocol',
},
},
},
{
properties: {
something: {
type: 'number',
},
},
},
],
};

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

const protocolInputID = '#root_protocol';
expect(node.querySelector(protocolInputID).value).to.equal('0');

act(() => {
fireEvent.change(node.querySelector(protocolInputID), {
target: { value: '1' },
});
});

sinon.assert.calledWithMatch(
onChange.lastCall,
{
formData: {
protocol: 'balanced',
},
schema,
},
'root_protocol',
);

expect(node.querySelector(protocolInputID).value).to.equal('1');
});
});

describe('Blur handler', () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/utils/src/schema/getDefaultFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,14 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
required,
shouldMergeDefaultsIntoFormData = false,
} = computeDefaultsProps;
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
let formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
// Compute the defaults recursively: give highest priority to deepest nodes.
let defaults: T | T[] | undefined = parentDefaults;
// If we get a new schema, then we need to recompute defaults again for the new schema found.
let schemaToCompute: S | null = null;
let experimental_dfsb_to_compute = experimental_defaultFormStateBehavior;
let updatedRecurseList = _recurseList;

if (
schema[CONST_KEY] &&
experimental_defaultFormStateBehavior?.constAsDefaults !== 'never' &&
Expand Down Expand Up @@ -240,6 +239,13 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
if (schemaToCompute && !defaults) {
defaults = schema.default as T | undefined;
}

// If shouldMergeDefaultsIntoFormData is true
// And the schemaToCompute is set and the rawFormData is not an object
// Then set the formData to the rawFormData
if (shouldMergeDefaultsIntoFormData && schemaToCompute && !isObject(rawFormData)) {
formData = rawFormData as T;
}
} else if (DEPENDENCIES_KEY in schema) {
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
const defaultFormData: T = {
Expand Down