Skip to content

Commit 75f734d

Browse files
authored
additionalProperties: retain object title when renaming key (rjsf-team#1751)
Fixes rjsf-team#1749. This reverts part of rjsf-team#1401 (the conditional setting of the title in the wasPropertyKeyModified logic in ObjectField.js) -- this piece of logic doesn't seem to contribute to the fix in rjsf-team#1401, and it's what caused rjsf-team#1749. Additionally, the test that was added in rjsf-team#1401 didn't actually check to make sure the issue it was solving was resolved; that test passed even before rjsf-team#1401 was merged. The issue that rjsf-team#1401 was that onChange was called with the new key, but the new key did not show up in the DOM. I've updated that test to check for this behavior (updating the new key in the DOM) now.
1 parent 3e0ee10 commit 75f734d

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

packages/core/src/components/fields/ObjectField.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,7 @@ class ObjectField extends Component {
214214
const { SchemaField, TitleField, DescriptionField } = fields;
215215
const schema = retrieveSchema(this.props.schema, rootSchema, formData);
216216

217-
// If this schema has a title defined, but the user has set a new key/label, retain their input.
218-
let title;
219-
if (this.state.wasPropertyKeyModified) {
220-
title = name;
221-
} else {
222-
title = schema.title === undefined ? name : schema.title;
223-
}
224-
217+
const title = schema.title === undefined ? name : schema.title;
225218
const description = uiSchema["ui:description"] || schema.description;
226219
let orderedProperties;
227220
try {

packages/core/test/ObjectField_test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ describe("ObjectField", () => {
565565
});
566566
});
567567

568-
it("should retain user-input data if key-value pair has a title present in the schema", () => {
568+
it("should retain and display user-input data if key-value pair has a title present in the schema when renaming key", () => {
569569
const { node, onChange } = createFormComponent({
570570
schema: {
571571
type: "object",
@@ -585,6 +585,35 @@ describe("ObjectField", () => {
585585
sinon.assert.calledWithMatch(onChange.lastCall, {
586586
formData: { "Renamed custom title": 1 },
587587
});
588+
589+
const keyInput = node.querySelector("#root_Renamed\\ custom\\ title-key");
590+
expect(keyInput.value).eql("Renamed custom title");
591+
592+
const keyInputLabel = node.querySelector(
593+
'label[for="root_Renamed\\ custom\\ title-key"]'
594+
);
595+
expect(keyInputLabel.textContent).eql("Renamed custom title Key");
596+
});
597+
598+
it("should retain object title when renaming key", () => {
599+
const { node } = createFormComponent({
600+
schema: {
601+
title: "Object title",
602+
type: "object",
603+
additionalProperties: {
604+
type: "string",
605+
},
606+
},
607+
formData: { "Custom title": 1 },
608+
});
609+
610+
const textNode = node.querySelector("#root_Custom\\ title-key");
611+
Simulate.blur(textNode, {
612+
target: { value: "Renamed custom title" },
613+
});
614+
615+
const title = node.querySelector("#root__title");
616+
expect(title.textContent).eql("Object title");
588617
});
589618

590619
it("should keep order of renamed key-value pairs while renaming key", () => {

0 commit comments

Comments
 (0)