diff --git a/packages/survey-creator-core/src/creator-base.ts b/packages/survey-creator-core/src/creator-base.ts index 0bcc3c0b6b..65f158850f 100644 --- a/packages/survey-creator-core/src/creator-base.ts +++ b/packages/survey-creator-core/src/creator-base.ts @@ -2363,9 +2363,23 @@ export class SurveyCreatorModel extends Base } public createNewElement(json: any): IElement { - var newElement = Serializer.createClass(json["type"]); + const newElement = Serializer.createClass(json["type"]); new JsonObject().toObject(json, newElement); - this.setNewNames(newElement); + let needNewName = true; + if(!!json.name) { + if(newElement.isPage) { + needNewName = !!this.survey.getPageByName(newElement.name); + } else { + if(newElement.isPanel) { + needNewName = !!this.survey.getPanelByName(newElement.name); + } else { + needNewName = !!this.survey.getQuestionByName(newElement.name); + } + } + } + if(needNewName) { + this.setNewNames(newElement); + } return newElement; } diff --git a/packages/survey-creator-core/src/toolbox.ts b/packages/survey-creator-core/src/toolbox.ts index 840d166075..7593c77af4 100644 --- a/packages/survey-creator-core/src/toolbox.ts +++ b/packages/survey-creator-core/src/toolbox.ts @@ -892,13 +892,14 @@ export class QuestionToolbox const defaultCategories = useDefaultCategories ? this.getDefaultQuestionCategories() : {}; for (var i = 0; i < questions.length; i++) { - var name = questions[i]; - var question = ElementFactory.Instance.createElement(name, "q1"); + const name = questions[i]; + let question = ElementFactory.Instance.createElement(name, "q1"); if (!question) { question = Serializer.createClass(name); } - var json = this.getQuestionJSON(question); - var title = editorLocalization.getString("qt." + name); + const json = this.getQuestionJSON(question); + delete json.name; + const title = editorLocalization.getString("qt." + name); const iconName = "icon-" + name; const item: IQuestionToolboxItem = { id: name, @@ -952,10 +953,11 @@ export class QuestionToolbox if (!title) { title = json.name; } - var elementJson = json.defaultJSON ? json.defaultJSON : {}; + var elementJson = json.defaultJSON ? JSON.parse(JSON.stringify(json.defaultJSON)) : {}; if (!elementJson.type) { elementJson.type = json.name; } + delete elementJson.name; var category = json.category ? json.category : ""; const item: IQuestionToolboxItem = new Action({ id: json.name, diff --git a/packages/survey-creator-core/tests/creator-toolbox.tests.ts b/packages/survey-creator-core/tests/creator-toolbox.tests.ts index 762d463508..f1355918e9 100644 --- a/packages/survey-creator-core/tests/creator-toolbox.tests.ts +++ b/packages/survey-creator-core/tests/creator-toolbox.tests.ts @@ -62,6 +62,20 @@ test("Click on toolbox and insert into correct index", (): any => { expect(creator.selectedElementName).toEqual("question3"); expect(creator.survey.currentPage.elements[1].name).toEqual("question3"); }); +test("Try to use name property from JSON", (): any => { + const creator = new CreatorTester(); + creator.JSON = { + elements: [ + { type: "text", name: "question1" }, + { type: "text", name: "question2" } + ] + }; + creator.selectElement(creator.survey.getQuestionByName("question1")); + creator.clickToolboxItem({ type: "text", name: "text1" }); + expect(creator.selectedElementName).toEqual("text1"); + creator.clickToolboxItem({ type: "text", name: "text1" }); + expect(creator.selectedElementName).toEqual("question3"); +}); test("Convert checkbox into rating", (): any => { const creator = new CreatorTester(); creator.JSON = {