Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbox: Publish the getSubitem() method #6023

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions packages/survey-creator-core/src/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,15 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
return this.title.toLowerCase().indexOf(textLowerCase) >= 0 || this.name.toLowerCase().indexOf(textLowerCase) >= 0;
}

public getSubitemByName(id: string): QuestionToolboxItem {
return this.items?.filter(i => i.id === id)[0];
/**
* Finds a subitem with a specified name in the collection of subitems belonging to this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @param name A subitem [`name`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem#name).
* @returns A [`QuestionToolboxItem`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolboxitem) object that represents the subitem instance.
*/
public getSubitem(name: string): QuestionToolboxItem {
return this.items?.filter(i => i.id === name)[0];
}

public addSubitems(items: Array<QuestionToolboxItem>) {
Expand Down Expand Up @@ -270,9 +277,9 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
* @see removeSubitem
* @see clearSubitems
*/
public addSubitem(item: IQuestionToolboxItem, index: number = -1): void {
if (!item) return;
const newItem: QuestionToolboxItem = new QuestionToolboxItem(item);
public addSubitem(subitem: IQuestionToolboxItem, index: number = -1): void {
if (!subitem) return;
const newItem: QuestionToolboxItem = new QuestionToolboxItem(subitem);
newItem.iconName = "";
if (!newItem.className) newItem.className = QuestionToolboxItem.getItemClassNames(newItem.iconName);
newItem.className = new CssClassBuilder().append(newItem.className).append("svc-toolbox__item-subtype").toString();
Expand All @@ -292,13 +299,13 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
* @see clearSubitems
* @see addSubitem
*/
public removeSubitem(item: IQuestionToolboxItem | string): void {
if (!this.hasSubItems || !item) return;
public removeSubitem(subitem: IQuestionToolboxItem | string): void {
if (!this.hasSubItems || !subitem) return;

const id: string = (item as IQuestionToolboxItem)?.id || item as string;
const id: string = (subitem as IQuestionToolboxItem)?.id || subitem as string;
if (!id) return;

const removedItem = this.getSubitemByName(id);
const removedItem = this.getSubitem(id);
let array: Array<QuestionToolboxItem> = (this.items || []).slice();
const removedIndex = array.indexOf(removedItem);
if (removedIndex > -1) {
Expand Down