diff --git a/packages/survey-core/src/panel.ts b/packages/survey-core/src/panel.ts index 94ab39605c..f50de69d30 100644 --- a/packages/survey-core/src/panel.ts +++ b/packages/survey-core/src/panel.ts @@ -1460,9 +1460,12 @@ export class PanelModelBase extends SurveyElement } } } + private canFireAddRemoveNotifications(element: IElement): boolean { + return !!this.survey && (element).prevSurvey !== this.survey; + } protected onAddElement(element: IElement, index: number): void { const survey = this.survey; - const fireNotification = !!this.survey && (element).prevSurvey !== this.survey; + const fireNotification = this.canFireAddRemoveNotifications(element); element.setSurveyImpl(this.surveyImpl); element.parent = this; this.markQuestionListDirty(); @@ -1488,7 +1491,7 @@ export class PanelModelBase extends SurveyElement }, this.id); this.onElementVisibilityChanged(this); } - protected onRemoveElement(element: IElement) { + protected onRemoveElement(element: IElement): void { element.parent = null; this.markQuestionListDirty(); ((element)).unregisterPropertyChangedHandlers(["visible", "isVisible", "startWithNewLine"], this.id); @@ -1499,7 +1502,7 @@ export class PanelModelBase extends SurveyElement this.onElementVisibilityChanged(this); } private onRemoveElementNotifySurvey(element: IElement): void { - if(!this.survey) return; + if(!this.canFireAddRemoveNotifications(element)) return; if (!element.isPanel) { this.survey.questionRemoved(element); } else { diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 2f43ea388e..51d7168364 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -16181,17 +16181,24 @@ QUnit.test("onQuestionAdded & changing parent", function (assert) { } ] }); - var counter = 0; + let addedCounter = 0; + let removedCounter = 0; survey.onQuestionAdded.add((sender, options) => { - counter++; + addedCounter++; + }); + survey.onQuestionRemoved.add((sender, options) => { + removedCounter++; }); - assert.equal(counter, 0, "onQuestionAdded is not fired, #1"); + assert.equal(addedCounter, 0, "onQuestionAdded is not fired, #1"); survey.getQuestionByName("q1").page = survey.pages[1]; survey.getQuestionByName("q3").page = survey.pages[0]; - assert.equal(counter, 0, "onQuestionAdded is not fired, #2"); + assert.equal(addedCounter, 0, "onQuestionAdded is not fired, #2"); const q = new QuestionTextModel("q5"); q.page = survey.pages[1]; - assert.equal(counter, 1, "onQuestionAdded is fired for q5, #3"); + assert.equal(addedCounter, 1, "onQuestionAdded is fired for q5, #3"); + assert.equal(removedCounter, 0, "onQuestionRemoved #1"); + q.delete(); + assert.equal(removedCounter, 1, "onQuestionRemoved #2"); }); QUnit.test("Set values into radiogroup and checkbox questions before creating them", function (assert) { const survey = new SurveyModel();