Skip to content

Commit

Permalink
When a question value is set from code using the setValue function, t…
Browse files Browse the repository at this point in the history
…he setValueExpression stops being executed fix #9132 (#9134)
  • Loading branch information
andrewtelnov authored Dec 4, 2024
1 parent 8690415 commit bf38684
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
12 changes: 7 additions & 5 deletions packages/survey-core/src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,8 @@ export class Question extends SurveyElement<Question>
this.triggersInfo.push(info);
return info;
}
private runTriggerInfo(info: TriggerExpressionInfo, name: string, value: any): void {
private runTriggerInfo(info: TriggerExpressionInfo, keys: any): void {
const expression = this[info.name];
const keys: any = {};
keys[name] = value;
if (!expression || info.isRunning || !info.canRun()) {
if (info.runSecondCheck(keys)) {
info.doComplete();
Expand All @@ -619,10 +617,14 @@ export class Question extends SurveyElement<Question>
info.isRunning = true;
info.runner.run(this.getDataFilteredValues(), this.getDataFilteredProperties());
}
public runTriggers(name: string, value: any): void {
public runTriggers(name: string, value: any, keys?: any): void {
if (this.isSettingQuestionValue || (this.parentQuestion && this.parentQuestion.getValueName() === name)) return;
if(!keys) {
keys = {};
keys[name] = value;
}
this.triggersInfo.forEach(info => {
this.runTriggerInfo(info, name, value);
this.runTriggerInfo(info, keys);
});
}
private runConditions() {
Expand Down
6 changes: 3 additions & 3 deletions packages/survey-core/src/question_matrixdropdownbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,9 +1491,9 @@ export class QuestionMatrixDropdownModelBase extends QuestionMatrixBaseModel<Mat
);
this.updateVisibilityBasedOnRows();
}
public runTriggers(name: string, value: any): void {
super.runTriggers(name, value);
this.runFuncForCellQuestions((q: Question) => { q.runTriggers(name, value); });
public runTriggers(name: string, value: any, keys?: any): void {
super.runTriggers(name, value, keys);
this.runFuncForCellQuestions((q: Question) => { q.runTriggers(name, value, keys); });
}
public updateElementVisibility(): void {
super.updateElementVisibility();
Expand Down
6 changes: 3 additions & 3 deletions packages/survey-core/src/question_paneldynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1855,10 +1855,10 @@ export class QuestionPanelDynamicModel extends Question
super.runCondition(values, properties);
this.runPanelsCondition(this.panelsCore, values, properties);
}
public runTriggers(name: string, value: any): void {
super.runTriggers(name, value);
public runTriggers(name: string, value: any, keys?: any): void {
super.runTriggers(name, value, keys);
this.visiblePanelsCore.forEach(p => {
p.questions.forEach(q => q.runTriggers(name, value));
p.questions.forEach(q => q.runTriggers(name, value, keys));
});
}
private reRunCondition() {
Expand Down
11 changes: 10 additions & 1 deletion packages/survey-core/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6090,13 +6090,20 @@ export class SurveyModel extends SurveyElementCore
}
}
}
private questionTriggersKeys: any;
private runConditionOnValueChanged(name: string, value: any) {
if (this.isRunningConditions) {
this.conditionValues[name] = value;
if(this.questionTriggersKeys) {
this.questionTriggersKeys[name] = value;
}
this.isValueChangedOnRunningCondition = true;
} else {
this.questionTriggersKeys = {};
this.questionTriggersKeys[name] = value;
this.runConditions();
this.runQuestionsTriggers(name, value);
this.questionTriggersKeys = undefined;
}
}
private runConditionsCore(properties: any) {
Expand All @@ -6119,7 +6126,9 @@ export class SurveyModel extends SurveyElementCore
private runQuestionsTriggers(name: string, value: any): void {
if (this.isDisplayMode || this.isDesignMode) return;
const questions = this.getAllQuestions();
questions.forEach(q => q.runTriggers(name, value));
questions.forEach(q => {
q.runTriggers(name, value, this.questionTriggersKeys);
});
}
private checkIfNewPagesBecomeVisible(oldCurrentPageIndex: number) {
var newCurrentPageIndex = this.pages.indexOf(this.currentPage);
Expand Down
55 changes: 55 additions & 0 deletions packages/survey-core/tests/surveyquestiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7907,3 +7907,58 @@ QUnit.test("Show parent number in the question #8813, #3", function (assert) {
assert.equal(survey.getQuestionByName("q6").no, "2.", "q6.no");
assert.equal(survey.getQuestionByName("q7").no, "3.", "q7.no");
});
QUnit.test("Recursive changes setValueExpression #9132", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "dropdown",
name: "question1",
choices: ["Item 1", "Item 2", "Item 3"],
},
{
type: "text",
name: "question2",
setValueExpression:
"iif({question1} = 'Item 1', 1, iif({question1} = 'Item 2', 2, iif({question1} = 'Item 3', 3, 0)))",
inputType: "number",
},
{
type: "dropdown",
name: "question3",
choices: ["Item 1", "Item 2", "Item 3"],
},
{
type: "text",
name: "question4",
setValueExpression:
"iif({question3} = 'Item 1', 1, iif({question3} = 'Item 2', 2, iif({question3} = 'Item 3', 3, 0)))",
inputType: "number",
},
{
type: "text",
name: "question5",
defaultValueExpression: "{question2}+{question4}",
inputType: "number",
readOnly: true,
},
{
type: "text",
name: "question6",
setValueExpression:
"iif({question5} > 5, 'High Risk', iif({question5} > 3, 'Medium Risk', iif({question5} > 1, 'Low Risk', '')))"
}
],
});
const q6 = survey.getQuestionByName("question6");
assert.equal(q6.isEmpty(), true, "#0");
survey.setValue("question6", "test");
assert.equal(q6.value, "test", "#1");
survey.setValue("question1", "Item 1");
assert.equal(q6.value, "", "#2");
survey.setValue("question3", "Item 1");
assert.equal(q6.value, "Low Risk", "#3");
survey.setValue("question1", "Item 3");
assert.equal(q6.value, "Medium Risk", "#4");
survey.setValue("question3", "Item 3");
assert.equal(q6.value, "High Risk", "#5");
});

0 comments on commit bf38684

Please sign in to comment.