Skip to content

Commit 68b55b4

Browse files
authored
The other value and comment in a Composite Question stores incorrectly fix #9378 (#9389)
1 parent 412e930 commit 68b55b4

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

packages/survey-core/src/question_custom.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { LocalizableString } from "./localizablestring";
2222
import { SurveyError } from "./survey-error";
2323
import { CustomError } from "./error";
2424
import { ConsoleWarnings } from "./console-warnings";
25+
import { settings } from "./settings";
2526

2627
/**
2728
* An interface used to create custom question types.
@@ -1244,6 +1245,31 @@ export class QuestionCompositeModel extends QuestionCustomModelBase {
12441245
this.settingNewValue = false;
12451246
this.runPanelTriggers(QuestionCompositeModel.ItemVariableName + "." + name, newValue);
12461247
}
1248+
setComment(name: string, newValue: string, locNotification: any): any {
1249+
let val = this.getUnbindValue(this.value);
1250+
const commentName = this.getCommentName(name);
1251+
if(!val && !newValue || !!newValue && !!val && val[commentName] === newValue) return;
1252+
if(!!newValue) {
1253+
if(!val) { val = {}; }
1254+
val[commentName] = newValue;
1255+
} else {
1256+
delete val[commentName];
1257+
}
1258+
const q = <Question>this.getQuestionByName(name);
1259+
if(!!q && q.comment !== newValue) {
1260+
q.comment = newValue;
1261+
}
1262+
this.value = val;
1263+
}
1264+
getComment(name: string): string {
1265+
const q = <Question>this.getQuestionByName(name);
1266+
if(!!q) return q.comment;
1267+
const val = this.value;
1268+
return !!val && val[this.getCommentName(name)] || "";
1269+
}
1270+
private getCommentName(name: string): string {
1271+
return name + settings.commentSuffix;
1272+
}
12471273
private runPanelTriggers(name: string, value: any): void {
12481274
if(!!this.contentPanel) {
12491275
this.contentPanel.questions.forEach(q => {
@@ -1321,18 +1347,23 @@ export class QuestionCompositeModel extends QuestionCustomModelBase {
13211347
super.setQuestionValue(newValue, updateIsAnswered);
13221348
}
13231349
private setValuesIntoQuestions(newValue: any): void {
1324-
if(!this.contentPanel) return;
1350+
if(!this.contentPanel || this.settingNewValue) return;
13251351
newValue = this.getValueForContentPanel(newValue);
13261352
const oldSettingNewValue = this.settingNewValue;
13271353
this.settingNewValue = true;
13281354
const questions = this.contentPanel.questions;
13291355
for (var i = 0; i < questions.length; i++) {
1330-
const key = questions[i].getValueName();
1331-
const val = !!newValue ? newValue[key] : undefined;
13321356
const q = questions[i];
1357+
const key = q.getValueName();
1358+
const commentKey = this.getCommentName(key);
1359+
const val = !!newValue ? newValue[key] : undefined;
1360+
const commentVal = !!newValue && newValue[commentKey] || "";
13331361
if(!this.isTwoValueEquals(q.value, val) && (val !== undefined || !q.isEmpty())) {
13341362
q.value = val;
13351363
}
1364+
if(q.comment !== commentVal) {
1365+
q.comment = commentVal;
1366+
}
13361367
}
13371368
this.settingNewValue = oldSettingNewValue;
13381369
}

packages/survey-core/tests/question_customtests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,5 +3462,32 @@ QUnit.test("Composite: clearIfInvisible='onHidden'", function (assert) {
34623462
q1.contentPanel.getQuestionByName("q2").value = 3;
34633463
assert.deepEqual(q1.value, { q1: "test1", q2: 3 }, "test #1");
34643464

3465+
ComponentCollection.Instance.clear();
3466+
});
3467+
QUnit.test("Composite: with dropdown & showOtherItem, Bug#9378", function (assert) {
3468+
ComponentCollection.Instance.add({
3469+
name: "test",
3470+
elementsJSON: [
3471+
{ type: "text", name: "q1" },
3472+
{ type: "dropdown", name: "q2", choices: [1, 2, 3], showOtherItem: true }
3473+
]
3474+
});
3475+
const survey = new SurveyModel({
3476+
elements: [
3477+
{ type: "test", name: "question1" }
3478+
]
3479+
});
3480+
const q = <QuestionCompositeModel>survey.getQuestionByName("question1");
3481+
const q1 = q.contentPanel.getQuestionByName("q1");
3482+
const q2 = q.contentPanel.getQuestionByName("q2");
3483+
q1.value = "test1";
3484+
q2.value = "other";
3485+
q2.comment = "abc";
3486+
assert.deepEqual(q.value, { q1: "test1", q2: "other", "q2-Comment": "abc" }, "q.value #1");
3487+
survey.data = {};
3488+
assert.ok(q.isEmpty(), "q.value #2");
3489+
survey.data = { question1: { q1: "test2", q2: "other", "q2-Comment": "def" } };
3490+
assert.deepEqual(q.value, { q1: "test2", q2: "other", "q2-Comment": "def" }, "q.value #3");
3491+
34653492
ComponentCollection.Instance.clear();
34663493
});

0 commit comments

Comments
 (0)