Skip to content

Commit

Permalink
defaultValueExpression doesn't work after setting survey.data fix #7276
Browse files Browse the repository at this point in the history
… (#7277)
  • Loading branch information
andrewtelnov committed Nov 1, 2023
1 parent c47438f commit 30402f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,15 +1861,18 @@ export class Question extends SurveyElement<Question>
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 {
Expand Down Expand Up @@ -2279,12 +2282,22 @@ export class Question extends SurveyElement<Question>
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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/surveyquestiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down

0 comments on commit 30402f6

Please sign in to comment.