From 94123cc8caa05e6eeb46f65d5e66b2ec2ee45e43 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Fri, 10 Nov 2023 17:53:21 +0200 Subject: [PATCH] Matrix dynamic with editing object doesn't work correctly on adding a new item in collection fix #7328 --- src/question_matrixdynamic.ts | 2 +- tests/editingObjectTests.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/question_matrixdynamic.ts b/src/question_matrixdynamic.ts index 8dca2277bd..0194a8899a 100644 --- a/src/question_matrixdynamic.ts +++ b/src/question_matrixdynamic.ts @@ -787,7 +787,7 @@ export class QuestionMatrixDynamicModel extends QuestionMatrixDropdownModelBase const lastDelRow = this.lastDeletedRow; this.lastDeletedRow = undefined; const rows = this.generatedVisibleRows; - if(!Array.isArray(val) || Math.abs(rows.length - val.length) > 1) return false; + if(!Array.isArray(val) || Math.abs(rows.length - val.length) > 1 || rows.length === val.length) return false; const index = this.getInsertedDeletedIndex(rows, val); if(rows.length > val.length) { this.lastDeletedRow = rows[index]; diff --git a/tests/editingObjectTests.ts b/tests/editingObjectTests.ts index 4418cb9616..121aded199 100644 --- a/tests/editingObjectTests.ts +++ b/tests/editingObjectTests.ts @@ -16,6 +16,7 @@ import { QuestionMultipleTextModel } from "../src/question_multipletext"; import { QuestionMatrixModel } from "../src/question_matrix"; import { Question } from "../src/question"; import { QuestionCheckboxModel } from "../src/question_checkbox"; +import { SurveyTriggerComplete } from "../src/trigger"; export default QUnit.module("Survey.editingObj Tests"); @@ -1472,3 +1473,31 @@ QUnit.test("Allow to set empty string into localization string & string property assert.strictEqual(q2.placeholder, "", "q2.placeholder value is empty"); assert.strictEqual(q2.minWidth, "", "q2.minWidth value is empty"); }); +QUnit.test("Edit triggers array", function (assert) { + const survey = new SurveyModel(); + + const editSurvey = new SurveyModel({ + elements: [ + { + type: "matrixdynamic", + name: "triggers", + columns: [{ cellType: "text", name: "type" }], + rowCount: 0, + }, + ], + }); + editSurvey.onMatrixCellCreated.add((sender, options) => { + const obj = options.row.editingObj; + if(obj) { + options.cell.value = obj.getType(); + } + }); + const matrix = ( + editSurvey.getQuestionByName("triggers") + ); + editSurvey.editingObj = survey; + assert.equal(matrix.visibleRows.length, 0, "There is no triggers"); + survey.triggers.push(new SurveyTriggerComplete()); + assert.equal(matrix.visibleRows.length, 1, "There is one trigger"); + assert.equal(matrix.visibleRows[0].cells[0].value, "completetrigger", "correct trigger type"); +});