Skip to content

Commit

Permalink
Merge pull request #8337 from surveyjs/bug/8333-boolean-update-from-s…
Browse files Browse the repository at this point in the history
…urvey

Fixed #8333 - Boolean Question appers incorrectly
  • Loading branch information
andrewtelnov authored May 31, 2024
2 parents 6d85696 + 959ff7d commit f775389
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/question_boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class QuestionBooleanModel extends Question {
this.value = val == true ? this.getValueTrue() : this.getValueFalse();
this.booleanValueRendered = val;
}
this.updateThumbMargin();
}
public get defaultValue(): any {
return this.getPropertyValue("defaultValue");
Expand Down Expand Up @@ -255,6 +254,16 @@ export class QuestionBooleanModel extends Question {
.toString();
}

updateValueFromSurvey(newValue: any, clearData: boolean = false): void {
super.updateValueFromSurvey(newValue, clearData);
this.updateThumbMargin();
}

protected onValueChanged(): void {
super.onValueChanged();
this.updateThumbMargin();
}

public get svgIcon(): string {
if (this.booleanValue && this.cssClasses.svgIconCheckedId) return this.cssClasses.svgIconCheckedId;
if (!this.isDeterminated && this.cssClasses.svgIconIndId) return this.cssClasses.svgIconIndId;
Expand Down
42 changes: 41 additions & 1 deletion tests/questionBooleanTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SurveyModel } from "../src/survey";

import { QuestionBooleanModel } from "../src/question_boolean";
import { QuestionRadiogroupModel } from "../src/question_radiogroup";
import { defaultV2Css } from "../src/defaultCss/defaultV2Css";
import { QuestionMatrixDynamicModel } from "../src/question_matrixdynamic";

Expand Down Expand Up @@ -279,4 +280,43 @@ QUnit.test("Boolean in matrix dynamic", function (assert) {
const q = <QuestionMatrixDynamicModel>survey.getAllQuestions()[0];
q.visibleRows[0].cells[0].value = "abc";
assert.deepEqual(q.value, [{ col1: "abc" }], "there is not boolean value");
});
});

QUnit.test("Invoke updateThumbMargin on value changed", function (assert) {
var survey = new SurveyModel({
elements: [
{
"type": "boolean",
"name": "question1",
"valueTrue": "yes",
"valueFalse": "no"
},
{
"type": "radiogroup",
"name": "question1",
"choices": [
{
"value": "yes",
"text": "Yes"
},
{
"value": "no",
"text": "No"
}
]
}
]
});
const q = <QuestionBooleanModel>survey.getAllQuestions()[0];
const radio = <QuestionRadiogroupModel>survey.getAllQuestions()[1];
const c = { counter: 0 };
q.updateThumbMargin = () => c.counter++;

assert.equal(c.counter, 0, "Initial");
q.value = "yes";
assert.equal(c.counter, 1, "Set value to question");
radio.value = "no";
assert.equal(c.counter, 2, "Set value to another question");
survey.data = { question1: "yes" };
assert.equal(c.counter, 4, "Set value to survey");
});

0 comments on commit f775389

Please sign in to comment.