Skip to content

Commit

Permalink
Cannot read properties of undefined (reading 'title') is thrown when …
Browse files Browse the repository at this point in the history
…adding and removing a toolbox item dynamically fix #5271 (#5272)
  • Loading branch information
andrewtelnov committed Feb 27, 2024
1 parent 7ab2b1f commit 9186bf2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3191,14 +3191,13 @@ export class SurveyCreatorModel extends Base
currentAddQuestionType = this.currentAddQuestionType;
if (!!currentAddQuestionType) {
const str = this.getLocString("ed.addNewTypeQuestion");
if (!!str && !!str["format"])
return str["format"](
this.toolbox.items.filter((item) => item.name == currentAddQuestionType)[0].title
);
const items = this.toolbox.items.filter((item) => item.name == currentAddQuestionType);
if (Array.isArray(items) && items.length > 0 && !!str && !!str["format"]) {
return str["format"](items[0].title);
}
}
return this.getLocString("ed.addNewQuestion");
}

public get addNewQuestionText() {
return this.getAddNewQuestionText();
}
Expand Down
13 changes: 13 additions & 0 deletions packages/survey-creator-core/tests/creator-base.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4377,4 +4377,17 @@ test("New ghost page shouldn't be created if onPageAdding sets allow to false",
expect(creator.survey.pages).toHaveLength(1);
expect(desigerTab.newPage).toBeFalsy();
expect(desigerTab.showNewPage).toBeFalsy();
});
test("Add-remove toolbox items, #5271", (): any => {
const creator = new CreatorTester();
creator.toolbox.allowExpandMultipleCategories = true;
creator.toolbox.showCategoryTitles = true;
creator.onQuestionAdded.add((sender, options) => {
if (options.question.getType() === "dropdown") {
creator.toolbox.removeItem("dropdown");
}
});
creator.currentAddQuestionType = "dropdown";
creator.addNewQuestionInPage(() => {}, undefined, "dropdown");
expect(creator.getAddNewQuestionText()).toEqual("Add Question");
});

0 comments on commit 9186bf2

Please sign in to comment.