Skip to content

Commit

Permalink
Use json.name to set question name on dropping question from a toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Jun 26, 2024
1 parent 2082d78 commit 459e46a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 16 additions & 2 deletions packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
12 changes: 7 additions & 5 deletions packages/survey-creator-core/src/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = <any>new Action(<any>{
id: json.name,
Expand Down
14 changes: 14 additions & 0 deletions packages/survey-creator-core/tests/creator-toolbox.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 459e46a

Please sign in to comment.