mirror of
https://github.com/zhigang1992/react-jsonschema-form.git
synced 2026-05-19 02:41:08 +08:00
Fix uiSchema for additionalProperties (#1144)
* fix: uiSchema for additionalProperties #1132 * doc: update readme * doc: fix toc * doc: update docs
This commit is contained in:
@@ -248,12 +248,24 @@ const uiSchema = {
|
||||
};
|
||||
```
|
||||
|
||||
### Object item options
|
||||
### Object additional properties
|
||||
|
||||
You can define `additionalProperties` by setting its value to a schema object, such as the following:
|
||||
|
||||
```js
|
||||
const schema = {
|
||||
"type": "object",
|
||||
"properties": {"type": "string"},
|
||||
"additionalProperties": {"type": "number"}
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
You can also define `uiSchema` options for `additionalProperties` by setting the `additionalProperties` attribute in the `uiSchema`.
|
||||
|
||||
#### `expandable` option
|
||||
|
||||
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.
|
||||
|
||||
You can turn support for `additionalProperties` off with the `expandable` option in `uiSchema`:
|
||||
|
||||
```jsx
|
||||
|
||||
@@ -213,7 +213,6 @@ class ObjectField extends Component {
|
||||
}
|
||||
|
||||
const Template = registry.ObjectFieldTemplate || DefaultObjectFieldTemplate;
|
||||
|
||||
const templateProps = {
|
||||
title: uiSchema["ui:title"] || title,
|
||||
description,
|
||||
@@ -230,7 +229,11 @@ class ObjectField extends Component {
|
||||
name={name}
|
||||
required={this.isRequired(name)}
|
||||
schema={schema.properties[name]}
|
||||
uiSchema={uiSchema[name]}
|
||||
uiSchema={
|
||||
addedByAdditionalProperties
|
||||
? uiSchema.additionalProperties
|
||||
: uiSchema[name]
|
||||
}
|
||||
errorSchema={errorSchema[name]}
|
||||
idSchema={idSchema[name]}
|
||||
idPrefix={idPrefix}
|
||||
|
||||
@@ -1162,6 +1162,22 @@ describe("ArrayField", () => {
|
||||
expect(addInput.value).eql("bar");
|
||||
});
|
||||
|
||||
it("should apply uiSchema to additionalItems", () => {
|
||||
const { node } = createFormComponent({
|
||||
schema: schemaAdditional,
|
||||
uiSchema: {
|
||||
additionalItems: {
|
||||
"ui:title": "Custom title",
|
||||
},
|
||||
},
|
||||
formData: [1, 2, "bar"],
|
||||
});
|
||||
const label = node.querySelector(
|
||||
"fieldset .field-string label.control-label"
|
||||
);
|
||||
expect(label.textContent).eql("Custom title*");
|
||||
});
|
||||
|
||||
it("should have an add button if additionalItems is an object", () => {
|
||||
const { node } = createFormComponent({ schema: schemaAdditional });
|
||||
expect(node.querySelector(".array-item-add button")).not.to.be.null;
|
||||
|
||||
@@ -412,6 +412,23 @@ describe("ObjectField", () => {
|
||||
expect(node.querySelectorAll(".field-string")).to.have.length.of(1);
|
||||
});
|
||||
|
||||
it("should apply uiSchema to additionalProperties", () => {
|
||||
const { node } = createFormComponent({
|
||||
schema,
|
||||
uiSchema: {
|
||||
additionalProperties: {
|
||||
"ui:title": "CustomName",
|
||||
},
|
||||
},
|
||||
formData: {
|
||||
property1: "test",
|
||||
},
|
||||
});
|
||||
const labels = node.querySelectorAll("label.control-label");
|
||||
expect(labels[0].textContent).eql("CustomName Key");
|
||||
expect(labels[1].textContent).eql("CustomName");
|
||||
});
|
||||
|
||||
it("should pass through non-schema properties and not throw validation errors if additionalProperties is undefined", () => {
|
||||
const undefinedAPSchema = {
|
||||
...schema,
|
||||
|
||||
Reference in New Issue
Block a user