diff --git a/packages/core/src/components/fields/ObjectField.js b/packages/core/src/components/fields/ObjectField.js index c154dc188d..340b70e1e4 100644 --- a/packages/core/src/components/fields/ObjectField.js +++ b/packages/core/src/components/fields/ObjectField.js @@ -214,14 +214,7 @@ class ObjectField extends Component { const { SchemaField, TitleField, DescriptionField } = fields; const schema = retrieveSchema(this.props.schema, rootSchema, formData); - // If this schema has a title defined, but the user has set a new key/label, retain their input. - let title; - if (this.state.wasPropertyKeyModified) { - title = name; - } else { - title = schema.title === undefined ? name : schema.title; - } - + const title = schema.title === undefined ? name : schema.title; const description = uiSchema["ui:description"] || schema.description; let orderedProperties; try { diff --git a/packages/core/test/ObjectField_test.js b/packages/core/test/ObjectField_test.js index 154508ab25..1468f8022e 100644 --- a/packages/core/test/ObjectField_test.js +++ b/packages/core/test/ObjectField_test.js @@ -565,7 +565,7 @@ describe("ObjectField", () => { }); }); - it("should retain user-input data if key-value pair has a title present in the schema", () => { + it("should retain and display user-input data if key-value pair has a title present in the schema when renaming key", () => { const { node, onChange } = createFormComponent({ schema: { type: "object", @@ -585,6 +585,35 @@ describe("ObjectField", () => { sinon.assert.calledWithMatch(onChange.lastCall, { formData: { "Renamed custom title": 1 }, }); + + const keyInput = node.querySelector("#root_Renamed\\ custom\\ title-key"); + expect(keyInput.value).eql("Renamed custom title"); + + const keyInputLabel = node.querySelector( + 'label[for="root_Renamed\\ custom\\ title-key"]' + ); + expect(keyInputLabel.textContent).eql("Renamed custom title Key"); + }); + + it("should retain object title when renaming key", () => { + const { node } = createFormComponent({ + schema: { + title: "Object title", + type: "object", + additionalProperties: { + type: "string", + }, + }, + formData: { "Custom title": 1 }, + }); + + const textNode = node.querySelector("#root_Custom\\ title-key"); + Simulate.blur(textNode, { + target: { value: "Renamed custom title" }, + }); + + const title = node.querySelector("#root__title"); + expect(title.textContent).eql("Object title"); }); it("should keep order of renamed key-value pairs while renaming key", () => {