Skip to content

Commit

Permalink
fix(Autocomplete) set menu items permanent, implement selected argume…
Browse files Browse the repository at this point in the history
…nt to set new or reset selected values, implement open argument to conditionally open dropdown on menu items change materializecss#551
  • Loading branch information
gselderslaghs committed Jan 4, 2025
1 parent 65ddf18 commit d312c60
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,25 @@ export class Autocomplete extends Component<AutocompleteOptions> {
/**
* Updates the visible or selectable items shown in the menu.
* @param menuItems Items to be available.
* @param selected Selected item ids
* @param open Option to conditionally open dropdown
*/
setMenuItems(menuItems: AutocompleteData[]) {
setMenuItems(menuItems: AutocompleteData[], selected: number[] | string[] = null, open: boolean = true) {
this.menuItems = menuItems;
this.open();
this.options.data = menuItems;
if (selected) {
this.selectedValues = this.menuItems.filter(
(item) => !(selected.indexOf(<never>item.id) === -1)
);
}
if (this.options.isMultiSelect) {
this._renderDropdown();
} else {
this._refreshInputText();
}
if (open) this.open();
this._updateSelectedInfo();
this._triggerChanged();
}

/**
Expand Down

0 comments on commit d312c60

Please sign in to comment.