Skip to content

Commit

Permalink
Fix for survey.onQuestionRemoved
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Nov 19, 2024
1 parent fa3fa3c commit 6f19042
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
9 changes: 6 additions & 3 deletions packages/survey-core/src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1460,9 +1460,12 @@ export class PanelModelBase extends SurveyElement<Question>
}
}
}
private canFireAddRemoveNotifications(element: IElement): boolean {
return !!this.survey && (<any>element).prevSurvey !== this.survey;
}
protected onAddElement(element: IElement, index: number): void {
const survey = this.survey;
const fireNotification = !!this.survey && (<any>element).prevSurvey !== this.survey;
const fireNotification = this.canFireAddRemoveNotifications(element);
element.setSurveyImpl(this.surveyImpl);
element.parent = this;
this.markQuestionListDirty();
Expand All @@ -1488,7 +1491,7 @@ export class PanelModelBase extends SurveyElement<Question>
}, this.id);
this.onElementVisibilityChanged(this);
}
protected onRemoveElement(element: IElement) {
protected onRemoveElement(element: IElement): void {
element.parent = null;
this.markQuestionListDirty();
(<Base>(<any>element)).unregisterPropertyChangedHandlers(["visible", "isVisible", "startWithNewLine"], this.id);
Expand All @@ -1499,7 +1502,7 @@ export class PanelModelBase extends SurveyElement<Question>
this.onElementVisibilityChanged(this);
}
private onRemoveElementNotifySurvey(element: IElement): void {
if(!this.survey) return;
if(!this.canFireAddRemoveNotifications(element)) return;
if (!element.isPanel) {
this.survey.questionRemoved(<Question>element);
} else {
Expand Down
17 changes: 12 additions & 5 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6f19042

Please sign in to comment.