Skip to content

Commit 174e136

Browse files
authoredJan 24, 2019
Fix uiSchema for additionalProperties (#1144)
* fix: uiSchema for additionalProperties #1132 * doc: update readme * doc: fix toc * doc: update docs
1 parent 2368740 commit 174e136

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed
 

‎docs/form-customization.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,23 @@ const uiSchema = {
248248
};
249249
```
250250

251-
### Object item options
251+
### Object additional properties
252252

253-
#### `expandable` option
253+
You can define `additionalProperties` by setting its value to a schema object, such as the following:
254254

255-
If `additionalProperties` contains a schema object, an add button for new properties is shown by default. The UX for editing properties whose names are user-defined is still experimental.
255+
```js
256+
const schema = {
257+
"type": "object",
258+
"properties": {"type": "string"},
259+
"additionalProperties": {"type": "number"}
260+
}
261+
```
262+
263+
In this way, an add button for new properties is shown by default. The UX for editing properties whose names are user-defined is still experimental.
264+
265+
You can also define `uiSchema` options for `additionalProperties` by setting the `additionalProperties` attribute in the `uiSchema`.
266+
267+
#### `expandable` option
256268

257269
You can turn support for `additionalProperties` off with the `expandable` option in `uiSchema`:
258270

‎src/components/fields/ObjectField.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ class ObjectField extends Component {
213213
}
214214

215215
const Template = registry.ObjectFieldTemplate || DefaultObjectFieldTemplate;
216-
217216
const templateProps = {
218217
title: uiSchema["ui:title"] || title,
219218
description,
@@ -230,7 +229,11 @@ class ObjectField extends Component {
230229
name={name}
231230
required={this.isRequired(name)}
232231
schema={schema.properties[name]}
233-
uiSchema={uiSchema[name]}
232+
uiSchema={
233+
addedByAdditionalProperties
234+
? uiSchema.additionalProperties
235+
: uiSchema[name]
236+
}
234237
errorSchema={errorSchema[name]}
235238
idSchema={idSchema[name]}
236239
idPrefix={idPrefix}

‎test/ArrayField_test.js

+16
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,22 @@ describe("ArrayField", () => {
11621162
expect(addInput.value).eql("bar");
11631163
});
11641164

1165+
it("should apply uiSchema to additionalItems", () => {
1166+
const { node } = createFormComponent({
1167+
schema: schemaAdditional,
1168+
uiSchema: {
1169+
additionalItems: {
1170+
"ui:title": "Custom title",
1171+
},
1172+
},
1173+
formData: [1, 2, "bar"],
1174+
});
1175+
const label = node.querySelector(
1176+
"fieldset .field-string label.control-label"
1177+
);
1178+
expect(label.textContent).eql("Custom title*");
1179+
});
1180+
11651181
it("should have an add button if additionalItems is an object", () => {
11661182
const { node } = createFormComponent({ schema: schemaAdditional });
11671183
expect(node.querySelector(".array-item-add button")).not.to.be.null;

‎test/ObjectField_test.js

+17
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,23 @@ describe("ObjectField", () => {
412412
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
413413
});
414414

415+
it("should apply uiSchema to additionalProperties", () => {
416+
const { node } = createFormComponent({
417+
schema,
418+
uiSchema: {
419+
additionalProperties: {
420+
"ui:title": "CustomName",
421+
},
422+
},
423+
formData: {
424+
property1: "test",
425+
},
426+
});
427+
const labels = node.querySelectorAll("label.control-label");
428+
expect(labels[0].textContent).eql("CustomName Key");
429+
expect(labels[1].textContent).eql("CustomName");
430+
});
431+
415432
it("should pass through non-schema properties and not throw validation errors if additionalProperties is undefined", () => {
416433
const undefinedAPSchema = {
417434
...schema,

0 commit comments

Comments
 (0)
Please sign in to comment.