Skip to content

Commit

Permalink
The visibleIf condition doesn't perform correctly when answering spec…
Browse files Browse the repository at this point in the history
…ific matrix questions fix #7396 (#7403)
  • Loading branch information
andrewtelnov authored Nov 22, 2023
1 parent ae7b955 commit e94b7f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/conditionProcessValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,20 @@ export class ProcessValue {
return res;
}
private getNonNestedObject(obj: any, text: string, createPath: boolean): any {
var curName = this.getFirstPropertyName(text, obj, createPath);
const checkedKeys = new Array<string>();
let len = 0;
let res = this.getNonNestedObjectCore(obj, text, createPath, checkedKeys);
while(!res && len < checkedKeys.length) {
len = checkedKeys.length;
res = this.getNonNestedObjectCore(obj, text, createPath, checkedKeys);
}
return res;
}
private getNonNestedObjectCore(obj: any, text: string, createPath: boolean, checkedKeys: Array<string>): any {
var curName = this.getFirstPropertyName(text, obj, createPath, checkedKeys);
if(!!curName) {
checkedKeys.push(curName);
}
var path = !!curName ? [curName] : null;
while (text != curName && !!obj) {
var isArray = text[0] == "[";
Expand All @@ -170,7 +183,7 @@ export class ProcessValue {
if (!!text && text[0] == ".") {
text = text.substring(1);
}
curName = this.getFirstPropertyName(text, obj, createPath);
curName = this.getFirstPropertyName(text, obj, createPath, checkedKeys);
if (!!curName) {
path.push(curName);
}
Expand All @@ -190,18 +203,15 @@ export class ProcessValue {
if (index < 0 || index >= curValue.length) return null;
return { value: curValue[index], text: text, index: index };
}
private getFirstPropertyName(
name: string,
obj: any,
createProp: boolean = false
): string {
private getFirstPropertyName(name: string, obj: any, createProp: boolean = false, checkedKeys: Array<string> = undefined): string {
if (!name) return name;
if (!obj) obj = {};
if (obj.hasOwnProperty(name)) return name;
var nameInLow = name.toLowerCase();
var A = nameInLow[0];
var a = A.toUpperCase();
for (var key in obj) {
if(Array.isArray(checkedKeys) && checkedKeys.indexOf(key) > -1) continue;
var first = key[0];
if (first === a || first === A) {
var keyName = key.toLowerCase();
Expand Down
32 changes: 31 additions & 1 deletion tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18183,4 +18183,34 @@ QUnit.test("Bug on loading json with collapsed panel. It was fixed in v1.9.117,
panel.expand();
assert.equal(panel.isCollapsed, false, "panel is not collapsed");
});

QUnit.test("Expression bug with complex path, #7396", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "matrix",
name: "Q1.1",
columns: [1, 2],
rows: [1, 2, 3]
},
{
type: "matrix",
name: "Q1.1.16",
columns: [1, 2, 3],
rows: ["16"]
},
{
type: "text",
name: "Q1.1.16.A",
visibleIf: "{Q1.1.16.16} anyof [1, 2, 3]"
}
]
});
const q1 = survey.getQuestionByName("Q1.1");
const q2 = survey.getQuestionByName("Q1.1.16");
const q3 = survey.getQuestionByName("Q1.1.16.A");
assert.equal(q3.isVisible, false, "visible #1");
q1.value = { "1": 1, "2": 2 };
assert.equal(q3.isVisible, false, "visible #2");
q2.value = { "16": 2 };
assert.equal(q3.isVisible, true, "visible #3");
});
2 changes: 1 addition & 1 deletion tests/textPreprocessorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ QUnit.test("ProcessValue setValue function - create path", function (assert) {
processor.setValue(data, "a[0].name", 1);
assert.deepEqual(
data,
{ a: { b: 1 }, b: 1, c: { a: { b: 2 } } },
{ a: { b: 1 }, "a[0]": { "name": 1 }, b: 1, c: { a: { b: 2 } } },
"create new object"
);
data = { a: { Item1: { c1: 0 }, Item3: { c1: 1 } } };
Expand Down

0 comments on commit e94b7f2

Please sign in to comment.