Skip to content

Commit

Permalink
defaultValue may work incorrectly for one question per page mode fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov authored Mar 7, 2024
1 parent 3f451f3 commit e63ca46
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
"revealWithFocus": "test-results",
"clearOnRun": "none"
},
"testing.openTesting": "neverOpen",
}
22 changes: 11 additions & 11 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ export class SurveyModel extends SurveyElementCore
public getType(): string {
return "survey";
}
protected onPropertyValueChanged(name: string, oldValue: any, newValue: any) {
protected onPropertyValueChanged(name: string, oldValue: any, newValue: any): void {
if (name === "questionsOnPageMode") {
this.onQuestionsOnPageModeChanged(oldValue);
}
Expand Down Expand Up @@ -3589,7 +3589,7 @@ export class SurveyModel extends SurveyElementCore
return this._isDesignMode;
}
private _isDesignMode: boolean = false;
public setDesignMode(value: boolean) {
public setDesignMode(value: boolean): void {
if (!!this._isDesignMode != !!value) {
this._isDesignMode = !!value;
this.onQuestionsOnPageModeChanged("standard");
Expand Down Expand Up @@ -4174,7 +4174,7 @@ export class SurveyModel extends SurveyElementCore
if (this.isDesignMode) return;
if (this.isShowingPreview) {
this.runningPages = this.pages.slice(0, this.pages.length);
this.setupPagesForPageModes(true);
this.setupPagesForPageModes(true, false);
} else {
if (this.runningPages) {
this.restoreOriginalPages(this.runningPages);
Expand All @@ -4201,7 +4201,7 @@ export class SurveyModel extends SurveyElementCore
}
private changeCurrentPageFromPreview: boolean;
private originalPages: any;
protected onQuestionsOnPageModeChanged(oldValue: string) {
protected onQuestionsOnPageModeChanged(oldValue: string, isFirstLoad: boolean = false): void {
if (this.isShowingPreview) return;
if (this.questionsOnPageMode == "standard" || this.isDesignMode) {
if (this.originalPages) {
Expand All @@ -4212,7 +4212,7 @@ export class SurveyModel extends SurveyElementCore
if (!oldValue || oldValue == "standard") {
this.originalPages = this.pages.slice(0, this.pages.length);
}
this.setupPagesForPageModes(this.isSinglePage);
this.setupPagesForPageModes(this.isSinglePage, isFirstLoad);
}
this.runConditions();
this.updateVisibleIndexes();
Expand All @@ -4229,10 +4229,10 @@ export class SurveyModel extends SurveyElementCore
private getPageStartIndex(): number {
return this.firstPageIsStarted && this.pages.length > 0 ? 1 : 0;
}
private isCreatingPagesForPreview: boolean;
private setupPagesForPageModes(isSinglePage: boolean) {
private isLockingUpdateOnPageModes: boolean;
private setupPagesForPageModes(isSinglePage: boolean, isFirstLoad: boolean) {
this.questionHashesClear();
this.isCreatingPagesForPreview = true;
this.isLockingUpdateOnPageModes = !isFirstLoad;
var startIndex = this.getPageStartIndex();
super.startLoadingFromJson();
var newPages = this.createPagesForQuestionOnPageMode(
Expand All @@ -4250,7 +4250,7 @@ export class SurveyModel extends SurveyElementCore
}
this.doElementsOnLoad();
this.updateCurrentPage();
this.isCreatingPagesForPreview = false;
this.isLockingUpdateOnPageModes = false;
}
private createPagesForQuestionOnPageMode(
isSinglePage: boolean,
Expand Down Expand Up @@ -6116,7 +6116,7 @@ export class SurveyModel extends SurveyElementCore
endLoadingFromJson() {
this.isEndLoadingFromJson = "processing";
this.onFirstPageIsStartedChanged();
this.onQuestionsOnPageModeChanged("standard");
this.onQuestionsOnPageModeChanged("standard", true);
super.endLoadingFromJson();
if (this.hasCookie) {
this.isCompletedBefore = true;
Expand Down Expand Up @@ -6443,7 +6443,7 @@ export class SurveyModel extends SurveyElementCore
allowNotifyValueChanged: boolean = true,
questionName?: string
): void {
if (this.isCreatingPagesForPreview) return;
if (this.isLockingUpdateOnPageModes) return;
var newValue = newQuestionValue;
if (allowNotifyValueChanged) {
newValue = this.questionOnValueChanging(name, newQuestionValue);
Expand Down
56 changes: 56 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18579,6 +18579,62 @@ QUnit.test("survey.toJSON() doesn't work correctly if questionsOnPageMode=questi

assert.deepEqual(surveyJson, prepareJSON);
});
QUnit.test("defaultValue & visibleIf issues if questionsOnPageMode=questionPerPage is used #7932", function (assert) {
const surveyJson = {
elements: [
{
type: "panel",
name: "panel1",
elements: [
{
type: "radiogroup",
name: "q1_1",
choices: ["A", "B"],
defaultValue: "A",
},
{
type: "text",
name: "q1_2",
visibleIf:
"{q1_1} equals 'A'",
},
],
},
{
type: "panel",
name: "panel2",
elements: [
{
type: "radiogroup",
name: "q2_1",
choices: ["A", "B"],
defaultValue: "A",
},
{
type: "text",
name: "q2_2",
visibleIf:
"{q2_1} equals 'A'",
},
],
}
],
questionsOnPageMode: "questionPerPage",
};

const survey = new SurveyModel(surveyJson);
assert.deepEqual(survey.data, { q1_1: "A", q2_1: "A" }, "survey.data");
survey.setValue("q3", "B");
const q1_1 = survey.getQuestionByName("q1_1");
const q2_1 = survey.getQuestionByName("q2_1");
const q1_2 = survey.getQuestionByName("q1_2");
const q2_2 = survey.getQuestionByName("q2_2");

assert.equal(q1_1.value, "A", "q1_1.value");
assert.equal(q2_1.value, "A", "q2_1.value");
assert.equal(q1_2.visible, true, "q1_2.visible");
assert.equal(q2_2.visible, true, "q2_2.visible");
});

QUnit.test("Bug on loading json with collapsed panel. It was fixed in v1.9.117, #7355", function (assert) {
const survey = new SurveyModel({
Expand Down

0 comments on commit e63ca46

Please sign in to comment.