From 07ce170f1e2024c230f49b8a8e6c3fc8504fae43 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Mon, 20 May 2024 14:07:56 +0300 Subject: [PATCH] The survey.onValueChanging event is not raised when updating a matrix comment value fix #8292 --- src/survey.ts | 11 ++++++-- tests/surveytests.ts | 65 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/survey.ts b/src/survey.ts index 9e9ce6ba3c..c67880f003 100644 --- a/src/survey.ts +++ b/src/survey.ts @@ -5623,7 +5623,7 @@ export class SurveyModel extends SurveyElementCore page.name = name; return page; } - protected questionOnValueChanging(valueName: string, newValue: any): any { + protected questionOnValueChanging(valueName: string, newValue: any, questionValueName?: string): any { if (!!this.editingObj) { const prop = Serializer.findProperty(this.editingObj.getType(), valueName); if (!!prop) newValue = prop.settingValue(this.editingObj, newValue); @@ -5631,7 +5631,7 @@ export class SurveyModel extends SurveyElementCore if (this.onValueChanging.isEmpty) return newValue; var options = { name: valueName, - question: this.getQuestionByValueName(valueName), + question: this.getQuestionByValueName(questionValueName || valueName), value: this.getUnbindValue(newValue), oldValue: this.getValue(valueName), }; @@ -6600,7 +6600,8 @@ export class SurveyModel extends SurveyElementCore public setComment(name: string, newValue: string, locNotification: any = false): void { if (!newValue) newValue = ""; if (this.isTwoValueEquals(newValue, this.getComment(name))) return; - var commentName = name + this.commentSuffix; + const commentName = name + this.commentSuffix; + newValue = this.questionOnValueChanging(commentName, newValue, name); if (this.isValueEmpty(newValue)) { this.deleteDataValueCore(this.valuesHash, commentName); } else { @@ -6626,6 +6627,10 @@ export class SurveyModel extends SurveyElementCore question: question, value: newValue, }); + question.comment = newValue; + if(question.comment != newValue) { + question.comment = newValue; + } } } /** diff --git a/tests/surveytests.ts b/tests/surveytests.ts index ca5ec0b7e6..71679a003a 100644 --- a/tests/surveytests.ts +++ b/tests/surveytests.ts @@ -1881,7 +1881,70 @@ QUnit.test("onValueChanging event", function (assert) { "onValueChanging event allows to change value" ); }); -QUnit.test("onValueChanging event - do not allow clear value, #1542", function ( +QUnit.test("onValueChanging event for comment, #8292", function ( + assert +) { + const survey = new SurveyModel(); + const page = survey.addNewPage("page"); + const q1 = page.addNewQuestion("text", "q1"); + q1.showCommentArea = true; + let oldComment, newComment, questionName; + survey.onValueChanging.add((sender, options) => { + if (options.name == "q1-Comment") { + questionName = options.question.name; + oldComment = options.oldValue; + newComment = options.value; + } + }); + q1.comment = "abc"; + assert.equal(questionName, "q1", "question name #1"); + assert.equal(q1.comment, "abc", "comment value #1"); + assert.equal(newComment, "abc", "newComment #1"); + q1.comment = "abcd"; + assert.equal(q1.comment, "abcd", "comment value #2"); + assert.equal(newComment, "abcd", "newComment #2"); + assert.equal(oldComment, "abc", "newComment #2"); +}); + +QUnit.test("onValueChanging event - do not allow clear value, #8292", function ( + assert +) { + const survey = new SurveyModel(); + const page = survey.addNewPage("page"); + const q1 = page.addNewQuestion("text", "q1"); + q1.showCommentArea = true; + let counter = 0; + let oldComment, newComment, questionName; + survey.onValueChanging.add((sender, options) => { + if (options.name === "q1-Comment") { + counter ++; + questionName = options.question.name; + if(!options.value) { + options.value = options.oldValue; + } + oldComment = options.oldValue; + newComment = options.value; + } + }); + let onValueChangedValue; + survey.onValueChanged.add((sender, options) => { + if (options.name === "q1-Comment") { + onValueChangedValue = options.value; + } + }); + q1.comment = "abc"; + assert.equal(counter, 1, "counter #1"); + assert.equal(q1.comment, "abc", "comment value #1"); + q1.comment = ""; + assert.equal(questionName, "q1", "question.name #2"); + assert.equal(oldComment, "abc", "oldComment #2"); + assert.equal(newComment, "abc", "newComment #2"); + assert.equal(counter, 2, "counter #2"); + assert.equal(q1.comment, "abc", "comment value #2"); + assert.equal(survey.getComment("q1"), "abc", "survey.getComment value #2"); + assert.equal(onValueChangedValue, "abc", "onValueChanged options.value"); +}); +QUnit.test("onValueChanging event - do not allow clear value in comment, #1542", function ( assert ) { var survey = new SurveyModel();