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

TS compilation errors when adding a new sub-item configuration using the addSubitem function #5871

Merged
merged 3 commits into from
Sep 10, 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
115 changes: 82 additions & 33 deletions packages/survey-creator-core/src/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type overflowBehaviorType = "hideInMenu" | "scroll";
/**
* A toolbox item configuration.
*
* `IQuestionToolboxItem` objects are used in such Toolbox API methods as [`getItemByName(name)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#getItemByName), [`addItem(item, index)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#addItem), [`replaceItem(item)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#replaceItem), and others.
* `IQuestionToolboxItem` objects are used in such Toolbox API methods as [`addItem(item, index)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#addItem), [`replaceItem(item)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#replaceItem), [`addSubitem(subitem, index)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolboxitem#addSubitem), and others.
*
* [Toolbox Customization](https://surveyjs.io/survey-creator/documentation/toolbox-customization (linkStyle))
*/
Expand All @@ -46,7 +46,7 @@ export interface IQuestionToolboxItem extends IAction {
*
* [UI Icons](https://surveyjs.io/form-library/documentation/icons (linkStyle))
*/
iconName: string;
iconName?: string;
/**
* A JSON object used to create a new question or panel when users click this toolbox item. It must contain the `type` property.
*
Expand All @@ -57,11 +57,11 @@ export interface IQuestionToolboxItem extends IAction {
* A user-friendly toolbox item title.
*/
title: string;
className: string;
className?: string;
/**
* A toolbox item tooltip.
*
* If `tooltip` is undefined, the [`title`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem#title) property value is used instead.
* If `tooltip` is undefined, the [`title`](#title) property value is used instead.
*/
tooltip?: string;
isCopied?: boolean;
Expand All @@ -80,32 +80,8 @@ export interface IQuestionToolboxItem extends IAction {
*/
enabled?: boolean;
getArea?: (el: HTMLElement) => HTMLElement;
/**
* Removes all subitems from this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @see removeSubitem
* @see addSubitem
*/
clearSubitems?(): void;
/**
* Adds a subitem to this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @param subitem An `IQuestionToolboxItem` object that represents a subitem configuration.
* @param index *(Optional)* A zero-based index at which to insert the subitem. If you do not specify this parameter, the subitem is added to the end.
* @see removeSubitem
* @see clearSubitems
*/
addSubitem?(subitem: IQuestionToolboxItem, index: number): void;
/**
* Removes a specific subitem from this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @param subitem A subitem [`name`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem#name) or an `IQuestionToolboxItem` object that represents a subitem configuration.
* @see clearSubitems
* @see addSubitem
*/
removeSubitem?(subitem: IQuestionToolboxItem | string): void;
}

Expand Down Expand Up @@ -143,6 +119,11 @@ export class QuestionToolboxCategory extends Base {
}
}
}
/**
* A toolbox item instance.
*
* An object of this class is returned by the `QuestionToolbox`'s [`getItemByName(name)`](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolbox#getItemByName) method.
*/
export class QuestionToolboxItem extends Action implements IQuestionToolboxItem {
static getItemClassNames(iconName?: string): string {
return new CssClassBuilder()
Expand Down Expand Up @@ -171,12 +152,59 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
.toString();
}) as any;
}
/**
* A user-friendly toolbox item title.
*/
public get title(): string {
return this.getTitle();
}
public set title(val: string) {
this.setTitle(val);
}
/**
* Specifies whether users can interact with the toolbox item.
*
* Default value: `true`
*/
public get enabled(): boolean {
return this.getEnabled();
}
public set enabled(val: boolean) {
this.setEnabled(val);
}
className: string;
/**
* An icon name.
*
* [UI Icons](https://surveyjs.io/form-library/documentation/icons (linkStyle))
*/
iconName: string;
/**
* A toolbox item identifier.
*
* > Toolbox item names must be unique.
*/
name: string;
/**
* A JSON object used to create a new question or panel when users click this toolbox item. It must contain the `type` property.
*
* [View Toolbox Customization Demo](https://surveyjs.io/survey-creator/examples/survey-toolbox-customization/ (linkStyle))
*/
json: any;
/**
* A toolbox item tooltip.
*
* If `tooltip` is undefined, the [`title`](#title) property value is used instead.
*/
tooltip: string;
isCopied: boolean;
/**
* A category to which this toolbox item belongs.
*
* Out-of-the-box categories include `"general"`, `"choice"`, `"text"`, `"containers"`, `"matrix"`, and `"misc"`.
*
* Default value: `"general"`
*/
category: string;
toJSON() {
return this.item;
Expand Down Expand Up @@ -210,15 +238,29 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
this.setSubItems({ items: items });
this.component = QuestionToolbox.defaultItemGroupComponent;
}

/**
* Removes all subitems from this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @see removeSubitem
* @see addSubitem
*/
public clearSubitems(): void {
if (this.hasSubItems) {
this.items = [];
this.component = "";
this.popupModel.dispose();
}
}

/**
* Adds a subitem to this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @param subitem An [`IQuestionToolboxItem`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem) object that represents a subitem configuration.
* @param index *(Optional)* A zero-based index at which to insert the subitem. If you do not specify this parameter, the subitem is added to the end.
* @see removeSubitem
* @see clearSubitems
*/
public addSubitem(item: IQuestionToolboxItem, index: number = -1): void {
if (!item) return;
const newItem: QuestionToolboxItem = new QuestionToolboxItem(item);
Expand All @@ -233,7 +275,14 @@ export class QuestionToolboxItem extends Action implements IQuestionToolboxItem
}
this.addSubitems(array);
}

/**
* Removes a specific subitem from this toolbox item.
*
* [Manage Toolbox Subitems](https://surveyjs.io/survey-creator/documentation/toolbox-customization#manage-toolbox-subitems (linkStyle))
* @param subitem A subitem [`name`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem#name) or an [`IQuestionToolboxItem`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem) object that represents a subitem configuration.
* @see clearSubitems
* @see addSubitem
*/
public removeSubitem(item: IQuestionToolboxItem | string): void {
if (!this.hasSubItems || !item) return;

Expand Down Expand Up @@ -757,11 +806,11 @@ export class QuestionToolbox
}
}
/**
* Returns a [toolbox item](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem) with a specified name.
* Returns a [toolbox item](https://surveyjs.io/survey-creator/documentation/api-reference/questiontoolboxitem) with a specified name.
* @param name A toolbox item's [`name`](https://surveyjs.io/survey-creator/documentation/api-reference/iquestiontoolboxitem#name).
* @returns A toolbox item or `null` if a toolbox item with the specified name isn't found.
*/
public getItemByName(name: string): IQuestionToolboxItem {
public getItemByName(name: string): QuestionToolboxItem {
if (!name) return null;
const index: number = this.indexOf(name);
return index > -1 ? this.actions[index] : null;
Expand Down
Loading