diff --git a/packages/survey-core/src/page.ts b/packages/survey-core/src/page.ts index ea3a648ba0..7ee664d446 100644 --- a/packages/survey-core/src/page.ts +++ b/packages/survey-core/src/page.ts @@ -279,7 +279,7 @@ export class PageModel extends PanelModel implements IPage { get hasShown(): boolean { return this.wasShown; } - public setWasShown(val: boolean) { + public setWasShown(val: boolean): void { if (val == this.hasShownValue) return; this.hasShownValue = val; if (this.isDesignMode || val !== true) return; @@ -289,7 +289,12 @@ export class PageModel extends PanelModel implements IPage { (els[i]).randomizeElements(this.areQuestionsRandomized); } } - this.randomizeElements(this.areQuestionsRandomized); + if(this.randomizeElements(this.areQuestionsRandomized)) { + const singleQuestion: any = this.survey?.currentSingleQuestion; + if(singleQuestion?.page === this) { + this.survey.currentSingleQuestion = this.getFirstVisibleQuestion(); + } + } } /** * Scrolls this page to the top. diff --git a/packages/survey-core/src/panel.ts b/packages/survey-core/src/panel.ts index 0f34fe69f3..ac2b62ed8a 100644 --- a/packages/survey-core/src/panel.ts +++ b/packages/survey-core/src/panel.ts @@ -567,8 +567,8 @@ export class PanelModelBase extends SurveyElement return isRandom && (this.questionOrder !== "initial") || this.questionOrder === "random"; } protected isRandomizing = false; - randomizeElements(isRandom: boolean): void { - if (!this.canRandomize(isRandom) || this.isRandomizing) return; + randomizeElements(isRandom: boolean): boolean { + if (!this.canRandomize(isRandom) || this.isRandomizing) return false; this.isRandomizing = true; var oldElements = []; var elements = this.elements; @@ -580,6 +580,7 @@ export class PanelModelBase extends SurveyElement this.updateRows(); this.updateVisibleIndexes(); this.isRandomizing = false; + return true; } /** * Returns `true` if elements in this panel/page are arranged in random order. @@ -1060,6 +1061,13 @@ export class PanelModelBase extends SurveyElement } return null; } + getFirstVisibleQuestion(): Question { + const qs = this.questions; + for (let i = 0; i < qs.length; i++) { + if (qs[i].isVisible) return qs[i]; + } + return null; + } /** * Focuses the first question in this panel/page. * @see focusFirstErrorQuestion diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 4ed533597e..cbc517ec71 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -7812,6 +7812,29 @@ QUnit.test("Randomize questions in page and panels", function (assert) { Helpers.randomizeArray = oldFunc; }); +QUnit.test("Randomize questions in page and panels & single question per page", function (assert) { + const oldFunc = Helpers.randomizeArray; + Helpers.randomizeArray = HelpTest.randomizeArray; + + const survey = new SurveyModel({ + questionsOnPageMode: "questionPerPage", + questionOrder: "random", + pages: [ + { elements: [{ type: "text", name: "q1" }, { type: "text", name: "q2" }, { type: "text", name: "q3" }] }, + { elements: [{ type: "text", name: "q4" }, { type: "text", name: "q5" }, { type: "text", name: "q6" }] }, + ] + }); + const page = survey.pages[0]; + assert.equal(page.elements[0].name, "q3"); + assert.equal(page.elements[2].name, "q1"); + assert.equal(survey.currentSingleQuestion.name, "q3", "The first question is q3"); + survey.performNext(); + survey.performNext(); + survey.performNext(); + assert.equal(survey.currentSingleQuestion.name, "q6", "The current question is q6"); + + Helpers.randomizeArray = oldFunc; +}); QUnit.test("Quiz, correct, incorrect answers", function (assert) { var survey = new SurveyModel({