Skip to content

Commit

Permalink
Issues when using question valueName/valuePropertyName binding and th…
Browse files Browse the repository at this point in the history
…e Other option when storeOthersAsComment is true fix #9097 (#9099)
  • Loading branch information
andrewtelnov authored Nov 25, 2024
1 parent 375152c commit 6bf0edc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
21 changes: 20 additions & 1 deletion packages/survey-core/src/question_checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
}
return false;
}
protected hasUnknownValueItem(val: any, includeOther: boolean = false,
isFilteredChoices: boolean = true, checkEmptyValue: boolean = false): boolean {
const propName = this.valuePropertyName;
if(!!propName && typeof val === "object" && val[propName] !== undefined) {
val = val[propName];
}
return super.hasUnknownValueItem(val, includeOther, isFilteredChoices, checkEmptyValue);
}
protected convertFuncValuetoQuestionValue(val: any): any {
if(!!this.valuePropertyName && Array.isArray(val) && val.length > 0) {
const res: Array<any> = [];
Expand Down Expand Up @@ -413,10 +421,21 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
if (ind < 0) return "";
return newValue[ind];
}
public getStoreOthersAsComment(): boolean {
if(!!this.valuePropertyName) return false;
return super.getStoreOthersAsComment();
}
protected setOtherValueIntoValue(newValue: any): any {
var ind = this.getFirstUnknownIndex(newValue);
if (ind < 0) return newValue;
newValue.splice(ind, 1, this.otherItem.value);
let otherVal: any = this.otherItem.value;
const propName = this.valuePropertyName;
if(propName) {
const obj: any = {};
obj[propName] = otherVal;
otherVal = obj;
}
newValue.splice(ind, 1, otherVal);
return newValue;
}
private getFirstUnknownIndex(newValue: any): number {
Expand Down
10 changes: 3 additions & 7 deletions packages/survey-core/src/question_tagbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,9 @@ export class QuestionTagboxModel extends QuestionCheckboxModel {
this.onOpened.fire(this, { question: this, choices: this.choices });
}

protected hasUnknownValue(
val: any,
includeOther: boolean,
isFilteredChoices: boolean,
checkEmptyValue: boolean
): boolean {
if(this.choicesLazyLoadEnabled) { return false; }
protected hasUnknownValue(val: any, includeOther: boolean = false,
isFilteredChoices: boolean = true, checkEmptyValue: boolean = false): boolean {
if(this.choicesLazyLoadEnabled) return false;
return super.hasUnknownValue(val, includeOther, isFilteredChoices, checkEmptyValue);
}
protected needConvertRenderedOtherToDataValue(): boolean {
Expand Down
36 changes: 35 additions & 1 deletion packages/survey-core/tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,11 @@ QUnit.test("checkbox vs valuePropertyName, check hasOther", (assert) => {
]
});
const q = <QuestionCheckboxModel>survey.getQuestionByName("q1");
assert.equal(q.getStoreOthersAsComment(), false, "It becomes false because of valuePropertyName");
q.renderedValue = ["other"];
assert.deepEqual(q.value, [{ fruit: "other" }], "#1");
q.comment = "text1";
assert.deepEqual(survey.data, { q1: [{ fruit: "other" }], "q1-Comment": "text1" }, "2");
assert.deepEqual(survey.data, { q1: [{ fruit: "text1" }] }, "2");
});
QUnit.test("checkbox vs valuePropertyName, getDisplayValue", (assert) => {
const survey = new SurveyModel({
Expand Down Expand Up @@ -720,6 +721,39 @@ QUnit.test("checkbox vs valuePropertyName, check hasOther vs storeOthersAsCommen
assert.deepEqual(q.value, [{ fruit: "text1" }], "#2");
assert.deepEqual(survey.data, { q1: [{ fruit: "text1" }] }, "#3");
});
QUnit.test("checkbox vs valuePropertyName, check hasOther vs storeOthersAsComment & matrix", (assert) => {
const survey = new SurveyModel({
elements: [
{
type: "checkbox",
name: "q1",
choices: ["apple", "banana", "orange"],
valuePropertyName: "fruit",
hasOther: true
},
{
type: "matrixdynamic",
name: "q2",
valueName: "q1",
columns: [
{ cellType: "text", name: "col1" },
{ cellType: "expression", name: "col2", expression: "{row.fruit}" }
]
}
]
});
const q = <QuestionCheckboxModel>survey.getQuestionByName("q1");
assert.equal(q.getStoreOthersAsComment(), false, "it is false valuePropertyName is not empty");
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("q2");
q.renderedValue = ["apple", "other"];
const rows = matrix.visibleRows;
assert.equal(rows.length, 2, "matrix rows");
q.comment = "text1";
assert.equal(rows.length, 2, "matrix rows");
assert.equal(rows[0].cells[1].question.value, "apple", "rows[0]");
assert.equal(rows[1].cells[1].question.value, "text1", "rows[1]");
assert.deepEqual(survey.data, { q1: [{ fruit: "apple", col2: "apple" }, { fruit: "text1", col2: "text1" }] }, "survey.data");
});
QUnit.test("checkbox vs valuePropertyName, use in expression", (assert) => {
const survey = new SurveyModel({
storeOthersAsComment: false,
Expand Down

0 comments on commit 6bf0edc

Please sign in to comment.