Skip to content

Commit

Permalink
ComponentCollection add "remove" function and "internal" boolean prop…
Browse files Browse the repository at this point in the history
…erty into interface fix #7300 (#7301)
  • Loading branch information
andrewtelnov committed Nov 8, 2023
1 parent 8f7bc6f commit da9981e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/question_custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface ICustomQuestionTypeConfiguration {
* [UI Icons](https://surveyjs.io/form-library/documentation/icons (linkStyle))
*/
iconName?: string;
internal?: boolean;
/**
* A function that is called when the custom question type is initialized. Use it to add, remove, or modify the type's properties (see [Override Base Question Properties](https://surveyjs.io/form-library/documentation/customize-question-types/create-composite-question-types#override-base-question-properties)).
*/
Expand Down Expand Up @@ -336,21 +337,36 @@ export class ComponentCollection {
this.onAddingJson(name, customQuestion.isComposite);
this.customQuestionValues.push(customQuestion);
}
public remove(componentName: string): boolean {
if(!componentName) return false;
const index = this.getCustomQuestionIndex(componentName.toLowerCase());
if(index < 0) return false;
this.removeByIndex(index);
return true;
}
public get items(): Array<ComponentQuestionJSON> {
return this.customQuestionValues;
}
public getCustomQuestionByName(name: string): ComponentQuestionJSON {
const index = this.getCustomQuestionIndex(name);
return index >= 0 ? this.customQuestionValues[index] : undefined;
}
private getCustomQuestionIndex(name: string): number {
for (var i = 0; i < this.customQuestionValues.length; i++) {
if (this.customQuestionValues[i].name == name)
return this.customQuestionValues[i];
if (this.customQuestionValues[i].name === name) return i;
}
return null;
return -1;
}
public clear() {
for (var i = 0; i < this.customQuestionValues.length; i++) {
Serializer.removeClass(this.customQuestionValues[i].name);
private removeByIndex(index: number): void {
Serializer.removeClass(this.customQuestionValues[index].name);
this.customQuestionValues.splice(index, 1);
}
public clear(includeInternal?: boolean): void {
for (let i = this.customQuestionValues.length - 1; i >= 0; i--) {
if(includeInternal || !this.customQuestionValues[i].json.internal) {
this.removeByIndex(i);
}
}
this.customQuestionValues = [];
}
public createQuestion(
name: string,
Expand Down
29 changes: 29 additions & 0 deletions tests/question_customtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2479,3 +2479,32 @@ QUnit.test("Single: Apply error css", function (assert) {
assert.equal(qText.getControlClass().indexOf(errorCss) > -1, true, "errors is here");
ComponentCollection.Instance.clear();
});
QUnit.test("ComponentCollection.Instance.remove", function (assert) {
ComponentCollection.Instance.add({
name: "newquestion",
questionJSON: { type: "text" },
});
assert.equal(ComponentCollection.Instance.getCustomQuestionByName("newquestion").name, "newquestion", "it exists");
assert.equal(ComponentCollection.Instance.remove("aaa"), false, "aaa is not exists");
assert.equal(ComponentCollection.Instance.remove("newquestion"), true, "newquestion is removed");
assert.notOk(ComponentCollection.Instance.getCustomQuestionByName("newquestion"), "newquestion is not here");
});
QUnit.test("internal boolean flag", function (assert) {
ComponentCollection.Instance.add({
name: "newquestion1",
internal: true,
questionJSON: { type: "text" },
});
ComponentCollection.Instance.add({
name: "newquestion2",
questionJSON: { type: "text" },
});
assert.equal(ComponentCollection.Instance.getCustomQuestionByName("newquestion1").name, "newquestion1", "newquestion1 is here");
assert.equal(ComponentCollection.Instance.getCustomQuestionByName("newquestion2").name, "newquestion2", "newquestion2 is here");
ComponentCollection.Instance.clear();
assert.equal(ComponentCollection.Instance.getCustomQuestionByName("newquestion1").name, "newquestion1", "newquestion1 is here");
assert.notOk(ComponentCollection.Instance.getCustomQuestionByName("newquestion2"), "newquestion2 is not here, #1");
ComponentCollection.Instance.clear(true);
assert.notOk(ComponentCollection.Instance.getCustomQuestionByName("newquestion1"), "newquestion1 is not here, #2");
assert.notOk(ComponentCollection.Instance.getCustomQuestionByName("newquestion2"), "newquestion2 is not here, #2");
});

0 comments on commit da9981e

Please sign in to comment.