Skip to content

Commit

Permalink
Checkboxes - The defaultValue/setValueExpression do not work when val…
Browse files Browse the repository at this point in the history
…uePropertyName is defined fix #8973
  • Loading branch information
andrewtelnov committed Oct 25, 2024
1 parent fe6e7f2 commit c094d88
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
16 changes: 16 additions & 0 deletions packages/survey-core/src/question_checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
69 changes: 68 additions & 1 deletion packages/survey-core/tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ QUnit.test("checkbox:readonly:clickItemHandler", (assert) => {
});
const q = <QuestionCheckboxModel>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({
Expand Down Expand Up @@ -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 = <QuestionCheckboxModel>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 = <QuestionCheckboxModel>survey.getQuestionByName("q1");
const q2 = <QuestionCheckboxModel>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 = <QuestionCheckboxModel>survey.getQuestionByName("q1");
const q2 = <QuestionCheckboxModel>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: [
Expand Down

0 comments on commit c094d88

Please sign in to comment.