From 30402f6b8d708acccfd9e0cf6c9b0c219ad18c85 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 1 Nov 2023 12:12:37 +0200 Subject: [PATCH] defaultValueExpression doesn't work after setting survey.data fix #7276 (#7277) --- src/question.ts | 29 +++++++++++++++++++++-------- tests/surveyquestiontests.ts | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/question.ts b/src/question.ts index de6be3c5af..cb0d5d15fd 100644 --- a/src/question.ts +++ b/src/question.ts @@ -1861,15 +1861,18 @@ export class Question extends SurveyElement return runner; } protected setDefaultValue(): void { + this.setDefaultValueCore((val: any): void => { + if (!this.isTwoValueEquals(this.value, val)) { + this.value = val; + } + }); + } + private setDefaultValueCore(func: (val: any) => void): void { this.defaultValueRunner = this.getDefaultRunner(this.defaultValueRunner, this.defaultValueExpression); this.setValueAndRunExpression( this.defaultValueRunner, this.getUnbindValue(this.defaultValue), - (val) => { - if (!this.isTwoValueEquals(this.value, val)) { - this.value = val; - } - } + (val) => func(val) ); } protected isValueExpression(val: any): boolean { @@ -2279,12 +2282,22 @@ export class Question extends SurveyElement newValue = this.valueFromDataCallback(newValue); } if (!this.checkIsValueCorrect(newValue)) return; - this.isChangingViaDefaultValue = this.isValueEmpty(newValue); - this.setQuestionValue(this.valueFromData(newValue)); - this.isChangingViaDefaultValue = false; + const isEmpty = this.isValueEmpty(newValue); + if(!isEmpty && this.defaultValueExpression) { + this.setDefaultValueCore((val: any): void => { + this.updateValueFromSurveyCore(newValue, this.isTwoValueEquals(newValue, val)); + }); + } else { + this.updateValueFromSurveyCore(newValue, isEmpty); + } this.updateDependedQuestions(); this.updateIsAnswered(); } + private updateValueFromSurveyCore(newValue: any, viaDefaultVal: boolean): void { + this.isChangingViaDefaultValue = viaDefaultVal; + this.setQuestionValue(this.valueFromData(newValue)); + this.isChangingViaDefaultValue = false; + } updateCommentFromSurvey(newValue: any): any { this.questionComment = newValue; } diff --git a/tests/surveyquestiontests.ts b/tests/surveyquestiontests.ts index f8a8162a6e..7be1b52704 100644 --- a/tests/surveyquestiontests.ts +++ b/tests/surveyquestiontests.ts @@ -7283,6 +7283,21 @@ QUnit.test("Set array and convert it to a string & defaultValueExpression, bug#6 assert.equal(q2.value, "a\nb\nc", "q2"); assert.equal(q3.value, "a, b, c", "q3"); }); +QUnit.test("defaultValueExpression & set data", function (assert) { + const survey = new SurveyModel({ + elements: [ + { type: "text", name: "q1" }, + { type: "text", name: "q2" }, + { type: "text", name: "q3", defaultValueExpression: "{q1} + {q2}" } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q3 = survey.getQuestionByName("q3"); + survey.data = { q1: 1, q2: 2, q3: 3 }; + assert.equal(q3.value, 3, "Value is set correctly"); + q1.value = 5; + assert.equal(q3.value, 7, "Value is changed based on expression"); +}); QUnit.test("question.resetValueIf, basic functionality", function (assert) { const survey = new SurveyModel({ elements: [{