FIX: Error encountered when adding child object in object setting editor (#26259)

Why this change?

If an object doesn't have any child objects for a particular property
and we try to add one through the editor, an error will be raised.

```
Cannot read properties of undefined (reading 'push')
    at SchemaThemeSettingEditor.addItem (editor.js:190:1)
```
This commit is contained in:
Alan Guo Xiang Tan 2024-03-21 11:35:49 +08:00 committed by GitHub
parent 4c667f16c7
commit e5566b8519
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 4 deletions

View File

@ -69,7 +69,9 @@ export default class SchemaThemeSettingEditor extends Component {
index,
schema,
object,
text: object[schema.identifier] || `${schema.name} ${index + 1}`,
text:
object[schema.identifier] ||
this.defaultSchemaIdentifier(schema.name, index),
parentTree: tree,
});
@ -84,7 +86,7 @@ export default class SchemaThemeSettingEditor extends Component {
const subtree = new Tree();
subtree.propertyName = childObjectsProperty.name;
subtree.schema = childObjectsProperty.schema;
subtree.data = data[index][childObjectsProperty.name];
subtree.data = data[index][childObjectsProperty.name] ||= [];
data[index][childObjectsProperty.name]?.forEach(
(childObj, childIndex) => {
@ -232,6 +234,7 @@ export default class SchemaThemeSettingEditor extends Component {
const data = this.activeNode.parentTree.data;
data.splice(this.activeIndex, 1);
this.tree.nodes = this.tree.nodes.filter((n, i) => i !== this.activeIndex);
if (data.length > 0) {
this.activeIndex = Math.max(this.activeIndex - 1, 0);
} else if (this.history.length > 0) {
@ -257,10 +260,14 @@ export default class SchemaThemeSettingEditor extends Component {
return descriptions[key];
}
defaultSchemaIdentifier(schemaName, index) {
return `${schemaName} ${index + 1}`;
}
createNodeFromSchema(schema, tree) {
const object = {};
const index = tree.nodes.length;
const defaultName = `${schema.name} ${index + 1}`;
const defaultName = this.defaultSchemaIdentifier(schema.name, index);
if (schema.identifier) {
object[schema.identifier] = defaultName;

View File

@ -25,7 +25,7 @@ class TreeFromDOM {
const children = [
...queryAll(
`.schema-theme-setting-editor__tree-node.--child[data-test-parent-index="${index}"]:not(.--heading)`
`.schema-theme-setting-editor__tree-node.--child[data-test-parent-index="${index}"]`
),
].map((child) => {
return {
@ -824,6 +824,70 @@ module(
assert.dom(tree.nodes[3].textElement).hasText("level1 4");
});
test("adding an object to a child list of objects when an object has multiple objects properties", async function (assert) {
const setting = ThemeSettings.create({
setting: "objects_setting",
objects_schema: {
name: "something",
properties: {
title: {
type: "string",
},
links: {
type: "objects",
schema: {
name: "link",
properties: {
url: {
type: "string",
},
},
},
},
chairs: {
type: "objects",
schema: {
name: "chair",
properties: {
name: {
type: "string",
},
},
},
},
},
},
value: [
{
title: "some title",
},
],
});
await render(<template>
<AdminSchemaThemeSettingEditor @themeId="1" @setting={{setting}} />
</template>);
const tree = new TreeFromDOM();
await click(tree.nodes[0].addButtons[0]);
tree.refresh();
assert.dom(tree.nodes[0].children[0].textElement).hasText("link 1");
await click(tree.nodes[0].addButtons[1]);
tree.refresh();
assert.dom(tree.nodes[0].children[1].textElement).hasText("chair 1");
await click(tree.nodes[0].children[0].element);
const inputFields = new InputFieldsFromDOM();
assert.dom(inputFields.fields.url.labelElement).hasText("url");
});
test("adding an object to a child list of objects", async function (assert) {
const setting = schemaAndData(1);