Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialized Question Types - The question.isDesignMode doesn't return… #8931

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});