Skip to content

Commit

Permalink
Specialized Question Types - The question.isDesignMode doesn't return…
Browse files Browse the repository at this point in the history
… the actual value fix #8767
  • Loading branch information
andrewtelnov committed Oct 14, 2024
1 parent e377ae5 commit 3a6a20a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
37 changes: 26 additions & 11 deletions packages/survey-core/src/question_custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface ICustomQuestionTypeConfiguration {
* A custom question.
*/
onLoaded?(question: Question): void;
onSetSurvey?(question: Question): void;
/**
* A function that is called after the entire question is rendered.
*
Expand Down Expand Up @@ -294,17 +295,25 @@ export class ComponentQuestionJSON {
);
this.onInit();
}
public onInit() {
if (!this.json.onInit) return;
this.json.onInit();
public onInit(): void {
if (!!this.json.onInit) {
this.json.onInit();
}
}
public onCreated(question: Question) {
if (!this.json.onCreated) return;
this.json.onCreated(question);
public onCreated(question: Question): void {
if (!!this.json.onCreated) {
this.json.onCreated(question);
}
}
public onLoaded(question: Question) {
if (!this.json.onLoaded) return;
this.json.onLoaded(question);
public onLoaded(question: Question): void {
if (!!this.json.onLoaded) {
this.json.onLoaded(question);
}
}
public onSetSurvey(question: Question): void {
if (!!this.json.onSetSurvey) {
this.json.onSetSurvey(question);
}
}
public onAfterRender(question: Question, htmlElement: any): void {
if (!this.json.onAfterRender) return;
Expand Down Expand Up @@ -598,7 +607,7 @@ export abstract class QuestionCustomModelBase extends Question
return res;
}
protected abstract getElement(): SurveyElement;
protected initElement(el: SurveyElement) {
protected initElement(el: SurveyElement): void {
if (!el) return;
el.setSurveyImpl(this);
el.disableDesignActions = true;
Expand All @@ -608,9 +617,15 @@ export abstract class QuestionCustomModelBase extends Question
this.isSettingValOnLoading = true;
super.setSurveyImpl(value, isLight);
this.initElement(this.getElement());
this.callSetSurvey();
this.isSettingValOnLoading = false;
}
public onSurveyLoad() {
private callSetSurvey(): void {
if(!!this.survey) {
this.customQuestion?.onSetSurvey(this);
}
}
public onSurveyLoad(): void {
super.onSurveyLoad();
if (!!this.getElement()) {
this.getElement().onSurveyLoad();
Expand Down
30 changes: 30 additions & 0 deletions packages/survey-core/tests/question_customtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3408,4 +3408,34 @@ QUnit.test("Dynamic serializable properties, bug#8852", function (assert) {
assert.equal(q1.contentQuestion.maxRateDescription, "val2", "maxRateDescription");
assert.equal(q1.contentQuestion.hasMinRateDescription, true, "hasMinRateDescription");
assert.equal(q1.contentQuestion.hasMaxRateDescription, true, "hasMaxRateDescription");
});
QUnit.test("Single: Register and load from json", function (assert) {
let isDesignMode1Counter = 0;
let isDesignMode2Counter = 0;
ComponentCollection.Instance.add({
name: "newquestion1",
questionJSON: { type: "text" },
onSetSurvey: (question: Question): void => {
isDesignMode1Counter += question.isDesignMode ? 10: 1;
}
});
ComponentCollection.Instance.add({
name: "newquestion2",
elementsJSON: [{ type: "text", name: "q1" }],
onSetSurvey: (question: Question): void => {
isDesignMode2Counter += question.isDesignMode ? 10: 1;
}
});
const survey = new SurveyModel();
survey.setDesignMode(true);
survey.fromJSON({
elements: [{ type: "newquestion1", name: "q1" }, { type: "newquestion2", name: "q2" }],
});
assert.equal(isDesignMode1Counter, 10, "isDesignMode1 #1");
assert.equal(isDesignMode2Counter, 10, "isDesignMode2 #1");
survey.currentPage.addNewQuestion("newquestion1", "q3");
survey.currentPage.addNewQuestion("newquestion2", "q4");
assert.equal(isDesignMode1Counter, 20, "isDesignMode1 #2");
assert.equal(isDesignMode2Counter, 20, "isDesignMode2 #2");
ComponentCollection.Instance.clear();
});

0 comments on commit 3a6a20a

Please sign in to comment.