Skip to content

Commit

Permalink
Add items to the Select element dynamically (#131)
Browse files Browse the repository at this point in the history
* Move remove item to list component

* implement add and remove items methods
  • Loading branch information
CyberDex authored Jan 30, 2024
1 parent 0457314 commit 100e6e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,21 @@ export class List extends Container
}
});
}

/**
* Removes items from the list. (Does not destroy them)
* @param itemID - Item to remove (starting from 0).
*/
removeItem(itemID: number)
{
const child = this.children[itemID];

if (!child)
{
return;
}

this.removeChild(child);
this.arrangeChildren();
}
}
9 changes: 1 addition & 8 deletions src/ScrollBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,7 @@ export class ScrollBox extends Container
*/
removeItem(itemID: number)
{
const child = this.list.children[itemID];

if (!child)
{
return;
}

this.list.removeChild(child);
this.list.removeItem(itemID);

this.resize();
}
Expand Down
23 changes: 21 additions & 2 deletions src/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Offset = {
export type SelectItemsOptions = {
items: string[];
backgroundColor: number | string;
width: number;
height: number;
hoverColor?: number;
width?: number;
height?: number;
textStyle?: Partial<TextStyle>;
radius?: number;
};
Expand Down Expand Up @@ -193,6 +193,16 @@ export class Select extends Container
this.scrollBox.y += scrollBox.offset.y ?? 0;
}

this.addItems(items, selected);
}

/**
* Adds items to the dropdown.
* @param items
* @param selected
*/
addItems(items: SelectItemsOptions, selected = 0)
{
this.convertItemsToButtons(items).forEach((button, id) =>
{
const text = button.text;
Expand All @@ -216,6 +226,15 @@ export class Select extends Container
});
}

/**
* Remove items from the dropdown.
* @param itemID - Item to remove (starting from 0).
*/
removeItem(itemID: number)
{
this.scrollBox.removeItem(itemID);
}

/** Toggle the select state (open if closed, closes - id open). */
toggle()
{
Expand Down

0 comments on commit 100e6e2

Please sign in to comment.