Skip to content

Commit

Permalink
clearInvisibleValues onHiddenContainer breaks defaultValueExpression …
Browse files Browse the repository at this point in the history
…for text input fix #7010 (#7016)
  • Loading branch information
andrewtelnov committed Sep 26, 2023
1 parent be39f89 commit e099c25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { JsonObject, Serializer, property } from "./jsonobject";
import { Base, EventBase } from "./base";
import { IElement, IQuestion, IPanel, IConditionRunner, ISurveyImpl, IPage, ITitleOwner, IProgressInfo, ISurvey, IPlainDataOptions } from "./base-interfaces";
import { SurveyElement } from "./survey-element";
import { surveyLocalization } from "./surveyStrings";
import { AnswerRequiredError, CustomError } from "./error";
import { SurveyValidator, IValidatorOwner, ValidatorRunner } from "./validator";
import { TextPreProcessor, TextPreProcessorValue } from "./textPreProcessor";
import { TextPreProcessorValue } from "./textPreProcessor";
import { LocalizableString } from "./localizablestring";
import { ConditionRunner, ExpressionRunner } from "./conditions";
import { ExpressionRunner } from "./conditions";
import { QuestionCustomWidget } from "./questionCustomWidgets";
import { CustomWidgetCollection } from "./questionCustomWidgets";
import { settings, ISurveyEnvironment } from "./settings";
import { settings } from "./settings";
import { SurveyModel } from "./survey";
import { PanelModel } from "./panel";
import { RendererFactory } from "./rendererFactory";
Expand Down Expand Up @@ -428,6 +427,9 @@ export class Question extends SurveyElement<Question>
if (this.areInvisibleElementsShowing) return true;
return this.isVisibleCore();
}
public get isVisibleInSurvey(): boolean {
return this.isVisible && this.isParentVisible;
}
protected isVisibleCore(): boolean {
return this.visible;
}
Expand Down Expand Up @@ -614,7 +616,7 @@ export class Question extends SurveyElement<Question>
if (!this.visible) {
this.clearValueOnHidding(isClearOnHidden);
}
if (isClearOnHidden && this.isVisible) {
if (isClearOnHidden && this.isVisibleInSurvey) {
this.updateValueWithDefaults();
}
}
Expand Down Expand Up @@ -1307,7 +1309,7 @@ export class Question extends SurveyElement<Question>
if (!properties) properties = {};
properties["question"] = this;
this.runConditionCore(values, properties);
if (!this.isValueChangedDirectly) {
if (!this.isValueChangedDirectly && (!this.isClearValueOnHidden || this.isVisibleInSurvey)) {
this.defaultValueRunner = this.getDefaultRunner(this.defaultValueRunner, this.defaultValueExpression);
this.runDefaultValueExpression(this.defaultValueRunner, values, properties);
}
Expand Down Expand Up @@ -1473,7 +1475,7 @@ export class Question extends SurveyElement<Question>
}
private canClearValueAsInvisible(reason: string): boolean {
if (reason === "onHiddenContainer" && !this.isParentVisible) return true;
if (this.isVisible && this.isParentVisible) return false;
if (this.isVisibleInSurvey) return false;
if (!!this.page && this.page.isStartPage) return false;
if (!this.survey || !this.valueName) return true;
return !this.survey.hasVisibleQuestionByValueName(this.valueName);
Expand All @@ -1500,6 +1502,7 @@ export class Question extends SurveyElement<Question>
protected clearValueIfInvisibleCore(reason: string): void {
if (this.canClearValueAsInvisible(reason)) {
this.clearValue();
this.isValueChangedDirectly = undefined;
}
}
/**
Expand Down
35 changes: 35 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17894,3 +17894,38 @@ QUnit.test("Error on pre-processing localizable string Bug#6967", function (asse
assert.equal(survey.locCompleteText.renderedHtml, "2", "Preprocess correctly");
surveyLocalization.locales.en.completeText = prevVal;
});
QUnit.test("clearInvisibleValues onHiddenContainer breaks defaultValueExpression for text input #7010", function (assert) {
const survey = new SurveyModel({
"elements": [
{
"type": "radiogroup",
"name": "q1",
"choices": [
"A",
"B"
]
},
{
"type": "panel",
"name": "panel1",
"elements": [
{
"type": "text",
"name": "q2",
"defaultValueExpression": "iif({q1} = 'A', 42, iif({q1} = 'B', 24, 0))"
}
],
"visibleIf": "{q1} anyof ['other', 'A', 'B']"
}
],
"clearInvisibleValues": "onHiddenContainer"
});
const q1 = survey.getQuestionByName("q1");
const q2 = survey.getQuestionByName("q2");
assert.equal(q2.isEmpty(), true, "initial value on loading and clear on becoming invisible");
q1.value = "A";
assert.equal(q2.value, 42, "q1.value = A");
q1.value = "B";
assert.equal(q2.value, 24, "q1.value = B");
});

0 comments on commit e099c25

Please sign in to comment.