Skip to content

Commit

Permalink
Checkboxes Question: Choices selected are not reflected in another fi…
Browse files Browse the repository at this point in the history
…eld using Default Value Expression Property fix #6886 (#6938)
  • Loading branch information
andrewtelnov committed Sep 11, 2023
1 parent 8f48ba0 commit 926eda6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2050,12 +2050,9 @@ export class Question extends SurveyElement<Question>
protected canSetValueToSurvey(): boolean {
return true;
}
protected valueFromData(val: any): any {
return val;
}
protected valueToData(val: any): any {
return val;
}
protected valueFromData(val: any): any { return val; }
protected valueToData(val: any): any { return val; }
protected convertToCorrectValue(val: any): any { return val; }
protected onValueChanged(): void { }
protected isMouseDown: boolean;
onMouseDown(): void {
Expand Down Expand Up @@ -2096,6 +2093,7 @@ export class Question extends SurveyElement<Question>
this.isValueChangedDirectly = true;
}
protected setQuestionValue(newValue: any, updateIsAnswered: boolean = true): void {
newValue = this.convertToCorrectValue(newValue);
const isEqual = this.isTwoValueEquals(this.questionValue, newValue);
if (!isEqual && !this.isChangingViaDefaultValue) {
this.setValueChangedDirectly();
Expand Down
1 change: 1 addition & 0 deletions src/question_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class QuestionCommentModel extends QuestionTextBase {
}
super.setNewValue(newValue);
}
protected getValueSeparator(): string { return "\n"; }
public get className() {
return (this.cssClasses ? this.getControlClass() : "panel-comment-root") || undefined;
}
Expand Down
4 changes: 4 additions & 0 deletions src/question_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ export class QuestionExpressionModel extends Question {
}
return val.toString();
}
protected convertToCorrectValue(val: any): any {
if(Array.isArray(val)) return val.join(", ");
return val;
}
}

export function getCurrecyCodes(): Array<string> {
Expand Down
5 changes: 5 additions & 0 deletions src/question_textbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export class QuestionTextBase extends Question {
super.setQuestionValue(newValue, updateIsAnswered);
this.updateRemainingCharacterCounter(newValue);
}
protected convertToCorrectValue(val: any): any {
if(Array.isArray(val)) return val.join(this.getValueSeparator());
return val;
}
protected getValueSeparator(): string { return ", "; }
public disableNativeUndoRedo = false;
protected checkForUndo(event: KeyboardEvent) {
if (this.disableNativeUndoRedo && this.isInputTextUpdate && (event.ctrlKey || event.metaKey)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/surveyquestiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7156,4 +7156,22 @@ QUnit.test("Dynamic error text in expression validator, bug#6790", function (ass
assert.equal(error.locText.renderedHtml, "80% left.", "Old error text is correct, #2");
assert.strictEqual(error, q2.errors[0], "Same errors");
assert.equal(errorTextChangedCounter, 1, "text has been updated");
});
QUnit.test("Set array and convert it to a string, bug#6886", function (assert) {
const survey = new SurveyModel({
elements: [
{ type: "text", name: "q1" },
{ type: "comment", name: "q2" },
{ type: "expression", name: "q3" }
]
});
const q1 = survey.getQuestionByName("q1");
const q2 = survey.getQuestionByName("q2");
const q3 = survey.getQuestionByName("q3");
q1.value = ["item1", "item2", "item3"];
q2.value = ["item1", "item2", "item3"];
q3.value = ["item1", "item2", "item3"];
assert.equal(q1.value, "item1, item2, item3", "q1");
assert.equal(q2.value, "item1\nitem2\nitem3", "q2");
assert.equal(q3.value, "item1, item2, item3", "q3");
});

0 comments on commit 926eda6

Please sign in to comment.