Skip to content

Commit

Permalink
feat: treat default field in additionalProperties (rjsf-team#4199)
Browse files Browse the repository at this point in the history
* feat: treat "default" in additionalProperties

* test: add test cases for the case there's a default in additionalProperties
  • Loading branch information
tomatommy-bs authored May 24, 2024
1 parent eec280a commit ec932db
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/core/src/components/fields/ObjectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,16 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
const newFormData = { ...formData } as T;

let type: RJSFSchema['type'] = undefined;
let defaultValue: RJSFSchema['default'] = undefined;
if (isObject(schema.additionalProperties)) {
type = schema.additionalProperties.type;
defaultValue = schema.additionalProperties.default;
let apSchema = schema.additionalProperties;
if (REF_KEY in apSchema) {
const { schemaUtils } = registry;
apSchema = schemaUtils.retrieveSchema({ $ref: apSchema[REF_KEY] } as S, formData);
type = apSchema.type;
defaultValue = apSchema.default;
}
if (!type && (ANY_OF_KEY in apSchema || ONE_OF_KEY in apSchema)) {
type = 'object';
Expand All @@ -217,7 +220,7 @@ class ObjectField<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo

const newKey = this.getAvailableKey('newKey', newFormData);
// Cast this to make the `set` work properly
set(newFormData as GenericObjectType, newKey, this.getDefaultValue(type));
set(newFormData as GenericObjectType, newKey, defaultValue ?? this.getDefaultValue(type));

onChange(newFormData);
};
Expand Down
44 changes: 44 additions & 0 deletions packages/core/test/ObjectField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,50 @@ describe('ObjectField', () => {
});
});

it("should add a new default item if default is provided in the additionalProperties' schema", () => {
const customSchema = {
...schema,
additionalProperties: {
type: 'string',
default: 'default value',
},
};
const { node, onChange } = createFormComponent({
schema: customSchema,
formData: {},
});

fireEvent.click(node.querySelector('.object-property-expand button'));

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: {
newKey: 'default value',
},
});
});

it('should add a new default item even if the schema of default value is invalid', () => {
const customSchema = {
...schema,
additionalProperties: {
type: 'string',
default: 1,
},
};
const { node, onChange } = createFormComponent({
schema: customSchema,
formData: {},
});

fireEvent.click(node.querySelector('.object-property-expand button'));

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: {
newKey: 1,
},
});
});

it('should not provide an expand button if length equals maxProperties', () => {
const { node } = createFormComponent({
schema: { maxProperties: 1, ...schema },
Expand Down

0 comments on commit ec932db

Please sign in to comment.