From c094d886030522599814ebcf577cf19be37079cc Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Fri, 25 Oct 2024 16:50:54 +0300 Subject: [PATCH] Checkboxes - The defaultValue/setValueExpression do not work when valuePropertyName is defined fix #8973 --- packages/survey-core/src/question_checkbox.ts | 16 +++++ .../tests/question_baseselecttests.ts | 69 ++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/survey-core/src/question_checkbox.ts b/packages/survey-core/src/question_checkbox.ts index 1c2d18b58c..88d54bdfa3 100644 --- a/packages/survey-core/src/question_checkbox.ts +++ b/packages/survey-core/src/question_checkbox.ts @@ -193,6 +193,22 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase { } return false; } + protected convertFuncValuetoQuestionValue(val: any): any { + if(!!this.valuePropertyName && Array.isArray(val) && val.length > 0) { + const res = []; + val.forEach(item => { + const isObj = typeof item === "object"; + let obj: any = isObj ? item : {}; + if(!isObj) { + obj[this.valuePropertyName] = item; + } + res.push(obj); + }); + val = res; + } + return super.convertDefaultValue(val); + + } private getRealValue(val: any): any { if (!val) return val; return !this.valuePropertyName ? val : val[this.valuePropertyName]; diff --git a/packages/survey-core/tests/question_baseselecttests.ts b/packages/survey-core/tests/question_baseselecttests.ts index 2994f1a1ea..0fad903b26 100644 --- a/packages/survey-core/tests/question_baseselecttests.ts +++ b/packages/survey-core/tests/question_baseselecttests.ts @@ -663,7 +663,7 @@ QUnit.test("checkbox:readonly:clickItemHandler", (assert) => { }); const q = survey.getQuestionByName("q1"); q.clickItemHandler(q.choices[2], true); - assert.deepEqual(q.value, ["banana"], "nothing changed"); + assert.deepEqual(q.value, [{ fruit: "banana" }], "nothing changed"); }); QUnit.test("checkbox vs valuePropertyName, check hasOther", (assert) => { const survey = new SurveyModel({ @@ -751,6 +751,73 @@ QUnit.test("checkbox vs valuePropertyName, use in expression", (assert) => { assert.equal(q2.isVisible, false, "#3"); }); +QUnit.test("checkbox vs valuePropertyName & defaultValue, Bug#8973", (assert) => { + const survey = new SurveyModel({ + storeOthersAsComment: false, + elements: [ + { + type: "checkbox", + name: "q1", + choices: ["apple", "banana", "orange"], + valuePropertyName: "fruit", + defaultValue: ["apple", "orange"] + } + ] + }); + const q1 = survey.getQuestionByName("q1"); + q1.renderedValue = ["apple", "orange"]; + assert.deepEqual(q1.value, [{ fruit: "apple" }, { fruit: "orange" }], "q1.value"); +}); +QUnit.test("checkbox vs valuePropertyName & defaultValueExpression, Bug#8973", (assert) => { + const survey = new SurveyModel({ + elements: [ + { + type: "checkbox", + name: "q1", + choices: ["apple", "banana", "orange"], + valuePropertyName: "fruit", + defaultValueExpression: "{q2}" + }, + { + type: "checkbox", + name: "q2", + choices: ["apple", "banana", "orange"] + } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + q2.renderedValue = ["apple", "orange"]; + assert.deepEqual(q2.value, ["apple", "orange"], "q2.value"); + assert.deepEqual(q1.value, [{ fruit: "apple" }, { fruit: "orange" }], "q1.value"); +}); +QUnit.test("checkbox vs valuePropertyName & setValueExpression, Bug#8973", (assert) => { + const survey = new SurveyModel({ + elements: [ + { + type: "checkbox", + name: "q1", + choices: ["apple", "banana", "orange"], + valuePropertyName: "fruit", + setValueExpression: "{q2}" + }, + { + type: "checkbox", + name: "q2", + choices: ["apple", "banana", "orange"] + } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + q2.renderedValue = ["apple", "orange"]; + assert.deepEqual(q2.value, ["apple", "orange"], "q2.value, #1"); + assert.deepEqual(q1.value, [{ fruit: "apple" }, { fruit: "orange" }], "q1.value, #2"); + q2.renderedValue = ["banana", "orange"]; + assert.deepEqual(q2.value, ["banana", "orange"], "q2.value, #2"); + assert.deepEqual(q1.value, [{ fruit: "banana" }, { fruit: "orange" }], "q1.value, #2"); +}); + QUnit.test("check radiogroup title actions", (assert) => { let survey = new SurveyModel({ questions: [