Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding properties should work when additionalProperties is true #3181

Merged
merged 5 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -22,6 +22,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/core
- Extended `Form.onChange` to optionally return the `id` of the field that caused the change, fixing (https://github.com/rjsf-team/react-jsonschema-form/issues/2768)
nickgros marked this conversation as resolved.
Show resolved Hide resolved
- Fixed a regression in earlier v5 beta versions where additional properties could not be added when `additionalProperties` was `true` ([#3719](https://github.com/rjsf-team/react-jsonschema-form/pull/3719)).

## @rjsf/utils
- Updated the `onChange` prop on `FieldProps` and `FieldTemplateProps` to add an optional `id` parameter to the callback.
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/components/fields/ObjectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,23 @@ class ObjectField<T = any, F = any> extends Component<
* @param schema - The schema element to which the new property is being added
*/
handleAddClick = (schema: RJSFSchema) => () => {
if (!isObject(schema.additionalProperties)) {
if (!schema.additionalProperties) {
return;
}
const { formData, onChange, registry } = this.props;
let type = schema.additionalProperties.type;
const newFormData = { ...formData };

if (REF_KEY in schema.additionalProperties) {
const { schemaUtils } = registry;
const refSchema = schemaUtils.retrieveSchema(
{ $ref: schema.additionalProperties[REF_KEY] },
formData
);

type = refSchema.type;
let type: RJSFSchema["type"] = undefined;
if (isObject(schema.additionalProperties)) {
type = schema.additionalProperties.type;
if (REF_KEY in schema.additionalProperties) {
const { schemaUtils } = registry;
const refSchema = schemaUtils.retrieveSchema(
{ $ref: schema.additionalProperties[REF_KEY] },
formData
);
type = refSchema.type;
}
}

const newKey = this.getAvailableKey("newKey", newFormData);
Expand Down
45 changes: 45 additions & 0 deletions packages/core/test/ObjectField_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,51 @@ describe("ObjectField", () => {
});
});

it("should add a property matching the additionalProperties schema", () => {
// Specify that additionalProperties must be an array of strings
const additionalPropertiesArraySchema = {
...schema,
additionalProperties: {
type: "array",
items: {
type: "string",
},
},
};
const { node, onChange } = createFormComponent({
schema: additionalPropertiesArraySchema,
formData: {},
});

Simulate.click(node.querySelector(".object-property-expand button"));

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

it("should add a string item if additionalProperties is true", () => {
// Specify that additionalProperties must be an array of strings
nickgros marked this conversation as resolved.
Show resolved Hide resolved
const customSchema = {
...schema,
additionalProperties: true,
};
const { node, onChange } = createFormComponent({
schema: customSchema,
formData: {},
});

Simulate.click(node.querySelector(".object-property-expand button"));

sinon.assert.calledWithMatch(onChange.lastCall, {
formData: {
newKey: "New Value",
},
});
});

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