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

defaultValueExpression doesn't work after setting survey.data fix #7276 #7277

Merged
merged 1 commit into from
Nov 1, 2023
Merged
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
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